-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq2_display_head_twice.cpp
More file actions
205 lines (197 loc) · 4.68 KB
/
q2_display_head_twice.cpp
File metadata and controls
205 lines (197 loc) · 4.68 KB
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
#include<iostream>
using namespace std;
//Definition of Circular Linked List, you can see main function for the solution
class Node
{
public:
int val;
Node *next;
Node()
{
val=0;
next=NULL;
}
Node(int val)
{
this->val=val;
next=NULL;
}
};
class CircularLinkedList
{
public:
Node *start;
int size;
CircularLinkedList()
{
start=NULL;
size=0;
}
void insertAtBeginning(int data)
{
Node *nnode=new Node(data);
if(start==NULL)
{
start=nnode;
nnode->next=start;
}
else
{
nnode->next=start;
Node *temp=start;
while(temp->next!=start)
{
temp=temp->next;
}
temp->next=nnode;
start=nnode;
cout<<data<<" inserted Successfully at Beginning"<<endl;
}
size++;
}
void insertAtEnd(int data)
{
Node *nnode=new Node(data);
if(start==NULL)
{
start=nnode;
nnode->next=start;
}
else
{
Node *temp=start;
while(temp->next!=start)
{
temp=temp->next;
}
temp->next=nnode;
nnode->next=start;
cout<<data<<" inserted Successfully at End"<<endl;
}
size++;
}
void insertInBetween(int data, int pos)
{
//Insert the data before the pos value
Node *temp=start;
while(temp->next->val!=pos)
{
temp=temp->next;
}
Node *nnode=new Node(data);
Node *t=temp->next;
temp->next=nnode;
nnode->next=t;
cout<<data<<" inserted before "<<pos<<endl;
size++;
}
void deleteNode(int data)
{
//Delete the node with data, it may be in the beginning or end or in between
//We will manage this using edge cases
if(start==NULL)
{
cout<<"List is Empty!!!"<<endl;
return;
}
//Delete from beginning
if(start->val==data)
{
Node *temp=start;
//temp will be used to assign the last pointer to start
Node *t=start;
//It will be used to delete t
start=start->next;
temp=temp->next;
while(temp->next!=t)
{
temp=temp->next;
}
temp->next=start;
cout<<data<<" deleted successfully from the list"<<endl;
delete(t);
return;
}
//Delete from Middle
Node *prev=start;
Node *temp=start->next;
while(temp->next!=start)
{
if(temp->val==data)
{
prev->next=temp->next;
delete(temp);
cout<<data<<" deleted successfully from the list"<<endl;
return;
}
prev=temp;
temp=temp->next;
}
if(temp->val==data)
{
prev->next=start;
delete(temp);
cout<<data<<" deleted successfully from the list"<<endl;
return;
}
cout<<data<<" Not Found!!!"<<endl;
}
void search(int data)
{
if(start==NULL)
{
cout<<"List is Empty!!!"<<endl;
return;
}
Node *temp=start;
do
{
if(temp->val==data)
{
cout<<data<<" found in the list"<<endl;
return;
}
temp=temp->next;
}while(temp!=start);
cout<<data<<" Not Found!!!"<<endl;
}
void display()
{
if(start==NULL)
{
cout<<"List is Empty!!!"<<endl;
return;
}
Node *temp=start;
cout<<"-------------Printing Circular Linked List---------------"<<endl;
cout<<"start-->";
do
{
cout<<temp->val<<"-->";
temp=temp->next;
} while (temp!=start);
cout<<"start"<<endl;
}
};
int main()
{
//Creating a Circular Linked List as given in the question
CircularLinkedList cll;
cll.insertAtEnd(20);
cll.insertAtEnd(100);
cll.insertAtEnd(40);
cll.insertAtEnd(80);
cll.insertAtEnd(60);
//Regular Printing
cll.display();
//Print Head Twice
Node *temp=cll.start;
cout<<temp->val<<" ";
temp=temp->next;
do
{
cout<<temp->val<<" ";
temp=temp->next;
} while (temp!=cll.start->next);
//Or we can directly display the start in our old display function to achieve the same output
}