Advertisement
Haru2k

Queue double link struct

Apr 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. //QUEUE//
  2. #include <iostream>
  3. #include "ctime"
  4.  
  5. using namespace std;
  6.  
  7. struct dl
  8. {
  9.     dl* prev;
  10.     dl* next;
  11.     int data;
  12. };
  13.  
  14.  
  15. dl* pop(dl* ft);
  16. void push(dl** ft, dl** end, int n);
  17.  
  18. bool empty(dl* ft);
  19. void dl_cout(dl* ft);
  20. void dl_delete(dl** ft);
  21.  
  22.  
  23. int main()
  24. {
  25.     srand(time(0));
  26.  
  27.     dl* ft = NULL;
  28.     dl* end = ft;
  29.  
  30.     int i, n;
  31.  
  32.     for (i = 0; i < 10; i++)
  33.     {
  34.         n = rand() % 100;
  35.         push(&ft, &end, n);
  36.     }
  37.  
  38.     dl_cout(ft);
  39.     ft = pop(ft);
  40.     cout << "Empty? " << std::boolalpha << empty(ft) << endl;
  41.     dl_cout(ft);
  42.     dl_delete(&ft);
  43.  
  44.     cout << "Empty? " << std::boolalpha << empty(ft) << endl;
  45. }
  46.  
  47. dl* pop(dl* ft)
  48. {
  49.     dl* ptr = ft;
  50.     if (ft == NULL)
  51.         return NULL;
  52.     else if (ft->next == NULL)
  53.     {
  54.         delete ft;
  55.         return NULL;
  56.     }
  57.     ptr = ft->next;
  58.     delete ft;
  59.     ft = ptr;
  60.     return ft;
  61. }
  62.  
  63. void push(dl** ft, dl** end, int n)
  64. {
  65.     dl* ptr = new dl;
  66.     ptr->data = n;
  67.     ptr->next = *ft;
  68.  
  69.     if (*end == NULL)
  70.         * end = ptr;
  71.  
  72.     *ft = ptr;
  73. }
  74.  
  75. bool empty(dl* ft) { return ft == NULL; }
  76.  
  77. void dl_cout(dl* ft)
  78. {
  79.     while (ft != NULL)
  80.     {
  81.         cout << ft->data << " ";
  82.         ft = ft->next;
  83.     }
  84.     cout << endl;
  85. }
  86.  
  87. void dl_delete(dl** ft)
  88. {
  89.     while (!empty(*ft))
  90.         * ft = pop(*ft);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement