Advertisement
nhbao

Untitled

Apr 12th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct node
  4. {
  5.     int data;
  6.     struct node *pNext;
  7. };
  8. typedef struct node NODE;
  9. struct stack
  10. {
  11.     NODE* pHead;
  12.     NODE* pTail;
  13. };
  14. typedef struct stack STACK;
  15. void init(STACK& s)
  16. {
  17.     s.pHead = NULL;
  18.     s.pTail = s.pHead;
  19. }
  20. NODE* getnode(int x)
  21. {
  22.     NODE* p = new NODE;
  23.     p->pNext = NULL;
  24.     p->data = x;
  25.     return p;
  26. }
  27. bool ifempty(STACK s)
  28. {
  29.     if (s.pHead == NULL) return true;
  30.     else return false;
  31.      
  32. }
  33. int pop(STACK &s)//lấy giá trị top ra và phá hủy nó
  34. {
  35.     if (ifempty(s) == true) cout << "error";
  36.     else
  37.     {
  38.         int x;
  39.         NODE* p = s.pHead;
  40.         x = p->data;
  41.         delete p;
  42.         return x;
  43.     }
  44. }
  45. int peak(STACK& s,int x)
  46. {
  47.     if (ifempty(s) == true) cout << "error";
  48.     else
  49.     {
  50.        
  51.         x = s.pHead->data;
  52.     }
  53.     return x;
  54. }
  55. void push(STACK& s, NODE *p)
  56. {
  57.     if (ifempty(s) == true) cout << "error";
  58.     else
  59.     {
  60.         p->pNext = s.pHead;
  61.         s.pHead = p;
  62.     }
  63. }
  64. void input(STACK& s)
  65. {
  66.     int x;
  67.     int n;
  68.     cout << "nhap so phan tu x cua stack: ";
  69.     cin >> x;
  70.  
  71.     for (int i = 1; i <= x; i++)
  72.     {
  73.         cout << "nhap phan tu: ";
  74.         cin >> n;
  75.         NODE* p = getnode(n);
  76.     }
  77. }
  78. void output(STACK s)
  79. {
  80.     NODE* p = s.pHead;
  81.     while( p != NULL)
  82.     {
  83.         cout << p->data;
  84.         p = p->pNext;
  85.     }
  86.    
  87. }
  88. int main()
  89. {
  90.     STACK s;
  91.     init(s);
  92.     input(s);
  93.     output(s);
  94.     system("pause");
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement