0% found this document useful (0 votes)
24 views

Circular Linked List

The document discusses insertion of nodes at the beginning and end of a circular linked list. It shows the steps to insert nodes with data 20, 30, and 40 at the end of an initially empty list. It then shows how to insert a node with data 50 at the beginning by (1) finding the last node, (2) updating its next pointer, and (3) making the new node the head by assigning the pointer s to it. Pseudocode for functions to insert at the end and beginning are also provided.

Uploaded by

ShubhamSidana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Circular Linked List

The document discusses insertion of nodes at the beginning and end of a circular linked list. It shows the steps to insert nodes with data 20, 30, and 40 at the end of an initially empty list. It then shows how to insert a node with data 50 at the beginning by (1) finding the last node, (2) updating its next pointer, and (3) making the new node the head by assigning the pointer s to it. Pseudocode for functions to insert at the end and beginning are also provided.

Uploaded by

ShubhamSidana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

INSERTION AT END

& BEGINING
IN
CIRCULAR LINKED
BY
Mr.
LIST
Shubham Sidana
Assistant Professor
ABES Engineering College
Dr. A.P.J. Abdul Kalam Technical
University

Initially,

NULL
s

Initially,

NULL
s

Insert 20 at
end:
NULL
s

//Since s is NULL, so it is first


insertion
Create node

Initially,

NULL
s

Insert 20 at
end:

NUL
L

//Since s is NULL, so it is first


insertion
20

Insert 20 in
data part

Initially,

NULL
s

Insert 20 at
end:

NUL
L
p

//Since s is NULL, so it is first


insertion
20

Store its
address in
pointer p

Initially,

NULL
s

Insert 20 at
end:

//Since s is NULL, so it is first


insertion
20

Make s point
to first node
// s=p;

Initially,

NULL
s

Insert 20 at
end:

//Since s is NULL, so it is first


insertion
20

Make circular
link
// s->next=s;

20

Insert 30 at
end:

20

30

Create node
with data 30
& Store its
address in
pointer p

20

Insert 30 at
end:
temp

20

30

while(temp->next!
= s)
{
temp=temp>next;
}

20

Insert 30 at
end:
temp

20

30

temp->next = p;

20

Insert 30 at
end:
temp

20

30

p ->next = s;

20

30

Insert 40 at
end:

20

40

30

Create node
with data 40
& Store its
address in
pointer p

20

30

Insert 40 at
end:

20

40

30

p
temp

while(temp>next! = s)
{
temp=temp>next;
}

20

30

Insert 40 at
end:

20

40

30

p
temp

while(temp>next! = s)
{
temp=temp>next;
}

20

30

Insert 40 at
end:

20

40

30

p
temp

temp->next = p;

20

30

Insert 40 at
end:

20

40

30

p
temp

p ->next = s;

20

40

30

Insert 50 at
Beginning:

50

20

30

Create node with data


50 and store its address
in pointer p

40

20

40

30

Insert 50 at
Beginning:

50

20

30

temp

while(temp->next! =
s)
{
temp=temp->next;
}

40

20

40

30

Insert 50 at
Beginning:

50

20

30

temp

while(temp->next! =
s)
{
temp=temp->next;
}

40

20

40

30

Insert 50 at
Beginning:

50

20

40

30

temp

while(temp->next! =
s)
{
temp=temp->next;
}

20

40

30

Insert 50 at
Beginning:

50

20

30

40

temp

temp->next = p;

20

40

30

Insert 50 at
Beginning:

50

20

30

40

temp

p->next = s;

20

40

30

Insert 50 at
Beginning:

s
p

50

20

30

40

temp

s = p;

20

40

30

Insert 50 at
Beginning:

s
p

50

20

30

40

temp

s = p;
Note: This last step differentiates
INSERT AT END & INSERT AT
BEGINNING
Which is extra in Insert at Beginning

void ins_at_end()
{
struct node *p, *temp;
temp = s;
p = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d", &pdata);
if ( s = = NULL )
{
s = p;
s next = s;
return;
}
while (tempnext != s)
{
temp = temp next;
}
tempnext = p;
pnext = s;
}

Insert at END

void ins_at_beg()
{
struct node *p, *temp;
temp = s;
p = (struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d", &pdata);
if ( s = = NULL )
{
s = p;
s next = s;
return;
}
while (tempnext != s)
{
temp = temp next;
}
tempnext = p;
pnext = s;
s = p; }

Insert at
beginning

You might also like