circular linked list
TRANSCRIPT
INSERTION AT END & BEGINING
IN CIRCULAR LINKED LIST
BY Mr. Shubham SidanaAssistant ProfessorABES Engineering CollegeDr. A.P.J. Abdul Kalam Technical University
Initially, NULLs
Initially, NULLs
Insert 20 at end: //Since s is NULL, so it is first insertion
NULLs
Create node
Initially, NULLs
Insert 20 at end: //Since s is NULL, so it is first insertion
NULLs
20 Insert 20 in data part
Initially, NULLs
Insert 20 at end: //Since s is NULL, so it is first insertion
NULLs
20 Store its address in pointer pp
Initially, NULLs
Insert 20 at end: //Since s is NULL, so it is first insertion
s20 Make s point to
first node
// s=p;
p
Initially, NULLs
Insert 20 at end: //Since s is NULL, so it is first insertion
s20 Make circular
link
// s->next=s;
s20
Insert 30 at end:
s20 30 Create node
with data 30 & Store its address in pointer p
p
s20
Insert 30 at end:
s20 30 while(temp->next! = s)
{ temp=temp->next; }
p
temp
s20
Insert 30 at end:
s20 30 temp->next = p;
p
temp
s20
Insert 30 at end:
s20 30 p ->next = s;
p
temp
s20 30
Insert 40 at end:
s20 30 40 Create node
with data 40 & Store its address in pointer p
p
s20 30
Insert 40 at end:
s20 30 40 while(temp->next! = s)
{ temp=temp->next; }p
temp
s20 30
Insert 40 at end:
s20 30 40 while(temp->next! = s)
{ temp=temp->next; }p
temp
s20 30
Insert 40 at end:
s20 30 40
ptemp temp->next = p;
s20 30
Insert 40 at end:
s20 30 40
ptemp p ->next = s;
s20 30 40
Insert 50 at Beginning:
s20 30 4050
p
Create node with data 50 and store its address in pointer p
s20 30 40
Insert 50 at Beginning:
s20 30 4050
p
while(temp->next! = s){ temp=temp->next; }
temp
s20 30 40
Insert 50 at Beginning:
s20 30 4050
p
while(temp->next! = s){ temp=temp->next; }
temp
s20 30 40
Insert 50 at Beginning:
s20 30 4050
p
while(temp->next! = s){ temp=temp->next; }
temp
s20 30 40
Insert 50 at Beginning:
s20 30 4050
p
temp->next = p;
temp
s20 30 40
Insert 50 at Beginning:
s20 30 4050
p
p->next = s;
temp
s20 30 40
Insert 50 at Beginning:
s20 30 4050
p
s = p;
temp
s20 30 40
Insert 50 at Beginning:
s20 30 4050
p
s = p;
temp
Note: This last step differentiates INSERT AT END & INSERT AT BEGINNING Which is extra in Insert at Beginning in circular linked list
Insert at END
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 beginning
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; }