-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (123 loc) · 4.51 KB
/
main.cpp
File metadata and controls
123 lines (123 loc) · 4.51 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
#include <bits/stdc++.h>
using namespace std;
class Node{ //звено
public:
int value;
Node* next;
Node(int value){ //конструктор звена
this->value = value;
this->next = NULL;
};
};
class Linked{
public:
Node* root;
Linked(){
root = NULL;
};
void add(int value){
Node* new_node = new Node(value); //выделение места
if (root == NULL || value < root->value){ //если список пустой или значение нового звена меньше, чем значение в корне, то мы вставляем новое звено перед корнем
new_node->next = root;
root = new_node;
}
else{ //в противном случае, мы двигаемся по списку, останавливаемся или в конце или в том месте, где значение нового звена больше чем значение рассматриваемого
//и вставляем между рассматриваемым и следующим за ним новое, ну или вставляем его в конец.
Node* current = root;
while(current->next != NULL && value > current->next->value){
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
};
void remove(int value){
Node* for_deletion = root;
Node* previous = NULL;
while(for_deletion != NULL){
if(for_deletion->value == value) { //если заданное значение есть в списке
if (previous == NULL) { //удаляемое в начале
root = for_deletion->next;
delete for_deletion;
for_deletion = root;
}
else { //удаляемое в середине или в конце
previous->next = for_deletion->next;
delete for_deletion;
for_deletion = previous->next;
}
}
else{ //такого значения в списке нет
previous = for_deletion;
for_deletion = for_deletion->next;
}
}
};
void print_list(){ //ну тут вывод
Node* current = root;
while (current != NULL){
if(current->next == NULL) {
cout << current->value;
current = current->next;
}
else {
cout << current->value << " " << "->" << " ";
current = current->next;
}
}
};
};
int main(){
system("chcp 65001");
//srand(time(NULL));
Linked list;
int size; int value;
char decision;
int for_removal;
int for_addition;
cout << "how many????" << endl;
cin >> size;
cout << "type your values cherez probel: ";
for(int i = 0; i < size; i++)
{
cin >> value;
list.add(value);
//list.add(rand()%100000-rand()%100000);
}
cout << "[ " ; list.print_list(); cout << "]" << endl;
cout << endl;
start:
cout << "wanna add or remove smth????" << endl;
cin >> decision;
switch(decision){
case 'y':
cout << "add or remove?" << endl;
cin >> decision;
switch(decision) {
case 'a':
cout << "how many nodes do you want to add?" << endl;
int quantity;
cin >> quantity;
for(int i = 0; i < quantity; i++) {
cout << "type your value cherez probel: " << endl;
cin >> for_addition;
list.add(for_addition);
}
cout << "[ " ; list.print_list(); cout << "]" << endl;
goto start;
case 'r':
cout << "type your value: " << endl;
cin >> for_removal;
list.remove(for_removal);
cout << "[ " ; list.print_list(); cout << "]" << endl;
goto start;
default: cout << "THINK AGAIN!!!!!!!" << endl; goto start;
}
case 'n':
cout << "here is your linked list!!" << endl;
goto stop;
default: cout << "THINK AGAIN!!!!!!!" << endl; goto start;
}
stop:
return 0;
}