Advertisement
LilChicha174

Untitled

Jun 2nd, 2022
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <string>
  3. #include <cstdio>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. struct ListNode {
  9.     ListNode *mNext;
  10.     int mData;
  11. };
  12.  
  13.  
  14. class Queue {
  15. public:
  16.     // конструктор
  17.     Queue() {
  18.         mHead = nullptr;
  19.     }
  20.  
  21.     // деструктор
  22.     ~Queue() {
  23.         ListNode *cur = mHead;
  24.         ListNode *before;
  25.         while (cur != nullptr) {
  26.             before = cur->mNext;
  27.             delete cur;
  28.             cur = before;
  29.         }
  30.     }
  31.  
  32.     void pop() {
  33.         ListNode *dl = mHead;
  34.         while (dl->mNext != nullptr) {
  35.             dl = dl->mNext;
  36.         }
  37.         delete dl->mNext;
  38.         dl->mNext = nullptr;
  39.     }
  40.  
  41.     void push(const int tag) {
  42.         ListNode *new_el = new ListNode;
  43.         new_el->mData = tag;
  44.         new_el->mNext = mHead;
  45.         mHead = new_el;
  46.     }
  47.  
  48.     int top() {
  49.         return mHead->mData;
  50.     }
  51.  
  52.     bool empty() {
  53.         return mHead == nullptr;
  54.     }
  55.  
  56.     size_t size() {
  57.         ListNode *cur = mHead;
  58.         size_t kl = 0;
  59.         while (cur != nullptr) {
  60.             cur = cur->mNext;
  61.             kl++;
  62.         }
  63.         return kl;
  64.     }
  65.  
  66.  
  67. private:
  68.  
  69. protected:
  70.     ListNode *mHead;
  71. };
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement