Advertisement
lordasif

stack fuad

Mar 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. struct stack
  8. {
  9. /// int stk[10];
  10.  
  11. int *stk;
  12. int cap, size, init_cap;
  13. void init(int input_cap)
  14. {
  15. /// int stk[10];
  16. stk = (int *)malloc(input_cap * sizeof(int));
  17. cap = input_cap;
  18. init_cap = input_cap;
  19. size = 0;
  20. }
  21. void push_front(int value)
  22. {
  23. /// size --> index
  24. if(size == cap)
  25. {
  26. /// new cap = old cap + init cap;
  27. /// 10 = 5 + 5
  28. /// 15 = 10 + 5
  29. /// 20 = 15 + 5
  30. cap = cap + init_cap;
  31. realloc(stk, cap * sizeof(int));
  32. }
  33. stk[size] = value;
  34. size = size + 1; /// size++
  35. }
  36. int pop_front()
  37. {
  38. /// index --> size;
  39. int temp = stk[size];
  40. size = size - 1;
  41. return temp;
  42.  
  43. }
  44. void print()
  45. {
  46. int i;
  47. for(i = 0; i < size; i++)
  48. cout << stk[i] << endl;
  49. }
  50. };
  51.  
  52. int main()
  53. {
  54. stack s;
  55. s.init(10);
  56. //cout << s.size << ' ' << s.cap;
  57. s.push_front(10);
  58. s.push_front(20);
  59. s.push_front(30);
  60. s.push_front(40);
  61. s.print();
  62. cout << "##\n";
  63. s.pop_front();
  64.  
  65. s.pop_front();
  66.  
  67. s.print();
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement