Линейный однонаправленный список (Старый вариант)

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> using namespace std; struct test{ int a; test *next; }; class List { private: test *head, *tail; public: List():head(nullptr),tail(nullptr){}; }; void List::addElem(int num) { tech *temp = new tech; temp->a = num; if (head != nullptr) { //перевірка на порожність tail->next = temp; tail = temp; temp->next = nullptr; } else { head = tail = temp; tail->next = head; head->next = tail; } } void List::printList() { if (head != nullptr) { //перевірка на наявність с test *temp = head; // створення покажчика і вказуємо на початок while (temp != nullptr) { // поки список не дійшов до кінця cout << temp->a << endl; temp = temp->next; //переадресовується на наступний елемент } } } void List::delElem() { if (head != tail) { tech *temp = head->next; delete head; // видаляє елемент з “голови” head = temp; } else { delete head; head = tail = nullptr; } } int main() { List list; list.addElem(5); list.addElem(10); list.addElem(5); list.addElem(15); list.print(); list.delElem(); list.print(); list.addElem(5); list.addElem(10); list.addElem(20); list.addElem(15); list.print(); list.delElem(); list.print(); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines