Lab 6

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Lab 6

Circular linked list


#include<iostream>

using namespace std;

struct CNode

{ int data;

CNode *next;

};

class Circ{

CNode *tail;

public:

Circ()

{ tail=NULL;

int count()

if(tail==NULL)

return 0;

CNode *ptr=tail->next;

int x=1;

while(ptr!=tail)

x++;

ptr=ptr->next;

}
return x;

void disp()

if(tail==NULL)

return;

else if(tail==tail->next)

cout<<"Node 1 :"<<tail->data<<endl;

else

int i=1;

CNode *ptr=tail->next;

while(ptr!=tail)

cout<<"Node "<<i<<" : "<<ptr->data<<endl;

ptr=ptr->next;

i++;

cout<<"Node "<<i<<" : "<<ptr->data<<endl;

void insertP()

//cout<<"insertion\n";

int z=count();
int x;

cout<<"Enter the position : ";

cin>>x;

if(x>(z+1))

cout<<"Node Doesn't exist.\n";

return;

cout<<"Enter the value : ";

CNode *ptr=new CNode;

cin>>ptr->data;

if(tail==NULL)

tail=ptr;

tail->next=tail;

else

CNode *ptr1=tail->next;

if(x==1)

ptr->next=tail->next;

tail->next=ptr;

// cout<<"asd";

}
else if(x==(z+1))

ptr->next=tail->next;

tail->next=ptr;

tail=ptr;

else

int i=2;

while(i<x)

ptr1=ptr1->next;

i++;

ptr->next=ptr1->next;

ptr1->next=ptr;

disp();

void del()

if(tail==NULL)

cout<<"Linked list doesn't exist.\n";


return;

int z=count();

int x;

cout<<"Enter the position to delete : ";

cin>>x;

if(x<=z)

if(tail==tail->next)

delete tail;

tail=NULL;

else

CNode *ptr=tail->next;

int i=1;

while(i<x)

ptr=ptr->next;

i++;

if(ptr->next==tail->next)

tail->next=tail->next->next;
ptr=ptr->next;

delete ptr;

else if(ptr->next==tail)

ptr->next=tail->next;

delete tail;

tail=ptr;

else

ptr->next=ptr->next->next;

ptr=ptr->next;

delete ptr;

};

int main()

Circ A;

A.insertP();

A.insertP();

A.insertP();
A.insertP();

Output

You might also like