Single linked list

21,740
-1

Published on

Includes various functions that can be performed on single linked list with animation

Published in: Education
41 Comments
71 Likes
Statistics
Notes
No Downloads
Views
Total Views
21,740
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
155
Comments
41
Likes
71
Embeds 0
No embeds

No notes for slide

Single linked list

  1. 1. <ul><li>A list implemented by each item having a link to the next item. </li></ul><ul><li>Head points to the first node. </li></ul><ul><li>Last node points to NULL. </li></ul>Linked List 10 20 30 10 NULL Node A Node B Node C Node D P
  2. 2. <ul><li>Linked List is a series of nodes. </li></ul><ul><li>Each node consists of 2 parts viz. Data part & pointer part. </li></ul><ul><li>Pointer part stores the address of next node. </li></ul>Node DATA POINTER 10 NODE
  3. 3. Declarations <ul><li>p : always points to the first node </li></ul><ul><li>q: used for traversing the list, initialised with p </li></ul><ul><li>temp : used for refering the newly created node </li></ul><ul><li>link: points to the address of the next node </li></ul><ul><ul><li>Eg: q->link </li></ul></ul><ul><li>data: consists data of a node </li></ul><ul><ul><li>E.g: q->data </li></ul></ul><ul><li>num: Number entered by the user </li></ul><ul><li>position: indicates the position of the node </li></ul><ul><li>counter: counts the no of node </li></ul>
  4. 4. <ul><li>Declare Node struct for nodes </li></ul><ul><ul><li>data: int-type data in this example </li></ul></ul><ul><ul><li>next: a pointer to the next node in the list </li></ul></ul><ul><li>struct node </li></ul><ul><li>{ </li></ul><ul><li>int data; //data </li></ul><ul><li>struct node *link; // pointer </li></ul><ul><li>}; </li></ul>Structure
  5. 5. ADD <ul><li>If list is empty i.e. P==NULL then create a new node with data and set its pointer to NULL </li></ul><ul><li>If list is not empty i.e. P!=NULL then </li></ul><ul><ul><li>Traverse till the last node </li></ul></ul><ul><ul><li>create a node with data </li></ul></ul><ul><ul><li>set its pointer to NULL </li></ul></ul>
  6. 6. If list is empty i.e. P==NULL <ul><li>struct node *P=null; </li></ul><ul><li>struct node *q=p; </li></ul>P Q NULL
  7. 7. If list is empty i.e. P==NULL P Q NULL
  8. 8. If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node))
  9. 9. If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node)) P->data=num 10
  10. 10. If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node)) P->data=num 10 P->link=NULL NULL
  11. 11. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
  12. 12. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q <ul><li>Traversal : </li></ul><ul><ul><li>while(q->link!=NULL) q=q->link; </li></ul></ul>true
  13. 13. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q <ul><li>Traversal : </li></ul><ul><ul><li>while(q->link!=NULL) q=q->link; </li></ul></ul>false
  14. 14. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C P Q <ul><li>Creating a node : </li></ul><ul><ul><li>q->link=(struct node *) malloc(sizeof(struct node)) </li></ul></ul><ul><ul><li>q->link->data=num </li></ul></ul><ul><ul><li>q->link->link=NULL </li></ul></ul>10 Node D NULL
  15. 15. DISPLAY <ul><li>If P==NULL then list is empty </li></ul><ul><li>Else </li></ul><ul><ul><li>Traverse till the last node </li></ul></ul><ul><ul><ul><li>while(q!=NULL) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>printf(&quot; %d: data=%d&quot;,i,q->data); </li></ul></ul></ul><ul><ul><ul><li>q=q->link; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  16. 16. If list is empty P Q NULL <ul><li>struct node *P=null; </li></ul><ul><li>struct node *q=p; </li></ul><ul><li>If(p==NULL) </li></ul><ul><li>o/p : list is empty </li></ul>
  17. 17. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q <ul><li>Traversal & output : </li></ul><ul><ul><li>while(q!=NULL) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>printf(“%d ”,q->data); </li></ul></ul><ul><ul><li>q=q->link; </li></ul></ul><ul><ul><li>} </li></ul></ul>true o/p : 10 o/p : 20
  18. 18. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q <ul><li>Traversal & output : </li></ul><ul><ul><li>while(q!=NULL) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>printf(“%d ”,q->data); </li></ul></ul><ul><ul><li>q=q->link; </li></ul></ul><ul><ul><li>} </li></ul></ul>o/p : 10 o/p : 20 o/p : 30
  19. 19. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B NULL Node C P Q <ul><li>Traversal & output : </li></ul><ul><ul><li>while(q!=NULL) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>printf(“%d ”,q->data); </li></ul></ul><ul><ul><li>q=q->link; </li></ul></ul><ul><ul><li>} </li></ul></ul>false o/p : 10 o/p : 20 o/p : 30
  20. 20. INSERT <ul><li>If position is 1 then create a node with data and set its pointer to first node i.e. q as it also points to first node. </li></ul><ul><li>If position is after the first node then traverse till the node before the insertion position where the node is to be inserted. Save the link of previous node in a temp. Overwrite the </li></ul>
  21. 21. If position is 1 10 20 30 Node A Node B Node C NULL Q P
  22. 22. If position is 1 10 20 30 Node A Node B Node C NULL Q P p=(struct node *) malloc(sizeof(struct node));
  23. 23. If position is 1 10 20 30 Node A Node B Node C NULL Q P p=(struct node *) malloc(sizeof(struct node)); p->data=num 5
  24. 24. If position is 1 10 20 30 Node A Node B Node C NULL Q P p=(struct node *) malloc(sizeof(struct node)); p->data=num; 5 p->link=q;
  25. 25. If position is greater than 1 10 20 30 Node A Node B Node C NULL Q P Suppose a node is to be inserted at position 3.
  26. 26. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; }
  27. 27. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
  28. 28. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
  29. 29. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp Node BC q->link=(struct node *) malloc(sizeof(struct node));
  30. 30. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp Node BC q->link=(struct node *) malloc(sizeof(struct node)); 25 q->link->data=num;
  31. 31. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp Node BC q->link=(struct node *) malloc(sizeof(struct node)); 25 q->link->data=num; q->link->link=temp;
  32. 32. COUNT <ul><li>Initialise counter=0; </li></ul><ul><li>Traverse till q!=NULL by q=q->link & simultaneously increment the counter with 1 </li></ul><ul><li>Return the value of counter </li></ul>
  33. 33. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q <ul><li>Traversal & count : </li></ul><ul><ul><li>counter=0; </li></ul></ul><ul><ul><li>while(q!=NULL) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>counter++; </li></ul></ul><ul><ul><li>q=q->link; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>return counter; </li></ul></ul>true counter= 1 counter=2
  34. 34. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q <ul><li>Traversal & count : </li></ul><ul><ul><li>counter=0; </li></ul></ul><ul><ul><li>while(q!=NULL) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>counter++; </li></ul></ul><ul><ul><li>q=q->link; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>return counter; </li></ul></ul>counter=1 counter=2 counter=3
  35. 35. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B NULL Node C P Q <ul><li>Traversal & count : </li></ul><ul><ul><li>counter=0; </li></ul></ul><ul><ul><li>while(q!=NULL) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>counter++; </li></ul></ul><ul><ul><li>q=q->link; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>return counter; </li></ul></ul>false counter=3 counter=1 counter=2
  36. 36. REVERSE <ul><li>struct node *q=p; </li></ul><ul><li>int i,k,l,no_of_nodes; </li></ul><ul><li>no_of_nodes=k=l=count(); </li></ul><ul><li>for(i=1;i<=no_of_nodes;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(k=1;k<l;k++) </li></ul><ul><li>{ </li></ul><ul><li>q=q->link; </li></ul><ul><li>} </li></ul><ul><li>printf(&quot;%d &quot;,q->data); </li></ul><ul><li>q=p; </li></ul><ul><li>l--; </li></ul><ul><li>} </li></ul><ul><li>Traverse till the no of nodes i.e till last node on first attempt. </li></ul><ul><li>After first attempt keep traversing by reducing the traversal with 1 & display the output. </li></ul>
  37. 37. SEARCH <ul><li>num is the number to be searched </li></ul><ul><li>position =1 </li></ul><ul><li>Traverse the list till the last node & keep comparing the values of list with num the moment it matches return the position which is incremented by everytime. </li></ul><ul><li>Return -1 if the no is not found </li></ul>
  38. 38. SEARCH <ul><li>Suppose 30 is to be searched. </li></ul><ul><li>position=1; </li></ul><ul><li>while(q!=NULL) { </li></ul><ul><li>if(q->data==num) </li></ul><ul><li>return(position); </li></ul><ul><li>position++; </li></ul><ul><li>q=q->link; } </li></ul>10 20 30 Node A Node B Node C P Q 40 NULL Node D position=1 position=2 position=3 position=4
  39. 39. SEARCH <ul><li>Suppose 30 is to be searched. </li></ul><ul><li>position=1; </li></ul><ul><li>while(q!=NULL) { </li></ul><ul><li>if(q->data==num) </li></ul><ul><li>return(position); </li></ul><ul><li>position++; </li></ul><ul><li>q=q->link; } </li></ul>10 20 30 Node A Node B Node C P Q 40 NULL Node D true position=1 position=2 position=3 position=4
  40. 40. REMOVE <ul><li>If position is 1 </li></ul><ul><li>Position is other than 1 </li></ul>
  41. 41. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
  42. 42. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q <ul><li>p=p->link </li></ul>
  43. 43. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
  44. 44. If position is 1 20 30 10 NULL Node B Node C Node D P Q <ul><li>free(q) </li></ul>
  45. 45. <ul><li>Suppose 3 node is to be removed, position=3 </li></ul><ul><li>for(i=1;i<=position-2;i++) </li></ul><ul><li>{ </li></ul><ul><li>q=q->link; </li></ul><ul><li>} </li></ul>10 20 30 Node A Node B Node C P Q 40 NULL Node D
  46. 46. <ul><li>Suppose 3 node is to be removed, position=3 </li></ul><ul><li>for(i=1;i<=position-2;i++) </li></ul><ul><li>{ </li></ul><ul><li>q=q->link; </li></ul><ul><li>} </li></ul>10 20 30 Node A Node B Node C P Q 40 NULL Node D
  47. 47. <ul><li>Suppose 3 node is to be removed, position=3 </li></ul><ul><li>for(i=1;i<=position-2;i++) </li></ul><ul><li>{ </li></ul><ul><li>q=q->link; </li></ul><ul><li>} </li></ul>10 20 30 Node A Node B Node C P Q 40 NULL Node D q->link=q->link->link;
  48. 48. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 7 6 3 Node A Node B Node C 1 NULL Node D i j
  49. 49. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 7 6 3 Node A Node B Node C 1 NULL Node D i j
  50. 50. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 6 7 3 Node A Node B Node C 1 NULL Node D i j
  51. 51. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 3 7 6 Node A Node B Node C 1 NULL Node D i j
  52. 52. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 3 7 6 Node A Node B Node C 1 NULL Node D i j
  53. 53. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 1 7 6 Node A Node B Node C 3 NULL Node D i j
  54. 54. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 1 6 7 Node A Node B Node C 3 NULL Node D i j
  55. 55. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 1 3 7 Node A Node B Node C 6 NULL Node D i j
  56. 56. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 1 3 7 Node A Node B Node C 6 NULL Node D i j
  57. 57. SORTING Traversal & comparison: for(i=p;i!=NULL;i=i->link) { for(j=i->link;j!=NULL;j=j->link) { if(i->data>j->data) { temp=i->data; i->data=j->data; j->data=temp; } } } 1 3 6 Node A Node B Node C 7 NULL Node D i j
  58. 58. Presented by <ul><li>Jasbirsingh chauhan </li></ul>

×