#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;
}