Circulary Linked List

[wpdm_package id=’1410′]

Inserting element at end:

[sourcecode lang=”cpp”]
void insertAtEnd(int d)
{ node *current=head;
node *temp=new node(d);
if(head==NULL)
{ head=temp;
temp->next=temp;
}
else
{ for(;current->next!=head;current=current->next);
temp->next=head;
current->next=temp;
}
}
[/sourcecode]


Inserting element at front:

[sourcecode lang=”cpp”]
void insertAtBegin(int d)
{ node *current=head;
node *temp=new node(d);
if(head==NULL)
{ head=temp;
temp->next=temp;
}
else
{ for(;current->next!=head;current=current->next);
temp->next=head;
current->next=temp;
head=temp;
}
}
[/sourcecode]

Deleting Last node:

[sourcecode lang=”cpp”]
void DeleteLastNode()
{ node *temp=head, *current=head;
if(head==NULL)
{ cout<<"List Empty"; return; } while(current->next!=head)
{ temp=current;
current=current->next;
}
temp->next=current->next;
free(current);
}
[/sourcecode]

Deleting first node:

[sourcecode lang=”cpp”]
void DeleteFrontNode()
{ node *temp=head, *current=head;
if(head==NULL)
{ cout<<"List Empty"; return; } while(current->next!=head)
current=current->next;
current->next=head->next;
head=head->next;
free(temp);
}
[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *