You are on page 1of 3

//deleting the node from the circular linked list

#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

node* deletefirst(node *n);

void deletenode(node *n,int pos);

void display(node *n);

int main()

int n;

cout<<"enter the no of nodes "<<endl;

cin>>n;

int a[n];

cout<<"enter the data of the nodes "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

node *first;

node *temp;

node *last;

first=new node();

first->data=a[0];

first->next=NULL;

last=first;
for(int i=1;i<n;i++)

temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;

last->next=first;

display(first);

int pos;

cout<<"enter the position of the node which you want to delete "<<endl;

cin>>pos;

if(pos==1)

first=deletefirst(first);

last->next=first;

else

deletenode(first,pos);

display(first);

void display(node *n)

node *p;

p=n;

do

cout<<p->data<<" ";
p=p->next;

while(p!=n);

cout<<endl;

void deletenode(node *n,int pos)

node *p;

for(int i=0;i<pos-1;i++)

p=n;

n=n->next;

p->next=n->next;

delete n;

node* deletefirst(node *n)

node *p;

p=n;

n=n->next;

delete p;

return n;

You might also like