circular linked list

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

Upload: shubhamsidana

Post on 08-Dec-2015

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Circular Linked List

INSERTION AT END & BEGINING

IN CIRCULAR LINKED LIST

BY Mr. Shubham SidanaAssistant ProfessorABES Engineering CollegeDr. A.P.J. Abdul Kalam Technical University

Page 2: Circular Linked List

Initially, NULLs

Page 3: Circular Linked List

Initially, NULLs

Insert 20 at end: //Since s is NULL, so it is first insertion

NULLs

Create node

Page 4: Circular Linked List

Initially, NULLs

Insert 20 at end: //Since s is NULL, so it is first insertion

NULLs

20 Insert 20 in data part

Page 5: Circular Linked List

Initially, NULLs

Insert 20 at end: //Since s is NULL, so it is first insertion

NULLs

20 Store its address in pointer pp

Page 6: Circular Linked List

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

Page 7: Circular Linked List

Initially, NULLs

Insert 20 at end: //Since s is NULL, so it is first insertion

s20 Make circular

link

// s->next=s;

Page 8: Circular Linked List

s20

Insert 30 at end:

s20 30 Create node

with data 30 & Store its address in pointer p

p

Page 9: Circular Linked List

s20

Insert 30 at end:

s20 30 while(temp->next! = s)

{ temp=temp->next; }

p

temp

Page 10: Circular Linked List

s20

Insert 30 at end:

s20 30 temp->next = p;

p

temp

Page 11: Circular Linked List

s20

Insert 30 at end:

s20 30 p ->next = s;

p

temp

Page 12: Circular Linked List

s20 30

Insert 40 at end:

s20 30 40 Create node

with data 40 & Store its address in pointer p

p

Page 13: Circular Linked List

s20 30

Insert 40 at end:

s20 30 40 while(temp->next! = s)

{ temp=temp->next; }p

temp

Page 14: Circular Linked List

s20 30

Insert 40 at end:

s20 30 40 while(temp->next! = s)

{ temp=temp->next; }p

temp

Page 15: Circular Linked List

s20 30

Insert 40 at end:

s20 30 40

ptemp temp->next = p;

Page 16: Circular Linked List

s20 30

Insert 40 at end:

s20 30 40

ptemp p ->next = s;

Page 17: Circular Linked List

s20 30 40

Insert 50 at Beginning:

s20 30 4050

p

Create node with data 50 and store its address in pointer p

Page 18: Circular Linked List

s20 30 40

Insert 50 at Beginning:

s20 30 4050

p

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

temp

Page 19: Circular Linked List

s20 30 40

Insert 50 at Beginning:

s20 30 4050

p

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

temp

Page 20: Circular Linked List

s20 30 40

Insert 50 at Beginning:

s20 30 4050

p

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

temp

Page 21: Circular Linked List

s20 30 40

Insert 50 at Beginning:

s20 30 4050

p

temp->next = p;

temp

Page 22: Circular Linked List

s20 30 40

Insert 50 at Beginning:

s20 30 4050

p

p->next = s;

temp

Page 23: Circular Linked List

s20 30 40

Insert 50 at Beginning:

s20 30 4050

p

s = p;

temp

Page 24: Circular Linked List

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

Page 25: 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;

}

Page 26: Circular Linked List

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; }