-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle_list.cpp
More file actions
218 lines (195 loc) · 4.41 KB
/
circle_list.cpp
File metadata and controls
218 lines (195 loc) · 4.41 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
206
207
208
209
210
211
212
213
214
215
216
217
218
#include<iostream>
class node{
public:
node *next;
int val;
node(int val);
};
node::node(int val)
{
this->val = val;
next = NULL;
}
class List{
node *current, *head, *temp;
int length;
public:
void append(int val);
List();
void print();
void insert(int val, int pos);
node* get_last();
void del(int pos);
void free(node* &f);
};
List::List()
{
head = current = temp = NULL;
length = 0;
}
void List::free(node* &f)
{
f->next = NULL;
delete f;
f = NULL;
}
node* List::get_last()
{
current = head;
node* last = head;
for(int i = 1 ; i < length; i++)
{
last = last->next;
}
return last;
}
void List::del(int pos)
{
int val;
if(pos < 1 )
throw "Position out of range. Select Valid position";
if(head->next == head)
{
// for one node
free(head);
std::cout << "This is the value of head which is freed : " << head << std::endl;
return;
}
if(pos == 1)
{
node* last = get_last();
current = head;
node* del = current;
head = head->next;
val = del->val;
last->next = head;
free(del);
length--;
std::cout << val << " at Position " << pos << " is deleted Successfully" << std::endl;
return;
}
// 1,2,3,4
// for middle cases
current = head;
node* del = current;
for(int i = 1 ; i < pos-1; i++)
{
current = current->next;
del = del->next;
}
del = del->next;
current->next = current->next->next;
val = del->val;
if(del == head)
{
head = head->next;
}
free(del);
length--;
std::cout << val << " at Position " << pos << " is deleted Successfully" << std::endl;
}
void List::insert(int val, int pos)
{
if(pos < 1)
throw "Position starts from 1. Select Valid position";
current = head;
if(head == NULL)
{
head = new node(val);
head->next = head;
current = head;
length++;
std::cout <<"The value " << val << " at Position " << pos << " is inserted Successfully" << std::endl;
return;
}
if(pos == (length + 1))
{ // this is the last position
node* lst = get_last();
node* last = new node(val);
lst->next = last;
last->next = head;
length++;
std::cout <<"The value " << val << " at Position " << pos << " is inserted Successfully" << std::endl;
return;
}
if(pos == 1)
{
// inserting at ist position
node* last = get_last();
node* ist = new node(val);
ist->next = head;
head = ist;
last->next = head;
length++;
std::cout <<"The value " << val << " at Position " << pos << " is inserted Successfully" << std::endl;
return;
}
// for middle case
current = head;
for(int i = 1; i < (pos - 1) ; i++)
{
current = current->next;
}
node* mid = new node(val);
mid->next = current->next;
current->next = mid;
length++;
std::cout <<"The value " << val << " at Position " << pos << " is inserted Successfully" << std::endl;
}
void List::print()
{
if(head->next == head)
{ // for one node
std::cout << "[ " << head->val << " ]" << std::endl;
return;
}
current = head;
std::cout << "[ ";
std::cout << current->val << ", ";
current = current->next;
while(current != head)
{
std::cout << current->val ;
if(current->next == head) // it will look like python list
std::cout << " ]";
else
std::cout << ", ";
current = current->next;
}
std::cout << std::endl;
}
void List::append(int val)
{
if(head == NULL)
{
head = new node(val);
head->next = head;
length++;
current = head;
return;
}
node* n = new node(val);
length++;
current->next = n;
current = current->next;
current->next = head;
}
int main(){
List l;
try{
for(int i = 1; i <= 10; i++)
{
if(i == 5)
continue;
l.append(i);
}
l.print();
l.insert(5,5);
l.print();
l.del(5);
l.print();
}catch(const char* msg){
std::cout<<std::endl << msg << std::endl;
}
return 0;
}