Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. struct nood {
  2.     int data;
  3.     nood* next;
  4.     nood* prev;
  5.    
  6.     nood(int& a):next(NULL), prev(NULL), data(a) {}
  7.     ~nood();
  8. }
  9.  
  10. class Deck {
  11. public:
  12.     Deck(int n);
  13.     ~Deck();
  14.    
  15.     void PushEnd(int a);
  16.     void PushBegin(int a);
  17.     int PopEnd();
  18.     int PopBegin();
  19.    
  20. private:
  21.     int size;
  22.     nood* head;
  23.     nood* tail;
  24. }
  25.  
  26. Deck:: Deck(int n): size(n), first(NULL), last(NULL) {}
  27. Deck:: ~Deck() {
  28.     nood* tmp;
  29.     while(head) {
  30.         tmp = head;
  31.         head = head->next;
  32.         delete tmp;
  33.     }
  34.     tail = NULL;
  35.     size = 0;
  36. }
  37. void Deck:: PushEnd(int& a) {
  38.     nood* new_nood = new nood(a);
  39.     if (head == NULL) {
  40.         head = tail = new_nood;
  41.     }
  42.     else {
  43.         new_nood->prev = tail;
  44.         tail_next = new_nood;
  45.         tail = new_nood;
  46.     }
  47.     size++;
  48. }
  49. void Deck:: PushBegin(int& a) {
  50.     nood* new_nood = new nood(a);
  51.     if (head == NULL) {
  52.         head = tail = new_nood;
  53.     }
  54.     else {
  55.         head->prev = new_nood;
  56.         new_nood->next = head;
  57.         head = new_nood;
  58.     }
  59.     size++;
  60. }
  61. int Deck:: PopBegin() {
  62.     assert(head != NULL);
  63.     tmp = head->data;
  64.     head = head->next;
  65.     head->prev = NULL;
  66.     size--;
  67.     return tmp;
  68. }
  69. int Deck:: PopEnd() {
  70.     assert(head != NULL);
  71.     tmp = tail->data;
  72.     tail = tail->prev;
  73.     tail_next = NULL;
  74.     size--;
  75.     return tmp;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement