Successfully reported this slideshow.
Upcoming SlideShare
×

2,296 views

Published on

Insertion and deletion in singly linked lists and doubly linked lists using animation. Will be useful for

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

1. 1. L I N K E D L IST (Download file to view animation) Dr. S. Lovelyn Rose PSG College of Technology Coimbatore, India
3. 3.  CIRCULAR LINKED LIST SINGLY Start Data 1 Node 2 Data 2 Node 3 Data 3 Node1 Node 1 Node 2 Node 3 DOUBLY StartNode3 Data 1 Node 2 Node 1 Data 1 Node 3 Node 1 Node 2 Node 2 Data 1 Node1 Node 3
4. 4. struct node{ int x; x c[10] next char c[10];struct node *next;}*current;
5. 5. create_node(){ current=(struct node *)malloc(sizeof(struct node)); current->x=10; current->c=“try”; current->next=null; return current;}Allocation x c[10] nextAssignment 10 try NULL
6. 6. Create a linked list for maintaining the employee details such as Ename,Eid,Edesig.Steps:1)Identify the node structure struct node {2)Allocate space for the node int Eid;3)Insert the node in the list char Ename[10],Edesig[10]; struct node *next; } *current; EID EName EDesig next
7. 7. Number of nodes AllocationInsert_N_Nodes(N) and{ Assignment Start=current=create_node(); Start,current 011 ABI HR NULL
8. 8. for(i=1;i<n-1;i++) { current->next=create_node(); current=current->next;} } Start, current 011 ABI HR i=1 012 Banu DBA NULL
9. 9. startInsert(){ 10 c c=create_node(); start=c; 20 c1 c1=create_node(); c->next=c1; c2=create_node(); 30 c2 c1->next=c2;}
10. 10. Start 20 30Insert_a_node(Start){ Startcurrent= create_node(); 10current->next=Start; 20Start=current;} current 30 10Note : Passing Start as a parameter implies the list already exists
11. 11. Start 10 20 30 40 25Steps:1)Have a pointer (slow) to the node after which the new node is to be inserted2)Create the new node to be inserted and store the address in ‘current’3) Adjust the pointers of ‘slow’ and ‘current’
12. 12.  Use two pointers namely, fast and slow Move the ‘slow’ pointer by one element in the list For every single movement of ‘slow’, move ‘fast’ by 2 elements When ‘fast’ points to the last element, ‘slow’ would be pointing to the middle element This required moving through the list only once So the middle element is found in a time complexity of O(n)
13. 13. {current=(struct node *)malloc(sizeof(struct node))fast=slow=Start Start 10do fast slow{ if(fast->next->next!=null) 20 fast=fast->next->next current else break; slow=slow->next 25 30}while(fast->next!=null)current->next=slow->nextslow->next=current 40}Note : fast and slow are below the node they point to
14. 14. Insert_last(Start){ Start 10 current=Start; while(current->next!=null) 20 { current=current->next; } temp 30 temp=create_node(); 25 current->next=temp;} 40
15. 15. delete_node(start,x){ temp=start; temp1=temp; while(temp!=null) Last node { if(temp->n==x) start { Only one node if(temp==start && temp->next==null) 10 start=null temp
16. 16. startelse if(temp==start){ First node 10 temp start=start->next; temp->next=null; 20 break;} startelse if(temp->next==null) temp1{ 10 temp1->next=null; temp} 20 Last node
17. 17. Start 50else{ temp1->next=temp->next; temp1 20 temp->next=null; break; temp 10}} Any nodetemp1=temp; 40temp=temp->next;}}
18. 18. Inserting N nodesInsert_node(N) create_node(){ { c=(struct node*)malloc(sizeof(structstart=c=create_node(); node))for(i=0;i<N-1;i++) c->x;{ c->previous=null; c1=create_node(); c->next=null; return c; c->next=c1; } c1->previous=c;}} 10 20 start c c1
19. 19. delete_node(start,n){ starttemp=startwhile(temp!=null) End of list 10{ tempif(temp->x==n){ single node if(temp==start){temp->next->previous=null;start=temp->next}
20. 20. else if(temp->next==null) start temp->previous->next=null;else 20 Last node{temp->next->previous=temp->previous; 10temp->previous->next=temp->next; temp} middle node 30}else temp=temp->next;}if(temp==null) print(“Element not found”);}
21. 21. http://datastructuresinterview.blogspot.in/http://talkcoimbatore.blogspot.in/http://simpletechnical.blogspot.in/