Вы находитесь на странице: 1из 51

Complete this activity given in below slides.

You are give 4 tasks


Insert at Start and End
Delete from start and End

Fill the blocks below for each case to complete these Four tasks.
Insert New Node At start
Note* variable name of newly created node is
newN
Single Linked List
Code:

void insertAtstart(node *newN)

if(this->start==0)

this->start=newN;
newN->next=0;

Else

newN->next=this->start;

this->start=newN;

Double Linked List

Code:

void insertAtstart(node *newN)

if(this->start==0)

this->start=newN;
newN->next=0;

newN->pre=0;

Else

This->start>pre=newN;

newN->next=this->start;

newN->pre=0;

this->start=newN;

}
Circular Single Linked List
void insertAtstart(node *newN)
{
if(this->start==0)
{
this->start=newN;
newN->next=0;
}
else
{
this->start=newN;
newN->next=this->start;
Node *t=this->start;
while(t->next!=this->start)
{
t=t->next;
}
t->next=newN;
this->start=newN;
}
Circular Double Linked List
void insertAtstart(node *newN)

if(this->start==0)

this->start=newN;

newN->next=newN;

newN->pre=newN;

else
{

this->start->pre=newN;

newN->next=this->start;

Node *t=this->start;

while(t->!=this->start)

t=t->next;

t->next=newN;

this->start->pre=t;

this->start=newN;

}
Insert New Node At End
Note* variable name of newly created node is newN
use end variable to store address of last node
in list
Single Linked List
void insertAtEnd(node *newN)

if(this->start==0)
{

this->start=newN;

newN->next=0;

else

Node *t=this->start;

while(t->next!=0)

t->next=newN;

newN->next=0;

}
Double Linked List
void insertAtEnd(node *newN)

if(this->start==0)

this->start=newN;

newN->next=0;

newN->pre=0;

else
{

Node *t=this->start;

while(t->next!=0)

t->next=newN;

newN->next=0;

this->start->pre=0;

}
Circular Single Linked List
void insertAtEnd(node *newN)

if(this->start==0)

this->start=newN;

newN->next=newN;

else

{
Node *t=this->start;

while(t->next!=this->start)

t->next=newN;

newN->next=this->start;

}
Circular Double Linked List

void insertAtEnd(node *newN)

if(this->start==0)

this->start=newN;

newN->next=newN;

newN->pre=newN;

else
{

Node *t=this->start;

while(t->next!=this->start)

t->next=newN;

newN->next=this->start;

this->start->pre=newN;

}
Delete Node From start
Single Linked List

Node deleteEnd()

if(this->start==0)

cout<<"EMpty list";

return 0;

elseif(this->start->next==0)

{
Node *t=this->start;

this->start=0;

return t;

else

Node *t=this->start;

this->start=t->next;

return t;

}
Double Linked List
Node deleteEnd()

if(this->start==0)

cout<<"EMpty list";

return 0;
}

elseif(this->start->next==0)

Node *t=this->start;

this->start=0;

this->pre=0;

return t;

else

Node *t=this->start;

this->start=t->next;

return t;

}
Circular Single Linked List
Node deleteEnd()

if(this->start==0)

cout<<"EMpty list";

return 0;

elseif(this->start->next==this->start)

{
Node *t=this->start;

this->start=0;

return t;

else

Node *t=this->start;

this->start=t->next;

t->next=this->start;

return t;

}
Circular Double Linked List
Node deleteEnd()

if(this->start==0)

cout<<"EMpty list";

return 0;

elseif(this->start->next==this->start)

{
Node *t=this->start;

this->start=0;

return t;

else

Node *t=this->start;

this->start=t->next;

t->next=this->start;

this->start->pre=t;

return t;

}
Delete Node From End
Note* use sEnd variable to store address of second
last node in list
Single Linked List
Node deleteEnd()

if(this->start==0)

cout<<"EMpty list";
return 0;

elseif(this->start->==0)

Node *t=this->start;

this->start=0;

return t;

else

Node *t=this->strat;

while(t->next->next!=0)

t=t->next;

t->next=0;

}
Double Linked List
Node deleteEnd()

if(this->start==0)

cout<<"EMpty list";

return 0;

elseif(this->start->next==0)

Node *t=this->start;
this->start=0;

this->pre=0;

return t;

else

Node *t=this->start;

while(t->next->next=0)

t=t->next;

t->next=0;

this->start->pre=0;

return t;

}
Circular Single Linked List
Node deleteEnd()

if(this->start==0)

cout<<"EMpty list";

return 0;

elseif(this->start->next==this->start)

Node *t=this->start;
this->start=0;

return t;

else

Node *t=this->strat;

while(t->next->next!=this->start)

t=t->next;

t->next=this->start;

}
Circular Double Linked List

Node deleteEnd()

if(this->start==0)

cout<<"EMpty list";

return 0;

elseif(this->start->next==this->start)

{
Node *t=this->start;

this->start=0;

return t;

else

Node *t=this->start->pre->pre;

t=t->next;

t->next=this->start;

this->start->pre=t;

return t;