-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_Link_List.cpp
142 lines (114 loc) · 2.15 KB
/
circular_Link_List.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next ;
node(int val)
{
this->data = val;
this->next = NULL;
}
node()
{
this->data = 0;
this->next = NULL;
}
};
class circularLinkList
{
private:
node *head;
public:
circularLinkList(node *n)
{
head = n;
head->next = head;
}
circularLinkList()
{
head = NULL;
}
void insert(int val);
void show();
bool isEmpty();
void appendNodeNextTo(int check, int val);
};
int main()
{
circularLinkList L;
int ans , check , val;
do
{
cout<<"\n\n1. insert() \n2. show()\n3. isEmpty() \n4. appendNextTo()\n0. Exit\n\nEnter your ans : ";
cin>>ans;
switch (ans)
{
case 1:
cout<<"Enter val : ";
cin>>val;
L.insert(val);
break;
case 2:
L.show();
break;
case 3:
cout<<((L.isEmpty())?"True":"False");
cout<<endl;
break;
case 4:
cout<<"Enter check and val : ";
cin>>check>>val;
L.appendNodeNextTo(check , val);
break;
default:
break;
}
} while (ans);
return 0;
}
void circularLinkList ::insert(int val)
{
node *dumi = new node(val);
if(isEmpty()){
head = dumi;
head->next = dumi ;
cout << "Insert Successful" << endl;
return;
}
node *ptr = head;
while (ptr->next != head)
ptr = ptr->next;
dumi->next = ptr->next;
ptr->next = dumi;
cout << "Insert Successful" << endl;
}
void circularLinkList :: show(){
if(isEmpty()){
cout<<"List is Empty "<<endl;
return;
}
node * ptr = head ;
do {
cout<<ptr->data<<" ";
ptr= ptr->next ;
}while (ptr != head);
cout<<endl;
}
bool circularLinkList:: isEmpty(){
if(head == NULL) return 1;
return 0;
}
void circularLinkList:: appendNodeNextTo(int check , int val){
node * ptr = head ;
while(ptr->next != head && ptr->data != check) ptr = ptr->next ;
if(ptr->data != check) {
cout << "appendNodeNext to "<< check<<" Unsuccessful" << endl;
return ;
}
node * dumi = new node(val);
dumi->next = ptr->next;
ptr->next = dumi;
cout << "appendNodeNextTo "<<check<<" Successful" << endl;
}