Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct node
  4. {
  5.     node*next;
  6.     int val;
  7. };
  8. bool isEmpty(node *head)
  9. {
  10.     if (head)return true;
  11.     else false;
  12. }
  13. void enqueue(node *&head, node *&tail, int v)
  14. {
  15.  
  16.  
  17.         node * e = new node;
  18.         e->val =v;
  19.         e->next = nullptr;
  20.         if (tail) tail->next = e;
  21.         else head = e;
  22.         tail = e;
  23. }
  24.  int dequeue(node *&head, node *&tail)
  25. {
  26.     if (head)
  27.     {
  28.        
  29.         node*e = head;
  30.         int tmp = head->val;
  31.         head = head->next;
  32.         delete e;
  33.         if (!head)tail = nullptr;
  34.         cout << "element popped: " << tmp << endl;
  35.         return tmp;
  36.  
  37.     }
  38. }
  39. void showHT(node *head, node *tail)
  40. {
  41.     if (head)
  42.     {
  43.         cout << "head: [" << head->val << "]   " << "tail: [" << tail->val << "]" << endl;
  44.     }
  45.     else cout << "head & tail : NULL" << endl;
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51.     node * head = nullptr;
  52.     node * tail = nullptr;
  53.     dequeue(head,tail);
  54.     showHT(head, tail);
  55.     dequeue(head,tail);
  56.     showHT(head, tail);
  57.     enqueue(head,tail,9);
  58.     showHT(head, tail);
  59.     enqueue(head,tail,2);
  60.     showHT(head, tail);
  61.     enqueue(head,tail,5);
  62.     showHT(head, tail);
  63.     dequeue(head,tail);
  64.     showHT(head, tail);
  65.  
  66.  
  67.    
  68.     system("pause");
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement