-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularll.cpp
231 lines (226 loc) · 3.9 KB
/
circularll.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* This file was first created on 15th September,2018 by Keshav Tangri
This is for educational purposes only.
This is for B.Tech Students who are or will be studying Data Structures in there curriculum
For queries mail me @ : [email protected]
*/
#include <iostream>
#include<stdlib.h>
using namespace std;
class Node{
public:
Node* next;
int data;
};
class cll{
public:
Node* head = new Node;
cll(){
head=NULL;
}
Node* createnode(int val);
void display();
void addeleatbeg(int val);
void addeleatlast(int val);
void deleleatbeg();
void deleleatlast();
void divideintotwo();
void exchangefirstandlastnode();
};
int main()
{
int value;
int i;
cll obj;
while(1){
cout<<"\n\n1.ADD ELEMENT AT BEGINNING\n2.ADD ELEMENT AT THE LAST\n3.DISPLAY LIST\n4.DELETE AT BEG\n5.DELETE AT LAST\n6.DIVIDE IN TWO\n7.EXCHANGE FIRST AND LAST NODE\n8.EXIT\nChoose your Option : ";
cin>>i;
switch(i){
case 1:
cout<<"\n\nEnter value you want to add : ";
cin>>value;
obj.addeleatbeg(value);
break;
case 2:
cout<<"\n\nEnter value you want to add : ";
cin>>value;
obj.addeleatlast(value);
break;
case 3:
obj.display();
break;
case 4:
obj.deleleatbeg();
break;
case 5:
obj.deleleatlast();
break;
case 6:
obj.divideintotwo();
break;
case 7:
obj.exchangefirstandlastnode();
break;
case 8:
exit(0);
break;
}
}
return 0;
}
//createnode function
Node* cll::createnode(int val){
Node* temp = new Node;
temp->data = val;
return temp;
}
//display function
void cll::display(){
if(head==NULL){
cout<<"\n\nBLANK LIST !\n\n";
return;
}else{
Node* ptr=new Node;
ptr = head;
do{
cout<<ptr->data<<" ";
ptr=ptr->next;
}while(ptr!=head);
}
}
//element at the beginning
void cll::addeleatbeg(int val){
Node* temp = createnode(val);
if(head==NULL){
head=temp;
temp->next = head;
cout<<"\n\nELEMENT ADDED !\n\n";
}else{
Node* ptr = new Node;
ptr = head;
while(ptr->next!=head){
ptr=ptr->next;
}
temp->next=head;
ptr->next = temp;
head = temp;
cout<<"\n\nELEMENT ADDED !\n\n";
}
}
//add element at last
void cll::addeleatlast(int val){
if(head==NULL){
addeleatbeg(val);
}else{
Node* temp = createnode(val);
Node* ptr = new Node;
ptr=head;
while(ptr->next!=head){
ptr=ptr->next;
}
ptr->next = temp;
temp->next = head;
cout<<"\n\nELEMENT ADDED !\n\n";
}
}
//delete element at beg
void cll::deleleatbeg(){
if(head==NULL){
cout<<"\n\nUNDERFLOW !\n\n";
return;
}else if(head->next==head){
head = NULL;
cout<<"\n\nELEMENT AT BEGINNING DELETED !\n\n";
return;
}
Node * ptr = new Node;
Node * preptr = new Node;
preptr=head;
do{
preptr=preptr->next;
}while(preptr->next!=head);
ptr=head;
head = ptr->next;
preptr->next = head;
delete ptr;
cout<<"\n\nELEMENT AT BEGINNING DELETED !\n\n";
}
//delete element at last
void cll::deleleatlast(){
if(head==NULL){
cout<<"\n\nUNDERFLOW !\n\n";
return;
}else if(head->next==head){
head = NULL;
cout<<"\n\nELEMENT AT LAST DELETED !\n\n";
return;
}
Node * preptr = new Node;
Node * ptr =new Node;
ptr=head;
do{
preptr =ptr;
ptr=ptr->next;
}while(ptr->next!=head);
preptr->next =head;
delete ptr;
cout<<"\n\nELEMENT AT LAST DELETED !\n\n";
}
//Divide list into 2 lists
void cll::divideintotwo(){
if(head==NULL){
cout<<"\n\nBLANK LIST!\n\n";
return;
}
Node* ptr = new Node;
ptr=head;
int coun = 1,h1,h2;
do{
coun++;
ptr=ptr->next;
}while(ptr->next!=head);
ptr=head;
if(coun%2==0){
h1=coun/2;
h2=h1;
}else{
h1 = coun/2+1;
h2 = coun/2;
}
cll ob1,ob2;
for(int i=1;i<=coun;i++){
if(i<=h1){
ob1.addeleatlast(ptr->data);
ptr=ptr->next;
}else{
ob2.addeleatlast(ptr->data);
ptr=ptr->next;
}
}
cout<<"\nFIRST HALF : ";
ob1.display();
cout<<"\n\n";
cout<<"\nSECOND HALF : ";
ob2.display();
cout<<"\n\n";
}
//exchange first and last node
void cll::exchangefirstandlastnode(){
if(head==NULL){
cout<<"\n\nBLANK LIST !\n\n";
return;
}
Node* prev=new Node;
Node* lastp = new Node;
Node* firstp = new Node;
firstp= head;
lastp=head;
while(lastp->next!=head){
prev=lastp;
lastp=lastp->next;
}
head = lastp;
lastp->next=firstp->next;
prev->next=firstp;
firstp->next = head;
cout<<"\n\nELEMENTS SWAPPED/EXCHANGED\n\n";
}