Singly Linked List

Download as pps
Download as pps
You are on page 1of 58

Linked List

• A list implemented by each item having a link


to the next item.
• Head points to the first node.
• Last node points to NULL.
P Node A Node B Node C Node D

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=(struct node *) malloc(sizeof(struct node))


If list is empty i.e. P==NULL

P Q

P->data=num 10

p=(struct node *) malloc(sizeof(struct node))


If list is empty i.e. P==NULL

P Q

P->data=num 10 NULL P->link=NULL

p=(struct node *) malloc(sizeof(struct node))


If list is not empty i.e. p!=NULL

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

o/p : 10 o/p : 20 o/p : 30


If list is not empty i.e. p!=NULL
o Traversal & output : false
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 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

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

p=(struct node *) malloc(sizeof(struct node));


If position is 1

Q
P
Node A Node B Node C

10 20 30 NULL

p->data=num 5

p=(struct node *) malloc(sizeof(struct node));


If position is 1

Q
P
Node A Node B Node C

10 20 30 NULL

p->data=num; 5
p->link=q;

p=(struct node *) malloc(sizeof(struct node));


If position is greater than 1
Suppose a node is to be inserted at position 3.

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

counter=1 counter=2 counter=3


If list is not empty i.e. p!=NULL
o Traversal & count : false
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 counter=3


REVERSE
struct node *q=p;
int i,k,l,no_of_nodes;
• Traverse till the no of
no_of_nodes=k=l=count(); nodes i.e till last node
for(i=1;i<=no_of_nodes;i++)
{
on first attempt.
for(k=1;k<l;k++) • After first attempt keep
{
q=q->link;
traversing by reducing
} the traversal with 1 &
printf("%d ",q->data);
q=p;
display the output.
l--;
}
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; }

P Q
Node A Node B Node C Node D

10 20 30 40 NULL

position=1 position=2 position=3 position=4


SEARCH
• Suppose 30 is to be searched.
• position=1;
• while(q!=NULL) {
true
if(q->data==num)
return(position);
position++;
q=q->link; }

P Q
Node A Node B Node C Node D

10 20 30 40 NULL

position=1 position=2 position=3 position=4


REMOVE
• If position is 1
• Position is other than 1
If position is 1

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

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

Node A Node B Node C Node D

1 3 6 7 NULL
Presented by
• Jasbirsingh chauhan

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy