Your SlideShare is downloading. ×
Single linked list
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Single linked list

18,316

Published on

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

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

Published in: Education
31 Comments
55 Likes
Statistics
Notes
No Downloads
Views
Total Views
18,316
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
155
Comments
31
Likes
55
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1.
    • A list implemented by each item having a link to the next item.
    • Head points to the first node.
    • Last node points to NULL.
    Linked List 10 20 30 10 NULL Node A Node B Node C Node D P
  • 2.
    • Linked List is a series of nodes.
    • Each node consists of 2 parts viz. Data part & pointer part.
    • Pointer part stores the address of next node.
    Node DATA POINTER 10 NODE
  • 3. Declarations
    • p : always points to the first node
    • q: used for traversing the list, initialised with p
    • temp : used for refering the newly created node
    • link: points to the address of the next node
      • Eg: q->link
    • data: consists data of a node
      • E.g: q->data
    • num: Number entered by the user
    • position: indicates the position of the node
    • counter: counts the no of node
  • 4.
    • Declare Node struct for nodes
      • data: int-type data in this example
      • next: a pointer to the next node in the list
    • struct node
    • {
    • int data; //data
    • struct node *link; // pointer
    • };
    Structure
  • 5. ADD
    • If list is empty i.e. P==NULL then create a new node with data and set its pointer to NULL
    • If list is not empty i.e. P!=NULL then
      • Traverse till the last node
      • create a node with data
      • set its pointer to NULL
  • 6. If list is empty i.e. P==NULL
    • struct node *P=null;
    • struct node *q=p;
    P Q NULL
  • 7. If list is empty i.e. P==NULL P Q NULL
  • 8. If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node))
  • 9. If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node)) P->data=num 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. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
  • 12. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
    • Traversal :
      • while(q->link!=NULL) q=q->link;
    true
  • 13. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
    • Traversal :
      • while(q->link!=NULL) q=q->link;
    false
  • 14. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C P Q
    • Creating a node :
      • q->link=(struct node *) malloc(sizeof(struct node))
      • q->link->data=num
      • q->link->link=NULL
    10 Node D NULL
  • 15. DISPLAY
    • If P==NULL then list is empty
    • Else
      • Traverse till the last node
        • while(q!=NULL)
        • {
        • printf(" %d: data=%d",i,q->data);
        • q=q->link;
        • }
  • 16. If list is empty P Q NULL
    • struct node *P=null;
    • struct node *q=p;
    • If(p==NULL)
    • o/p : list is empty
  • 17. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
    • Traversal & output :
      • while(q!=NULL)
      • {
      • printf(“%d ”,q->data);
      • q=q->link;
      • }
    true o/p : 10 o/p : 20
  • 18. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
    • Traversal & output :
      • while(q!=NULL)
      • {
      • printf(“%d ”,q->data);
      • q=q->link;
      • }
    o/p : 10 o/p : 20 o/p : 30
  • 19. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B NULL Node C P Q
    • Traversal & output :
      • while(q!=NULL)
      • {
      • printf(“%d ”,q->data);
      • q=q->link;
      • }
    false o/p : 10 o/p : 20 o/p : 30
  • 20. INSERT
    • 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.
    • 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
  • 21. If position is 1 10 20 30 Node A Node B Node C NULL Q P
  • 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. 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. 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. 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. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; }
  • 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. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
  • 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. 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. 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. COUNT
    • Initialise counter=0;
    • Traverse till q!=NULL by q=q->link & simultaneously increment the counter with 1
    • Return the value of counter
  • 33. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
    • Traversal & count :
      • counter=0;
      • while(q!=NULL)
      • {
      • counter++;
      • q=q->link;
      • }
      • return counter;
    true counter= 1 counter=2
  • 34. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
    • Traversal & count :
      • counter=0;
      • while(q!=NULL)
      • {
      • counter++;
      • q=q->link;
      • }
      • return counter;
    counter=1 counter=2 counter=3
  • 35. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B NULL Node C P Q
    • Traversal & count :
      • counter=0;
      • while(q!=NULL)
      • {
      • counter++;
      • q=q->link;
      • }
      • return counter;
    false counter=3 counter=1 counter=2
  • 36. REVERSE
    • struct node *q=p;
    • int i,k,l,no_of_nodes;
    • no_of_nodes=k=l=count();
    • for(i=1;i<=no_of_nodes;i++)
    • {
    • for(k=1;k<l;k++)
    • {
    • q=q->link;
    • }
    • printf(&quot;%d &quot;,q->data);
    • q=p;
    • l--;
    • }
    • Traverse till the no of nodes i.e till last node on first attempt.
    • After first attempt keep traversing by reducing the traversal with 1 & display the output.
  • 37. SEARCH
    • num is the number to be searched
    • position =1
    • 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.
    • Return -1 if the no is not found
  • 38. SEARCH
    • Suppose 30 is to be searched.
    • position=1;
    • while(q!=NULL) {
    • if(q->data==num)
    • return(position);
    • position++;
    • q=q->link; }
    10 20 30 Node A Node B Node C P Q 40 NULL Node D position=1 position=2 position=3 position=4
  • 39. SEARCH
    • Suppose 30 is to be searched.
    • position=1;
    • while(q!=NULL) {
    • if(q->data==num)
    • return(position);
    • position++;
    • q=q->link; }
    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. REMOVE
    • If position is 1
    • Position is other than 1
  • 41. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
  • 42. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
    • p=p->link
  • 43. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
  • 44. If position is 1 20 30 10 NULL Node B Node C Node D P Q
    • free(q)
  • 45.
    • Suppose 3 node is to be removed, position=3
    • for(i=1;i<=position-2;i++)
    • {
    • q=q->link;
    • }
    10 20 30 Node A Node B Node C P Q 40 NULL Node D
  • 46.
    • Suppose 3 node is to be removed, position=3
    • for(i=1;i<=position-2;i++)
    • {
    • q=q->link;
    • }
    10 20 30 Node A Node B Node C P Q 40 NULL Node D
  • 47.
    • Suppose 3 node is to be removed, position=3
    • for(i=1;i<=position-2;i++)
    • {
    • q=q->link;
    • }
    10 20 30 Node A Node B Node C P Q 40 NULL Node D q->link=q->link->link;
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Presented by
    • Jasbirsingh chauhan

×