Skip to main content

Insert node at first position in doubly link list

Now we will see how to insert a new node at first position in doubly link list.

Algorithm for insertion at first position in doubly link list:

In this algorithm, START is pointer to first node and PTR is node to be inserted. The step are as follows:
  • Create new node PTR
  • Set the info field of PTR
  • Set PTR->PREV = NULL
  • If list is empty i.e. START == NULL
    • START = PTR
    • PTR->NEXT = NULL
  • Else
    • PTR->NEXT = START
    • START->PREV = PTR
    • START = PTR
  • End If;

Function to insert node at first position in doubly link list:


void insertAtFirst(NODE **start, int info){
 NODE *ptr = (NODE*) malloc(sizeof(NODE));
 ptr->info = info;
 ptr->prev = NULL;
 if(*start == NULL){
  *start = ptr;
  ptr->next = NULL;
 }
 else{
  ptr->next = *start;
  (*start)->prev = ptr;
  *start = ptr;
 }
}

 

Program to insert at first position in the doubly link list:


#include <stdio.h>
#include <malloc.h>

typedef struct node{
 int info;
 struct node *next, *prev;
} NODE;

void insertAtFirst(NODE **, int);
void traverse(NODE **);

int main(){
 NODE *start = NULL;
 
 insertAtFirst(&start, 5);
 insertAtFirst(&start, 3);
 traverse(&start);
 return 0;
}

void insertAtFirst(NODE **start, int info){
 NODE *ptr = (NODE*) malloc(sizeof(NODE));
 ptr->info = info;
 ptr->prev = NULL;
 if(*start == NULL){
  *start = ptr;
  ptr->next = NULL;
 }
 else{
  ptr->next = *start;
  (*start)->prev = ptr;
  *start = ptr;
 }
}

void traverse(NODE **start){
 NODE *temp = *start;
 while(temp != NULL){
  printf("%d  ", temp->info);
  temp = temp->next;
 }
}

 
2  1
 

Comments

Popular posts from this blog

Link List

In computer science, a linked list is a linear collection of elements, called nodes, each node have info field and next field pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes in sequence and connected with links. In simplest form of this data structure, each node is consist of data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration. Types of Link List Singly Link List: Insert node at first position in singly link list Insert node at last position in singly link list Insert node at specific position in singly link list Delete node at first position in singly link list Delete node at last position in singly link list Delete node at specific position in singly link list Find number of nodes in singly link list Dublication of singly link list Concatenation of two singly link list So...

Deletion of node at specific position N in circular link list

Now we will see how to delete node at position 'N' in circular link list. Algorithm to delete node at position 'N' in circular link list: In this algorithm, TAIL points to last node of the link list and POS is position where the new node to be deleted. The steps are as follows: Initialize TEMP and TEMP2 If list have no node or position is less than one  (TAIL == NULL or POS < 1) Node cannot be deleted Else If (POS == 1) If list have one node   (TAIL->NEXT == TAIL) Delete TAIL   (TAIL = NULL) Else Delete the first node   (TAIL->NEXT = TAIL->NEXT->NEXT) End If; Else Set TEMP = TAIL->NEXT   (TEMP now points to first node) Traverse the list for (POS-1)th node using TEMP If node to be deleted is last node i.e. (TEMP->NEXT == TAIL) Set TAIL = TEMP End If; Set TEMP->NEXT = TEMP->NEXT->NEXT End If; End If;     Function to delete node at position 'N' in circular link list: int deleteAtPosition(NODE **tail, ...

Postfix Expression Evaluation

With a given Postfix Expression, we will show you how to evaluate a postfix expression using stack.   Algorithm to evaluate postfix expression: In this algorithm, we will use stack to store operands. The step are as follows: Get the Postfix Expression String While the end of Postfix Expression string If the current character is operand Push it into Stack End If If the current character is operator Pop stack for second_operand Pop stack for first_operand Evaluate the expression (first_operand)(operator)(second_operand) and push the result into stack End If End While Pop stack for result and return it       Function to Evaluate Postfix Expression: double evaluate_postfix_exp(char *poststr){ STACK stk; //stack for pushing operands int i = -1; double op1, op2; stk.top = -1; //top of stack while(poststr[++i]){ if(poststr[i] == ' '){ //escape space continue; } if(isdig(poststr[i])){ push( &stk, (double)(poststr[i]-'0')/*char to int...