-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS11_CircularLinkedList.cpp
175 lines (157 loc) · 4.37 KB
/
DS11_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
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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int CurrData){
this->data=CurrData;
this->next=NULL;
}
};
static int size_CLL;
class CircularLL{
private:
public:
Node* Head;
Node* Tail;
CircularLL(){
this->Head=NULL;
this->Tail=NULL;
}
void AddStart(int data){
Node* NewNode=new Node(data);
if(Head==NULL){
Head=Tail=NewNode;
return;
}
NewNode->next=Head;
Head=NewNode;
Tail->next=Head;
size_CLL++;
}
void AddLast(int data){
Node* NewNode=new Node(data);
if(Head==NULL){
Head=Tail=NewNode;
return;
}
Tail->next=NewNode;
Tail=NewNode;
Tail->next=Head;
size_CLL++;
}
Node* GetMiddle(){
Node* Slow=Head->next;
Node* Fast=Head->next->next;
while(Fast!=Tail && Fast->next!=Tail){
Slow=Slow->next;
Fast=Fast->next->next;
}
return Slow;
}
void AddMiddle(int data){
Node* NewNode=new Node(data);
if(Head==NULL){
Head=Tail=NewNode;
return;
}
Node* Slow=GetMiddle();
NewNode->next=Slow->next;
Slow->next=NewNode;
}
int RemoveMiddle(){
if(Head==NULL){
cout<<"CIRCULAR LINKED LIST IS EMPTY , DELETION NOT POSSIBLE"<<endl;
return (-1);
}
Node* MidNode=GetMiddle();
Node* Prev=Head;
while(Prev->next!=MidNode){
Prev=Prev->next;
}
int Value=MidNode->data;
Prev->next=MidNode->next;
MidNode->next=NULL;
free(MidNode);
return Value;
}
int DeleteStart(){
if(Head==NULL){
cout<<"CIRCULAR LINKED LIST IS EMPTY , DELETION NOT POSSIBLE"<<endl;
return(-1);
}
Node*temp=Head;
int Value=temp->data;
Head=Head->next;
Tail->next=Head;
temp->next=NULL;
free(temp);
return Value;
}
int Delete_End(){
if(Head==NULL){
cout<<"CIRCULAR LINKED LIST IS EMPTY , DELETION NOT POSSIBLE"<<endl;
return(-1);
}
Node*temp=Head;
while(temp->next!=Tail){
temp=temp->next;
}
Node* ptr=temp->next;
int Value=ptr->data;
temp->next=Head;
Tail=temp;
Tail->next=Head;
ptr->next=NULL;
free(ptr);
return Value;
}
void Reverse_CLL(){
if(Head==NULL){
cout<<"CIRCULAR LINKED LIST IS EMPTY , REVERSAL NOT POSSIBLE"<<endl;
}
Node* Prev=Tail;
Node* Temp=Head;
Node* Ahead=NULL;
while(Temp!=Tail){
Ahead=Temp->next;
Temp->next=Prev;
Prev=Temp;
Temp=Ahead;
}
Temp->next=Prev;
Tail=Head;
Head=Temp;
}
void Print_CLL(){
cout<<"THE CIRCULAR LINKED LIST IS ::: ";
Node*temp=Head;
while(temp!=Tail){
cout<<temp->data<<" --> ";
temp=temp->next;
}
cout<<temp->data<<endl;
}
};
int main(){
CircularLL cll;
// cll.AddStart(2);
// cll.AddStart(1);
// cll.AddLast(5);
// cll.AddLast(6);
// cll.AddMiddle(3);
// cll.AddMiddle(4);
// cll.Print_CLL();
// Node* temp=cll.GetMiddle();
// cout<<"MIDDLE NODE OF Circular LL IS ::: "<<temp->data<<endl;
// cll.DeleteStart();
// cll.Print_CLL();
// cll.Delete_End();
// cll.Print_CLL();
// cll.RemoveMiddle();
// cll.Print_CLL();
// cll.Reverse_CLL();
// cll.Print_CLL();
return 0;
}