forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularLinkedList.cpp
102 lines (93 loc) · 1.76 KB
/
CircularLinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
};
class circlist
{
node *top = NULL,*ptr,*rear = NULL,*end = NULL;
int x,count;
public:
void insert();
void del();
void search();
void display();
};
void circlist::insert()
{
char ch = 'y';
while(ch == 'y' || ch == 'Y')
{
cout<<"\nEnter element : ";
cin>>x;
ptr = new node;
ptr->info = x;
if(top == NULL)
{
top = ptr;
ptr->link = top;
rear = ptr;
}
else
{
rear->link = ptr;
ptr->link = top;
rear = ptr;
}
cout<<"\nDo you want to enter more?(y/n)...";
cin>>ch;
}
}
void circlist::display()
{
node *tp = top;
while(tp->link != top)
{
cout<<tp->info<<"\n";
tp = tp->link;
}
cout<<tp->info;
tp = tp->link;
cout<<"\nInitial element-->"<<tp->info<<"-->";
}
void circlist::del()
{
node *a;
char ch = 'y';
while(ch == 'y' || ch == 'Y')
{
a = top;
top = top->link;
rear->link = top;
cout<<a->info<<" deleted succesfully!";
delete(a);
cout<<"\nDo you want to delete more?(y/n)...";
cin>>ch;
}
}
int main()
{
circlist o;
int choice;
while(1)
{
cout<<"\nMENU\n1.Insertion\n2.Display\n3.Deletion\n4.Exit";
cout<<"\nEnter choice : ";
cin>>choice;
switch (choice)
{
case 1: o.insert();
break;
case 2: o.display();
break;
case 3: o.del();
break;
case 4: exit(0);
default: cout<<"\nInvalid choice : ";
break;
}
}
return 0;
}