Список (Linked lists)

Стек (лише 1 клас)

#include <iostream>

using namespace std;

class Worker{

 public:

     int number;

     Worker* privious = NULL;

};

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 != NULL)

     {

          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(NULL);

        if (head == NULL) {

            head = tmp;

        }

        else {

            Node* tmp2 = head;

            while (tmp2->GetNext() != NULL)

            {

                tmp2 = tmp2->GetNext();

            }

            tmp2->SetNext(tmp);

        }

    }

    void Show()

    {

        Node* tmp = head;

        while (tmp != NULL)

        {

            cout << tmp->GetA() << endl;

            tmp = tmp->GetNext();

        }

    }

 private:

    Node* head = NULL; 

};

int main()

{

    List list;

    list.Add(7);

    list.Add(8);

    list.Add(9);

    list.Show();  

    system("pause"); 

    return 0;

}

7

8

9

Список head, tail, size

Двобічно зв'язаний список

Список, що приймає різні типи даних