Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7.     int val;
  8.     node *next, *prev;
  9. };
  10.  
  11. void del(node*& head, node*& tail, node*& p, int &count)
  12. {
  13.     if (p->prev)
  14.         p->prev->next = p->next;
  15.     else
  16.         head = p->next;
  17.     if (p->next)
  18.         p->next->prev = p->prev;
  19.     else
  20.         tail = p->prev;
  21.     delete p;
  22.     count--;
  23. }
  24.  
  25. void ins_tail(node*& head, node*&tail, int x, int &count)
  26. {
  27.     node* nowy = new node;
  28.     nowy->next = NULL;
  29.     nowy->prev = NULL;
  30.     nowy->val = x;
  31.     if (!head)
  32.     {
  33.         head = nowy;
  34.         tail = nowy;
  35.     }
  36.     else
  37.     {
  38.         nowy->prev = tail;
  39.         tail->next = nowy;
  40.         tail = nowy;
  41.     }count++;
  42. }
  43.  
  44. void ins_head(node*& head, node*& tail, int x, int &count)
  45. {
  46.     node* nowy = new node;
  47.     nowy->next = NULL;
  48.     nowy->prev = NULL;
  49.     nowy->val = x;
  50.     if (!head)
  51.     {
  52.         head = nowy;
  53.         tail = nowy;
  54.     }
  55.     else
  56.     {
  57.         nowy->next = head;
  58.         head->prev = nowy;
  59.         head = nowy;
  60.     }count++;
  61. }
  62.  
  63. void ins_after(node*& p, int x, int &count)
  64. {
  65.     node* nowy = new node;
  66.     nowy->val = x;
  67.     nowy->next = p->next;
  68.     nowy->prev = p;
  69.     p->next->prev = nowy;
  70.     p->next = nowy;
  71.     count++;
  72. }
  73.  
  74. void ins_before(node*& p, int x, int &count)
  75. {
  76.     node* nowy = new node;
  77.     nowy->val = x;
  78.     nowy->next = p;
  79.     nowy->prev = p->prev;
  80.     p->prev->next = nowy;
  81.     p->prev = nowy;
  82.     count++;
  83. }
  84.  
  85. void show(node* head)
  86. {
  87.     while (head)
  88.     {
  89.         cout << head->val << " ";
  90.         head = head->next;
  91.     }cout << endl;
  92. }
  93.  
  94. int main()
  95. {
  96.     node* head = NULL;
  97.     node* tail = NULL;
  98.     int wybor=0, liczba=0, numer=0, count=0;
  99.     node* p = NULL;
  100.     do
  101.     {
  102.         p = head;
  103.         system("CLS");
  104.         cout << "Elementy listy: " << endl;
  105.         show(head);
  106.         cout << "Ilosc elementow w liscie: " << count << endl;
  107.         cout << "Menu listy: " << endl << "1. Dodaj na poczatek" << endl << "2. Dodaj na koniec" << endl << "3. Dodaj po danym elemencie" << endl << "4. Dodaj przed danym elementem" << endl << "5. Usun dowolny element" << endl << "Wybor: ";
  108.         cin >> wybor;
  109.  
  110.         switch (wybor)
  111.         {
  112.         case 1:
  113.             cout << "Podaj liczbe: ";
  114.             cin >> liczba;
  115.             ins_head(head, tail, liczba, count);
  116.             break;
  117.  
  118.         case 2:
  119.             cout << "Podaj liczbe: ";
  120.             cin >> liczba;
  121.             ins_tail(head, tail, liczba, count);
  122.             break;
  123.  
  124.         case 3:
  125.             cout << "Podaj numer elementu: ";
  126.             cin >> numer;
  127.             cout << "Podaj liczbe: ";
  128.             cin >> liczba;
  129.             for (int i = 1; i < numer; i++)
  130.                 p = p->next;
  131.             ins_after(p, liczba, count);
  132.             break;
  133.  
  134.         case 4:
  135.             cout << "Podaj numer elementu: ";
  136.             cin >> numer;
  137.             cout << "Podaj liczbe: ";
  138.             cin >> liczba;
  139.             for (int i = 1; i < numer; i++)
  140.                 p = p->next;
  141.             ins_before(p, liczba, count);
  142.             break;
  143.  
  144.         case 5:
  145.             cout << "Podaj numer elementu: ";
  146.             cin >> numer;
  147.             for (int i = 1; i < numer; i++)
  148.                 p = p->next;
  149.             del(head, tail, p, count);
  150.             break;
  151.         }
  152.     } while (wybor != 10);
  153.  
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement