Skip to content

Commit 5e7b83f

Browse files
authored
Merge pull request #125 from sanujsood/master
CIRCULAR_LINKED_LIST_C++
2 parents 87fb84b + caaa996 commit 5e7b83f

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include<iostream>
2+
using namespace std;
3+
int size;
4+
5+
struct Node
6+
{
7+
int data;
8+
struct Node *next;
9+
}*first=NULL,*last=NULL;
10+
void create()
11+
{
12+
struct Node *t;
13+
first=new Node;
14+
last =new Node;
15+
cout<<"enter size of linked list"<<endl;
16+
cin>>size;
17+
cout<<"enter elements"<<endl;
18+
cin>>first->data;
19+
first->next=first;
20+
last=first;
21+
22+
23+
24+
for(int i=2;i<=size;i++)
25+
{ t=new Node;
26+
cin>>t->data;
27+
t->next=last->next;
28+
last->next=t;
29+
last=t;
30+
}
31+
32+
33+
}
34+
void Display(struct Node *p)
35+
{
36+
do
37+
{
38+
cout<<p->data<<" "<<endl;
39+
p=p->next;
40+
}while(p!=first);
41+
}
42+
void insert(struct Node *p,int pos,int x)
43+
{
44+
Node *t;
45+
if(pos==1)
46+
{
47+
t=new Node;
48+
t->data=x;
49+
if(first==NULL)
50+
{
51+
first=t;
52+
first->next=first;
53+
}
54+
else
55+
{
56+
p=first;
57+
while(p->next!=first)
58+
p=p->next;
59+
p->next=t;
60+
t->next=first;
61+
first=t;
62+
}
63+
}
64+
else
65+
{
66+
p=first;
67+
for(int i=1;i<pos-1;i++)
68+
p=p->next;
69+
t=new Node;
70+
t->data=x;
71+
t->next=p->next;
72+
p->next=t;
73+
74+
}
75+
}
76+
int search(struct Node *p,int el)
77+
{ int flag=0;
78+
do
79+
{
80+
if(p->data==el)
81+
{
82+
flag=1;
83+
return flag;
84+
break;
85+
}
86+
else
87+
flag=0;
88+
p=p->next;
89+
}while(p!=first);
90+
return flag;
91+
}
92+
int del(struct Node *p,int pos)
93+
{ struct Node *q;
94+
int x=-1;
95+
if(pos==1)
96+
{
97+
p=first;
98+
while(p->next!=first)
99+
p=p->next;
100+
x=first->data;
101+
if(p==first)
102+
{
103+
delete first;
104+
first=NULL;
105+
}
106+
else
107+
{
108+
p->next=first->next;
109+
q=first;
110+
first=first->next;
111+
delete q;
112+
113+
}
114+
115+
}
116+
else
117+
{
118+
p=first;
119+
for(int i=1;i<pos-1;i++)
120+
p=p->next;
121+
q=p->next;
122+
x=q->data;
123+
delete q;
124+
}
125+
return x;
126+
}
127+
int main()
128+
{ int choice;
129+
cout<<"......MENU......."<<endl;
130+
cout<<"1)CREATE"<<endl;
131+
cout<<"2)DISPLAY"<<endl;
132+
cout<<"3)INSERT"<<endl;
133+
cout<<"4)SEARCH"<<endl;
134+
cout<<"5)DELETE"<<endl;
135+
do
136+
{
137+
138+
cout<<"enter choice";
139+
cin>>choice;
140+
switch(choice)
141+
{
142+
case 1: create();break;
143+
case 2: Display(first);break;
144+
case 3: int el,pos;
145+
cout<<"enter position and element"<<endl;
146+
cin>>pos>>el;
147+
insert(first,pos,el);break;
148+
case 4: int searchel;
149+
cout<<"enter element to be searched:";
150+
cin>>searchel;
151+
int z;
152+
z=search(first,searchel);
153+
if(z==1)
154+
cout<<"search successful"<<endl;
155+
else
156+
cout<<"unsuccessful"<<endl;break;
157+
case 5: int pos1;
158+
cout<<"enter position of node you want to delete:";
159+
cin>>pos1;
160+
int y;
161+
y=del(first,pos1);
162+
cout<<y<<" is deleted "<<endl;break;
163+
164+
}
165+
}while(choice>0);
166+
return 0;
167+
}

0 commit comments

Comments
 (0)