Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 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* p, node*& head, node*& tail)
  12. {
  13.     node* usun = new node;
  14.     if (p == head)
  15.     {
  16.         usun = head;
  17.         head = head->next;
  18.         head->prev = NULL;
  19.     }
  20.     else if (p == tail)
  21.     {
  22.         usun = tail;
  23.         tail = tail->prev;
  24.         tail->next = NULL;
  25.     }
  26.     else
  27.     {
  28.         usun = p;
  29.         p->prev->next = p->next;
  30.         p->next->prev = p->prev;
  31.     }
  32.     delete usun;
  33. }
  34.  
  35. void delete_tail(node*& tail)
  36. {
  37.     node* usun = new node;
  38.     usun = tail;
  39.     tail = tail->prev;
  40.     tail->next = NULL;
  41.     delete usun;
  42. }
  43.  
  44. void delete_head(node*& head)
  45. {
  46.     node* usun = new node;
  47.     usun = head;
  48.     head = head->next;
  49.     head->prev = NULL;
  50.     delete usun;
  51. }
  52.  
  53. void insert_head(node*& head, node*& tail, int x)
  54. {
  55.     node* nowy = new node;
  56.     nowy->val = x;
  57.     nowy->next = NULL;
  58.     nowy->prev = NULL;
  59.     if (!head)
  60.     {
  61.         head = nowy;
  62.         tail = nowy;
  63.     }
  64.     else
  65.     {
  66.         nowy->next = head;
  67.         head = nowy;
  68.         nowy->next->prev = nowy;
  69.     }
  70. }
  71.  
  72. void insert_tail(node*& head, node*& tail, int x)
  73. {
  74.     node* nowy = new node;
  75.     nowy->val = x;
  76.     nowy->next = NULL;
  77.     nowy->prev = NULL;
  78.    
  79.     if (!head)
  80.     {
  81.         head = nowy;
  82.         tail = nowy;
  83.     }
  84.     else
  85.     {
  86.         nowy->prev = tail;
  87.         tail = nowy;
  88.         nowy->prev->next = nowy;
  89.     }
  90. }
  91.  
  92. void insert_after(node* p, int x)
  93. {
  94.     node* nowy = new node;
  95.     nowy->val = x;
  96.     nowy->next = p->next;
  97.     nowy->prev = p;
  98.     if (p->next)
  99.     {
  100.         p->next->prev = nowy;
  101.         p->next = nowy;
  102.     }
  103. }
  104.  
  105. void insert_before(node* p, int x)
  106. {
  107.     node* nowy = new node;
  108.     nowy->val = x;
  109.     nowy->next = p;
  110.     nowy->prev = p->prev;
  111.     if (p->prev)
  112.     {
  113.         p->prev->next = nowy;
  114.         p->prev = nowy;
  115.     }
  116. }
  117.  
  118.  
  119. void show(node* head)
  120. {
  121.     while (head)
  122.     {
  123.         cout << head->val << " ";
  124.         head = head->next;
  125.     }cout << endl;
  126. }
  127.  
  128. int main()
  129. {
  130.     node* head = NULL;
  131.     node* tail = NULL;
  132.  
  133.     int wybor = 0, liczba = 0, numer = 0;
  134.  
  135.     do
  136.     {
  137.         node* p = head;
  138.         system("cls");
  139.         cout << "Elementy listy: " << endl;
  140.         show(head);
  141.         cout << "Menu: " << endl << "1. Dodaj na poczatek listy" << endl << "2. Dodaj na koniec listy" << endl << "3. Dodaj po danym elemencie" << endl << "4. Dodaj przed danym elementem" <<endl << "5. Usun element na poczatku" << endl <<"6. Usun element na koncu" << endl << "7. Usun dowolny element" << endl;
  142.         cout << "Wybor: ";
  143.         cin >> wybor;
  144.         switch (wybor)
  145.         {
  146.         case 1:
  147.             cout << "Podaj liczbe: ";
  148.             cin >> liczba;
  149.             insert_head(head, tail, liczba);
  150.             break;
  151.        
  152.         case 2:
  153.             cout << "Podaj liczbe: ";
  154.             cin >> liczba;
  155.             insert_tail(head, tail, liczba);
  156.             break;
  157.  
  158.         case 3:
  159.             cout << "Podaj liczbe: ";
  160.             cin >> liczba;
  161.             cout << "Podaj numer elementu: ";
  162.             cin >> numer;
  163.             for (int i = 1; i < numer; i++)
  164.                 p = p->next;
  165.             insert_after(p, liczba);
  166.             break;
  167.  
  168.         case 4:
  169.         {cout << "Podaj liczbe: ";
  170.         cin >> liczba;
  171.         cout << "Podaj numer elementu: ";
  172.         cin >> numer;
  173.         node* p = head;
  174.         for (int i = 1; i < numer; i++)
  175.             p = p->next;
  176.         insert_before(p, liczba);
  177.         break; }
  178.  
  179.         case 5:
  180.             delete_head(head);
  181.             break;
  182.  
  183.         case 6:
  184.             delete_tail(tail);
  185.             break;
  186.  
  187.         case 7:
  188.             cout << "Podaj numer elementu: ";
  189.             cin >> numer;
  190.             node* p = head;
  191.             for (int i = 1; i < numer; i++)
  192.                 p = p->next;
  193.             del(p, head, tail);
  194.             break;
  195.         }
  196.     } while (wybor != 10);
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement