Advertisement
congdantoancau

Array stack

Dec 13th, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include<iostream>
  2. #define MAXSIZE 100
  3. using namespace std;
  4.  
  5. struct array_stuck
  6. {
  7.     int nTop;
  8.     int nCapacity;
  9.     int *arr;
  10. };
  11.  
  12. void initial(array_stuck &s);
  13. bool isEmpty(array_stuck s);
  14. bool isFull(array_stuck s);
  15. void push(array_stuck &s, int nData);
  16. int pop(array_stuck &s);
  17.  
  18.  
  19. int main()
  20. {
  21.     array_stuck s;
  22.     initial(s);
  23.  
  24.     push(s, 5);
  25.     push(s, 6);
  26.  
  27.     cout << pop(s) << endl;
  28.     cout << pop(s) << endl;
  29.     cout << pop(s) << endl;
  30.  
  31.     system("pause");
  32.     return 0;
  33. }
  34.  
  35. int pop(array_stuck &s)
  36. {
  37.     if (isEmpty(s))
  38.         return INT_MIN;
  39.     else
  40.         return s.arr[s.nTop--];
  41. }
  42.  
  43. void push(array_stuck &s, int nData)
  44. {
  45.     if (isFull(s))
  46.         cout << "array_stuck overflow!\n";
  47.     else
  48.         s.arr[++s.nTop] = nData;
  49. }
  50.  
  51. bool isFull(array_stuck s)
  52. {
  53.     return s.nTop == s.nCapacity - 1;
  54. }
  55.  
  56. bool isEmpty(array_stuck s)
  57. {
  58.     return s.nTop == -1;
  59. }
  60.  
  61. void initial(array_stuck &s)
  62. {
  63.     s.nCapacity = MAXSIZE;
  64.     s.nTop = -1;
  65.     s.arr = new int[s.nCapacity];
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement