0% found this document useful (0 votes)
4 views5 pages

Circular Linked List Implementation

The document contains C++ code for a circular linked list implementation, including methods for inserting and deleting nodes at various positions. It defines a 'node' structure and a 'circularlinklist' class with functions to insert nodes at the beginning, end, and specific positions, as well as to delete nodes from the beginning, end, and specific positions. The main function demonstrates the usage of these methods by creating a circular linked list and performing several operations on it.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Circular Linked List Implementation

The document contains C++ code for a circular linked list implementation, including methods for inserting and deleting nodes at various positions. It defines a 'node' structure and a 'circularlinklist' class with functions to insert nodes at the beginning, end, and specific positions, as well as to delete nodes from the beginning, end, and specific positions. The main function demonstrates the usage of these methods by creating a circular linked list and performing several operations on it.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<iostream>

using namespace std;

struct node
{
/* data */
int data;
node* next;
};
class circularlinklist{
node *first;
public:
circularlinklist(){
first=NULL;
}
void insertnode(int x){
node *temp=new node;
temp->data=x;
temp->next=NULL;
if(first==NULL){
first=temp;
temp->next=first;
}
else{
node *p=first;
while (p->next!=first)
{
p=p->next;
}
p->next=temp;
temp->next=first;
}

void insertatbegin(int x){


node *temp=new node;
temp->data=x;
temp->next=NULL;
if(first==NULL){
first=temp;
temp->next=first;
}
else{
node *p=first;
while (p->next!=first)
{
p=p->next;
}
p->next=temp;
temp->next=first;
first=temp;
}
}
void insertatend(int x){
node *temp=new node;
temp->data=x;
temp->next=NULL;
if(first==NULL){
first=temp;
temp->next=first;
}
else{
node *p=first;
while (p->next!=first)
{
p=p->next;
}
p->next=temp;
temp->next=first;
}
}

void insertatposition(int x,int pos){


node *temp=new node;
temp->data=x;
temp->next=NULL;
if(pos==1){
if(first==NULL){
first=temp;
temp->next=first;
}
else{
node *p=first;
while (p->next!=first)
{
p=p->next;
}
p->next=temp;
temp->next=first;
first=temp;
}
}
else{
node *p=first;
for (int i = 1; i < pos-1 && p->next!=first; i++)
{
p=p->next;
}
if(p->next==first){
cout<<"Position out of bounds"<<endl;
return;
}
temp->next=p->next;
p->next=temp;
}
}

void deletefrombegin(){
if(first==NULL){
cout<<"list is empty"<<endl;
return;
}
if(first->next==first){
delete first;
first=NULL;
return;
}
node *p=first;
while (p->next!=first)
{
p=p->next;
}
node *q=first;
first=first->next;
p->next=first;
delete q;
}

void deletefromend(){
if(first==NULL){
cout<<"list is empty"<<endl;
return;
}
if(first->next==first){
delete first;
first=NULL;
return;
}
node *p=first;
node *q=NULL;
while(p->next!=first){
q=p;
p=p->next;

}
q->next=first;
delete p;

}
void deletefromposition(int pos){
if (first==NULL)
{
cout<<"list is empty"<<endl;
return;
}
if(pos==1){
if(first->next==first){
delete first;
first=NULL;
return;
}

}
else{
node *p=first;
node *q=NULL;
for (int i = 1; i < pos && p->next!=first; i++)
{
q=p;
p=p->next;
}
if(p->next==first && pos!=1){
cout<<"Position out of bounds"<<endl;
return;
}
q->next=p->next;
delete p;
}
}
void display(){
if(first==NULL){
cout<<"list is empty"<<endl;
return;
}
node *p=first;
do{
cout<<p->data<<" ";
p=p->next;
}while(p!=first);
cout<<endl;
}

};
int main(){
circularlinklist cl;
[Link](10);
[Link](20);
[Link](30);
[Link](15,2);
[Link](5);
[Link](40);
[Link]();
[Link](3);
[Link]();
[Link]();
// [Link]();
return 0;
}

You might also like