Singly Linked List
Singly Linked List
Singly Linked List
10 20 30 10 NULL
Node
• 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
10
DATA POINTER
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
Structure
• 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
};
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 Q
P->data=num 10
P Q
P Q
Node A Node B Node C
10 20 30 NULL
If list is not empty i.e. p!=NULL
o Traversal : true
while(q->link!=NULL)
q=q->link;
P Q
Node A Node B Node C
10 20 30 NULL
If list is not empty i.e. p!=NULL
o Traversal : false
while(q->link!=NULL)
q=q->link;
P Q
Node A Node B Node C
10 20 30 NULL
If list is not empty i.e. p!=NULL
o Creating a node :
q->link=(struct node *) malloc(sizeof(struct node))
q->link->data=num
q->link->link=NULL
P Q
Node A Node B Node C Node D
10 20 30 10 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
• struct node *P=null;
• struct node *q=p;
• If(p==NULL)
o/p : list is empty
P Q
NULL
If list is not empty i.e. p!=NULL
o Traversal & output : true
while(q!=NULL)
{
printf(“%d ”,q->data);
q=q->link;
}
P Q
Node A Node B Node C
10 20 30 NULL
o/p : 10 o/p : 20
If list is not empty i.e. p!=NULL
o Traversal & output :
while(q!=NULL)
{
printf(“%d ”,q->data);
q=q->link;
}
P Q
Node A Node B Node C
10 20 30 NULL
P Q
Node A Node B Node C
10 20 30 NULL
Q
P
Node A Node B Node C
10 20 30 NULL
If position is 1
Q
P
Node A Node B Node C
10 20 30 NULL
Q
P
Node A Node B Node C
10 20 30 NULL
p->data=num 5
Q
P
Node A Node B Node C
10 20 30 NULL
p->data=num; 5
p->link=q;
Q
P
Node A Node B Node C
10 20 30 NULL
for(i=1;i<=3-2;i++)
{
q=q->link;
}
P Q
Node A Node B Node C
10 20 30 NULL
for(i=1;i<=3-2;i++)
{
q=q->link;
}
P Q
Node A Node B Node C
10 20 30 NULL
temp
for(i=1;i<=3-2;i++)
{
q=q->link;
}
P Q
Node A Node B Node C
10 20 30 NULL
temp
for(i=1;i<=3-2;i++)
{
q=q->link; q->link=(struct node *) malloc(sizeof(struct node));
}
P Q
Node A Node B Node BC Node C
10 20 30 NULL
temp
for(i=1;i<=3-2;i++)
{
q=q->link; q->link=(struct node *) malloc(sizeof(struct node));
}
P Q
Node A Node B Node BC Node C
10 20 25 30 NULL
q->
link
- >da
ta=
n um
;
temp
for(i=1;i<=3-2;i++)
{
q=q->link; q->link=(struct node *) malloc(sizeof(struct node));
}
P Q
Node A Node B Node BC Node C
10 20 25 30 NULL
q->
link q->link->link=temp;
- >da
ta=
n um
;
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
o Traversal & count : true
counter=0;
while(q!=NULL)
{
counter++;
q=q->link;
}
return counter;
P Q
Node A Node B Node C
10 20 30 NULL
counter= 1 counter=2
If list is not empty i.e. p!=NULL
o Traversal & count :
counter=0;
while(q!=NULL)
{
counter++;
q=q->link;
}
return counter;
P Q
Node A Node B Node C
10 20 30 NULL
10 20 30 NULL
P Q
Node A Node B Node C Node D
10 20 30 40 NULL
P Q
Node A Node B Node C Node D
10 20 30 40 NULL
P Q
Node A Node B Node C Node D
10 20 30 10 NULL
If position is 1
• p=p->link
P Q
Node A Node B Node C Node D
10 20 30 10 NULL
If position is 1
P Q
Node A Node B Node C Node D
10 20 30 10 NULL
If position is 1
• free(q)
P Q
Node B Node C Node D
20 30 10 NULL
• Suppose 3 node is to be removed, position=3
• for(i=1;i<=position-2;i++)
{
q=q->link;
}
P Q
Node A Node B Node C Node D
10 20 30 40 NULL
• Suppose 3 node is to be removed, position=3
• for(i=1;i<=position-2;i++)
{
q=q->link;
}
P Q
Node A Node B Node C Node D
10 20 30 40 NULL
• Suppose 3 node is to be removed, position=3
• for(i=1;i<=position-2;i++)
{
q=q->link;
}
P Q
Node A Node B Node C Node D
10 20 30 40 NULL
q->link=q->link->link;
Traversal & comparison:
SORTING
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;
}
}
}
i j
7 6 3 1 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
7 6 3 1 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
6 7 3 1 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
3 7 6 1 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
3 7 6 1 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
1 7 6 3 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
1 6 7 3 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
1 3 7 6 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
1 3 7 6 NULL
Traversal & comparison:
SORTING
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;
}
}
}
i j
1 3 6 7 NULL
Presented by
• Jasbirsingh chauhan