Advertisement
huutho_96

Stack + Queue

Aug 5th, 2015
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6.     int x;
  7.     Node *pNext;
  8. };
  9.  
  10. struct Stack
  11. {
  12.     Node *pHead;
  13.     Node *pTail;
  14. };
  15.  
  16. void CreateEmptyStack(Stack &S)
  17. {
  18.     S.pHead = S.pTail = NULL;
  19. }
  20. //thêm phần tử vào Stack. addhead. khúc này gồm 2 bước tạo node và addhead
  21. void Push(Stack &S, int x)
  22. {
  23.     Node *p = new Node;
  24.     if (p == NULL)
  25.         exit(1);
  26.     p->x = x;
  27.     p->pNext = NULL;
  28.     if (S.pHead == NULL)
  29.     {
  30.         S.pHead = S.pTail = p;
  31.     }
  32.     else
  33.     {
  34.         p->pNext = S.pHead;
  35.         S.pHead = p;
  36.     }
  37. }
  38.  
  39. bool IsEmpty(Stack S)
  40. {
  41.     return (S.pHead == NULL);
  42. }
  43.  
  44. bool Pop(Stack &S, int &x)
  45. {
  46.     Node *p = S.pHead;
  47.     if (!IsEmpty(S))
  48.     {
  49.         x = p->x;
  50.         S.pHead = S.pHead->pNext;
  51.         delete p;
  52.         return true;
  53.     }
  54.     return false;
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. struct Queue
  66. {
  67.     Node *pHead;
  68.     Node *pTail;
  69. };
  70.  
  71. void CreateEmptyQueue(Queue &Q)
  72. {
  73.     Q.pHead = Q.pTail = NULL;
  74. }
  75.  
  76. //thêm phần tử vào Queue. addtail. khúc này gồm 2 bước tạo node và addhead
  77. void Push(Queue &Q, int x)
  78. {
  79.     Node *p = new Node;
  80.     if (p == NULL)
  81.         exit(1);
  82.     p->x = x;
  83.     p->pNext = NULL;
  84.     if (Q.pHead == NULL)
  85.     {
  86.         Q.pHead = Q.pTail = p;
  87.     }
  88.     else
  89.     {
  90.         Q.pTail->pNext = p;
  91.         Q.pTail = p;
  92.     }
  93. }
  94.  
  95. bool IsEmpty(Queue Q)
  96. {
  97.     return (Q.pHead == NULL);
  98. }
  99.  
  100. bool Pop(Queue &Q, int &x)
  101. {
  102.     Node *p = Q.pHead;
  103.     if (!IsEmpty(Q))
  104.     {
  105.         x = p->x;
  106.         Q.pHead = Q.pHead->pNext;
  107.         delete p;
  108.         return true;
  109.     }
  110.     return false;
  111. }
  112.  
  113. //Nhập cho Stack
  114. void Input(Stack &S)
  115. {
  116.     cout << "Nhap den khi gia tri nhap vao bang 0\n";
  117.     int x;
  118.     do
  119.     {
  120.         cout << "x = ";
  121.         cin >> x;
  122.         if (x == 0) break;
  123.         Push(S, x);
  124.     } while (1);
  125. }
  126.  
  127. //Chuyển số chẵn từ Stack qua Queue
  128. void Input(Stack &S, Queue &Q)
  129. {
  130.     int x;
  131.     while (!IsEmpty(S))
  132.     {
  133.         if (Pop(S, x) == true)
  134.         {
  135.             if (x % 2 == 0)
  136.                 Push(Q, x);
  137.         }
  138.     }
  139. }
  140.  
  141.  
  142. //Xuất Queue ra để xem kết quả
  143. void Output(Queue &Q)
  144. {
  145.     int x;
  146.     while (!IsEmpty(Q))
  147.     {
  148.         Pop(Q, x);
  149.         cout << x << endl;
  150.     }
  151. }
  152.  
  153. void main()
  154. {
  155.     Stack S;
  156.     Queue Q;
  157.     CreateEmptyQueue(Q);
  158.     CreateEmptyStack(S);
  159.     Input(S);
  160.     Input(S, Q);
  161.     Output(Q);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement