-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
216 lines (186 loc) · 2.94 KB
/
linked_list.cpp
File metadata and controls
216 lines (186 loc) · 2.94 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
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
void push_front(Node** head, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = *head;
*head = new_node;
}
void push_back(Node** head, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = NULL;
Node* tmp = *head;
while (tmp->next != NULL){
tmp = tmp->next;
}
tmp->next = new_node;
}
void AddAfter(Node* prev_node, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void AddBefore(Node** head, Node* next_node, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
if (next_node == NULL)
{
cout << "The given next node can not be NULL" << endl;
return;
}
if (next_node == *head)
{
push_front(head, new_data);
return;
}
Node *tmp = *head;
while (tmp->next != next_node)
{
tmp = tmp->next;
}
new_node->next = next_node;
tmp->next = new_node;
}
int pop_front(Node** head)
{
Node* tmp = *head;
if (tmp==NULL)
{
cout << "List is empty!" << endl;
return -1;
}
else
{
int value = tmp->data;
*head = tmp->next;
return value;
}
}
int top_front(Node **head)
{
Node* tmp = *head;
if (tmp==NULL)
{
cout << "List is empty !" << endl;
return -1;
}
else
{
return tmp->data;
}
}
int pop_back(Node** head)
{
Node* tmp = *head;
if (tmp == NULL)
{
cout << "List is empty!" << endl;
return -1;
}
if (tmp->next == NULL)
{
*head = NULL;
return tmp->data;
}
else
{
while (tmp->next->next != NULL)
{
tmp = tmp->next;
}
int value = tmp->next->data;
tmp->next = NULL;
return value;
}
}
int top_back(Node** head)
{
Node* tmp = *head;
if (tmp == NULL)
{
cout << "List is empty!" << endl;
return -1;
}
if (tmp->next == NULL)
{
return tmp->data;
}
else
{
while (tmp->next != NULL)
{
tmp = tmp->next;
}
return tmp->data;
}
}
void print_list(Node* head)
{
Node* ptr = head;
if (ptr==NULL)
{
cout << "List is empty!" << endl;
return;
}
while (ptr!=NULL)
{
cout << ptr->data << " ";
ptr = ptr->next;
}
cout << endl;
}
void delete_list(Node** head)
{
Node* current = *head;
Node* next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
*head = NULL;
}
int main()
{
Node* head = NULL;
push_front(&head, 5);
push_front(&head, 6);
push_front(&head, 7);
print_list(head);
int x = pop_front(&head);
cout << x << endl;
print_list(head);
push_back(&head, 8);
print_list(head);
int y = pop_back(&head);
cout << y << endl;
print_list(head);
Node *tmp1 = head;
AddAfter(tmp1, 9);
print_list(head);
Node*tmp2 = head;
AddBefore(&head, tmp2, 10);
print_list(head);
int a = top_front(&head);
cout << a << endl;
print_list(head);
int b = top_back(&head);
cout << b << endl;
print_list(head);
delete_list(&head);
print_list(head);
return 0;
}