KEMBAR78
Single linked list | PPS
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
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
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
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
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
If list is empty i.e. P==NULL struct node *P=null; struct node *q=p; P Q NULL
If list is empty i.e. P==NULL P Q NULL
If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node))
If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node)) P->data=num 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
If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
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
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
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
DISPLAY If P==NULL then list is empty Else Traverse till the last node  while(q!=NULL) { printf("\n%d:  data=%d",i,q->data); q=q->link; }
If list is empty P Q NULL struct node *P=null; struct node *q=p; If(p==NULL) o/p : list is empty
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
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
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
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
If position is 1 10 20 30 Node A Node B Node C NULL Q P
If position is 1 10 20 30 Node A Node B Node C NULL Q P p=(struct node *) malloc(sizeof(struct node));
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
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;
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.
10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; }
10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
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));
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;
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;
COUNT Initialise counter=0; Traverse till q!=NULL by q=q->link & simultaneously increment the counter with 1 Return the value of counter
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
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
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
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.
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
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
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
REMOVE If position is 1 Position is other than 1
If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q p=p->link
If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
If position is 1 20 30 10 NULL Node B Node C Node D P Q free(q)
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
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
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;
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
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
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
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
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
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
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
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
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
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
Presented by Jasbirsingh chauhan

Single linked list

  • 1.
    A list implementedby 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 isa 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 structfor 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 listis 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 isempty i.e. P==NULL struct node *P=null; struct node *q=p; P Q NULL
  • 7.
    If list isempty i.e. P==NULL P Q NULL
  • 8.
    If list isempty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node))
  • 9.
    If list isempty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node)) P->data=num 10
  • 10.
    If list isempty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node)) P->data=num 10 P->link=NULL NULL
  • 11.
    If list isnot empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
  • 12.
    If list isnot 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 isnot 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 isnot 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==NULLthen list is empty Else Traverse till the last node while(q!=NULL) { printf(&quot;\n%d: data=%d&quot;,i,q->data); q=q->link; }
  • 16.
    If list isempty P Q NULL struct node *P=null; struct node *q=p; If(p==NULL) o/p : list is empty
  • 17.
    If list isnot 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 isnot 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 isnot 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 positionis 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 is1 10 20 30 Node A Node B Node C NULL Q P
  • 22.
    If position is1 10 20 30 Node A Node B Node C NULL Q P p=(struct node *) malloc(sizeof(struct node));
  • 23.
    If position is1 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 is1 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 isgreater 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 30Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; }
  • 27.
    10 20 30Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
  • 28.
    10 20 30Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
  • 29.
    10 20 30Node 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 30Node 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 30Node 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 isnot 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 isnot 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 isnot 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 isthe 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 30is 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 30is 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 positionis 1 Position is other than 1
  • 41.
    If position is1 10 20 30 10 NULL Node A Node B Node C Node D P Q
  • 42.
    If position is1 10 20 30 10 NULL Node A Node B Node C Node D P Q p=p->link
  • 43.
    If position is1 10 20 30 10 NULL Node A Node B Node C Node D P Q
  • 44.
    If position is1 20 30 10 NULL Node B Node C Node D P Q free(q)
  • 45.
    Suppose 3 nodeis 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 nodeis 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 nodeis 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.