-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenu.h
More file actions
85 lines (74 loc) · 1.91 KB
/
Copy pathMainMenu.h
File metadata and controls
85 lines (74 loc) · 1.91 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
/*
Файл с однонаправленным списком главного меню
*/
#pragma once
#include <iostream>
using namespace std;
// указатель на следующий элемент в списке
struct Node {
string data;
struct Node* next;
};
class MainMenu {
private:
struct Node* my_head;
string prase;
int count_item;
public:
// для первого элемента
MainMenu(string phrase) {
count_item = 0;
addItem(phrase);
}
// очищаем память
~MainMenu() {
struct Node* old = NULL;
struct Node* current = my_head;
while (current != NULL) {
old = current;
current = current->next;
delete old;
}
}
// добавляет элемент в начало
void addItem(string inp_data) {
struct Node* new_item = new Node;
new_item->data = inp_data;
if (count_item == 0) { new_item->next = NULL; }
else { new_item->next = my_head; }
my_head = new_item;
count_item++;
}
// добавляет элемент в конец
void insertItem(int index, string inp_data) {
if (index < count_item || count_item < 0) { return; }
else if (index == 0) { addItem(inp_data); }
else {
struct Node* current = my_head;
for (int i = 0; i < index - 1; i++) {
current = current->next;
}
struct Node* new_item = new Node;
new_item->data = inp_data;
new_item->next = current->next;
current->next = new_item;
count_item++;
}
}
// вызывает функцию insertItem и сообщает ей, сколько элементов уже добавлено
void push_back(string inp_data) {
insertItem(count_item, inp_data);
}
// вывод элементов
void printItem() {
struct Node* current = my_head;
int i = 0;
while (current != NULL) {
if (i != 0 && i != count_item - 1) { cout << i << ". "; }
cout << current->data;
if (i != count_item - 1) { cout << "\n"; }
i++;
current = current->next;
}
}
};