Стек (лише 1 клас) #include <iostream> using namespace std; class Worker { public: int number; Worker* privious = nullptr; }; int main() { int a; cout << "a=" << endl; cin >> a; Worker r; r.number = a; Worker* tail = &r; while (true) { cout << "a=" << endl; cin >> a; if (a == -1) { break; } Worker *w = new Worker(); w->number = a; w->privious = tail; cout << "prev=" << w->privious << endl; tail = w; cout << "prev=" << tail << endl; } Worker* tmp = tail; while (tmp != nullptr) { cout << tmp->number << endl; tmp = tmp->privious; system("pause"); } system("pause"); return 0; }
Черга (2 класи) #include <iostream> using namespace std; class Node { public: void SetA(int b) { a = b; } void SetNext(Node* n) { next = n; } int GetA() { return a; } Node* GetNext() { return next; } private: int a; Node* next; }; class List { public: void Add(int c) { Node* tmp = new Node(); tmp->SetA(c); tmp->SetNext(nullptr); if (head == nullptr) { head = tmp; } else { Node* tmp2 = head; while (tmp2->GetNext() != nullptr) { tmp2 = tmp2->GetNext(); } tmp2->SetNext(tmp); } } void Show() { Node* tmp = head; while (tmp != nullptr) { cout << tmp->GetA() << endl; tmp = tmp->GetNext(); } } private: Node* head = nullptr; }; int main() { List list; list.Add(7); list.Add(8); list.Add(9); list.Show(); system("pause"); return 0; }
7
8
9
8
9
Список head, tail, size
Двобічно зв'язаний список
Список, що приймає різні типи даних