-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_pointer.cpp
250 lines (213 loc) · 4.53 KB
/
list_pointer.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include <locale.h>
#include <windows.h>
#include <string>
using namespace std;
typedef float Data;
typedef struct No{
Data value;
No *NextElement;
} No;
typedef No *PtNo;
void insert_beginning(PtNo &hea, Data val){
PtNo new_node;
new_node = new No;
new_node->value = val;
if(hea == NULL)
new_node->NextElement = NULL;
else
new_node->NextElement = hea;
hea = new_node;
}
void append(PtNo &hea, Data val){
PtNo new_last_node, temp;
new_last_node = new No;
temp = new No;
new_last_node->value = val;
new_last_node->NextElement = NULL;
if(hea == NULL)
hea = new_last_node;
else{
temp = hea;
while(temp->NextElement != NULL)
temp = temp->NextElement;
//cout << temp->value;
temp->NextElement = new_last_node;
}
}
void insert(PtNo &hea, int position, Data val, bool &ok){
PtNo temp, new_node;
new_node = new No;
temp = new No;
new_node->value = val;
if (hea == NULL){
new_node->NextElement = NULL;
hea = new_node;
}
else{
int coun = 0;
temp = hea;
while(temp != NULL){
temp = temp->NextElement;
coun++;
}
if ((position > 0) && (position < coun)){
ok = true;
temp = hea;
int c = 1;
while(c < position){
temp = temp->NextElement;
c++;
}
new_node->NextElement = temp->NextElement;
temp->NextElement = new_node;
}
else{
ok = false;
}
}
}
void delete_last_elemen(PtNo &hea, bool &ok){
PtNo temp;
temp = new No;
if(hea == NULL)
ok = false;
else{
temp = hea;
while(temp->NextElement != NULL){
if((temp->NextElement)->NextElement == NULL)
break;
temp = temp->NextElement;
}
temp->NextElement = NULL;
}
}
void delete_from_position(PtNo &hea, int position, bool &ok){
PtNo temp;
temp = new No;
temp = hea;
if (hea == NULL)
ok = false;
else{
ok = true;
int cont = 1;
while(cont < position){
temp = temp->NextElement;
cont++;
}
temp->NextElement = (temp->NextElement)->NextElement;
}
}
delete_from_value(PtNo &hea, Data val, bool &ok){
PtNo temp;
temp = new No;
if(hea == NULL)
ok = false;
else{
int coun = 0;
temp = hea;
while(temp != NULL){
temp = temp->NextElement;
coun++;
}
if(coun > 1){
ok = true;
temp = hea;
while(temp != NULL){
if(((temp->NextElement)->value) == val)
break;
temp = temp->NextElement;
}
temp->NextElement = (temp->NextElement)->NextElement;
}
else{
ok = false;
}
}
}
void delete_head(PtNo &hea){
PtNo temp;
temp = new No;
temp = hea->NextElement;
hea = temp;
}
void show_list(PtNo &hea){
PtNo temp;
temp = hea;
while(temp != NULL){
cout << temp->value << " ";
temp = temp->NextElement;
}
}
main(){
//setlocale(LC_ALL, "Portuguese");
bool ok;
PtNo head;
head = new No;
head = NULL;
int option;
Data insert_at_the_en, insert_at_the_begin, value_to_insert, position_to_insert, delete_from_posi, delete_from_val;
while(option != 9){
system("cls");
cout << " 1 INSERT AT THE END \n 2 INSERT AT THE BEGINNING \n 3 INSERT FROM POSITION \n 4 DELETE FROM POSITION \n 5 DELETE LAST NODE \n 6 DELETE FROM A VALUE \n 7 DELETE FIRST NODE \n 8 SHOW LIST \n 9 QUIT" << endl;
cout << " What would you like to do? ";
cin >> option;
switch(option){
case 1:
cout << "Value to append: ";
cin >> insert_at_the_en;
append(head, insert_at_the_en);
break;
case 2:
cout << "Value to insert at the beginning: ";
cin >> insert_at_the_begin;
insert_beginning(head, insert_at_the_begin);
break;
case 3:
cout << "Position to insert: ";
cin >> position_to_insert;
cout << "Value to insert: ";
cin >> value_to_insert;
insert(head, position_to_insert, value_to_insert, ok);
if(ok == false)
cout << "Invalid position!" << endl;
break;
case 4:
cout << "Position to be deleted: ";
cin >> delete_from_posi;
delete_from_position(head, delete_from_posi, ok);
if(ok == false)
cout << "Invalid position!" << endl;
break;
case 5:
delete_last_elemen(head, ok);
if(ok == true)
cout << "Last node deleted!" << endl;
break;
case 6:
cout << "Value to be deleted: ";
cin >> delete_from_val;
delete_from_value(head, delete_from_val, ok);
if(ok == true)
cout << "Deleted successfuly" << endl;
else
cout << "Error!" << endl;
break;
case 7:
delete_head(head);
break;
case 8:
show_list(head);
break;
case 9:
break;
default:
cout << "Invalid option!" << endl;
break;
}
if(!cin)
break;
Sleep(2000);
}
system("Pause");
}