L I N K E D L IST       Dr. S. Lovelyn Rose       PSG College of Technology
SINGLY LINKED LIST                   DATA         ADDRESS               Data 1 Data 2 . . . . . . Data n       ADDRESSDOUB...
Start   ID   Name   Desig   Address                            of Node2        ID   Name   Desig   Address of             ...
Start   NULL         Data 1   Address of                                Node 2        Address of   Data 2   Address of    ...
 CIRCULAR LINKED LIST          SINGLY  Start          Data 1    Node 2       Data 2   Node 3            Data 3     Node1 ...
struct node{  int x;                     x   c[10]   next   char c[10];struct node *next;}*current;
create_node(){  current=(struct node *)malloc(sizeof(struct node));  current->x=10; current->c=“try”; current->next=null; ...
Create a linked list for maintaining the employee details  such as Ename,Eid,Edesig.Steps:1)Identify the node structure st...
Number of nodes                                      AllocationInsert_N_Nodes(N)                                      and{...
for(i=1;i<n-1;i++)  {      current->next=create_node();      current=current->next;} }  Start, current             011    ...
startInsert(){                      10     c  c=create_node();  start=c;                       20     c1  c1=create_node()...
Start                        20                        30Insert_a_node(Start){                              Startcurrent= ...
Start   10            20             30             40                         25Steps:1)Have a pointer (slow) to the node...
 Use two pointers namely, fast and slow Move the ‘slow’ pointer by one element in the list For every single movement of...
{current=(struct node *)malloc(sizeof(struct node))fast=slow=Start                    Start          10do                 ...
Insert_last(Start){                               Start   10  current=Start;  while(current->next!=null)                  ...
delete_node(start,x){  temp=start;  temp1=temp;  while(temp!=null)     Last node  {  if(temp->n==x)                      s...
startelse if(temp==start){                         First node                                                 10          ...
Start                 50else{  temp1->next=temp->next;              temp1        20  temp->next=null;  break;             ...
Inserting N nodesInsert_node(N)          create_node(){                       {                        c=(struct node*)mal...
delete_node(start,n){                                 starttemp=startwhile(temp!=null)    End of list          10{        ...
else if(temp->next==null)                    start temp->previous->next=null;else                                         ...
 Download the file fromhttp://www.slideshare.net/lovelynrose/linked-list-17752737Also visit my blogs :  http://datastruct...
Upcoming SlideShare
Loading in …5
×

Linked list without animation

2,195 views
1,991 views

Published on

Insertion and Deletion in Singly, Doubly Linked List

Published in: Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,195
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
233
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Linked list without animation

  1. 1. L I N K E D L IST Dr. S. Lovelyn Rose PSG College of Technology
  2. 2. SINGLY LINKED LIST DATA ADDRESS Data 1 Data 2 . . . . . . Data n ADDRESSDOUBLY LINKED LIST ADDRESS Data 1 Data 2 . . . . . Data n ADDRESS Previous node Data part Next node
  3. 3. Start ID Name Desig Address of Node2 ID Name Desig Address of Node 3 ID Name Desig NULL
  4. 4. Start NULL Data 1 Address of Node 2 Address of Data 2 Address of Node 1 Node 3 Address of Data 3 Node 2 NULL
  5. 5.  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
  6. 6. struct node{ int x; x c[10] next char c[10];struct node *next;}*current;
  7. 7. 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
  8. 8. 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
  9. 9. Number of nodes AllocationInsert_N_Nodes(N) and{ Assignment Start=current=create_node(); Start,current 011 ABI HR NULL
  10. 10. 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
  11. 11. startInsert(){ 10 c c=create_node(); start=c; 20 c1 c1=create_node(); c->next=c1; c2=create_node(); 30 c2 c1->next=c2;}
  12. 12. 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
  13. 13. 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’
  14. 14.  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)
  15. 15. {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
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. Start 50else{ temp1->next=temp->next; temp1 20 temp->next=null; break; temp 10}} Any nodetemp1=temp; 40temp=temp->next;}}
  20. 20. 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
  21. 21. 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}
  22. 22. 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”);}
  23. 23.  Download the file fromhttp://www.slideshare.net/lovelynrose/linked-list-17752737Also visit my blogs : http://datastructuresinterview.blogspot.in/ http://talkcoimbatore.blogspot.in/ http://simpletechnical.blogspot.in/

×