-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList_Without_Head.cpp
196 lines (157 loc) · 3.88 KB
/
LinkedList_Without_Head.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
//Justyna Gruba 3 Nov 2020
/* Implementatnion Linked List without head */
#include <iostream>
struct element{
int i;
element* next = NULL;
};
void display(element *List) {
if(List == NULL)
return;
element *first = List;
while(first !=NULL){
std::cout << first->i << " ";
first = first->next;
}
}
element * create(){
return NULL;
}
element* find(element* List, int a){
element *tmp = List;
while(tmp != NULL){
if(tmp->i == a){
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
element* add_last(element* List, int a){
if(List == NULL){
List->i = a;
return List;
}
element *tmp = List;
while(tmp->next != NULL){
tmp = tmp->next;
}
element *newElem = new element;
newElem->i = a;
tmp->next = newElem;
return List;
}
element* add_first(element* List, int a){
element *newElement = new element;
newElement->i = a;
newElement->next = List;
List = newElement;
return List;
}
element * add_between(element* List, element *x, int a){
element* newElement = new element;
newElement->i = a;
element *tmp = List;
if(tmp == x){
newElement->next = List;
List = newElement;
return List;
}
while(tmp->next != NULL){
if(tmp->next->i == x->i){
newElement->next = tmp->next;
tmp->next = newElement;
return List;
}
tmp = tmp->next;
}
return List;
}
element* remove_all(element *List){
element *tmp = List;
while(List != NULL){
List = List-> next;
std::cout << "usuwane elementy =" << tmp ->i << " " << std::endl;
delete tmp;
tmp = List;
}
return List;
}
element * remove_integer(element *List, int a){
if(List == NULL)
return NULL;
element *tmp = List;
if(List->i == a){
List= List->next;
delete tmp;
return List;
}
else{
while(tmp->next != NULL){
if(tmp->next->i == a){
std:: cout << "tmp next wskazuje " << tmp->next->i << std:: endl;
element * del = tmp->next;
tmp->next = tmp->next->next;
delete del;
return List;
}
tmp = tmp->next;
}
return List;
}
}
element* remove_element(element* List, element* elem){
element* tmp = List;
if(List->i == elem ->i){
List = List->next;
delete tmp;
if(List == NULL){
return NULL;
}
return List;
}
while(tmp->next != NULL){
if(tmp->next->i == elem->i){
tmp->next = elem->next;
std::cout << "usuniety element to " << elem->i << std::endl;
delete elem;
break;
}
tmp = tmp->next;
}
if(tmp == NULL){
return NULL;
}
return List;
}
element* remove_next_element( element* List, element * elem){
element* tmp = List;
while(tmp -> next != NULL){
if(tmp->next->i == elem->next ->i) {
element *del = elem->next;
tmp->next = elem->next->next;
delete del;
break;
}
tmp = tmp->next;
}
return List;
}
int main() {
element *first= create();
first = add_first(first, 5);
first = add_first(first, 8);
first = add_first(first, 15);
first = add_last(first, 14);
first = add_last(first, 34);
element *x = find(first, 15);
display(first);
std::cout << std:: endl << "znaleniono element = " << x->i << " " << std:: endl;
first = add_between(first, x, 28);
// display(first);
// first= remove_element(first, x);
// first = remove_all(first);
// first = remove_integer(first, 15);
first = remove_next_element(first, x);
display(first);
}