tariq_zaghal

Stack

Apr 28th, 2023 (edited)
1,114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int Container;
  5. struct Stack{
  6.     int capacity;
  7.     int top;
  8.     Container* stack ;
  9. };
  10.  
  11. struct Stack* buildStack();
  12. int isEmpty(struct Stack* S);
  13. void push(int x, struct Stack* S);
  14. int pop(struct Stack* S);
  15. int top(struct Stack* S);
  16. void makeEmpty(struct Stack* S);
  17. void EmptyTheStackAndPrint(struct Stack* S);
  18.  
  19. int main(){
  20.  
  21. struct Stack* stack2 = buildStack();
  22.  
  23. push(10,stack2);
  24. push(20,stack2);
  25. push(30,stack2);
  26. push(40,stack2);
  27. push(50,stack2);
  28. push(60,stack2);
  29. push(70,stack2);
  30.  
  31.  
  32. push(10,stack2);
  33. push(20,stack2);
  34. push(30,stack2);
  35. push(40,stack2);
  36. push(50,stack2);
  37. push(60,stack2);
  38. push(70,stack2);
  39.  
  40.  
  41. push(10,stack2);
  42. push(20,stack2);
  43. push(30,stack2);
  44. push(40,stack2);
  45. push(50,stack2);
  46. push(60,stack2);
  47. push(70,stack2);
  48.  
  49.  
  50. push(10,stack2);
  51. push(20,stack2);
  52. push(30,stack2);
  53. push(40,stack2);
  54. push(50,stack2);
  55. push(60,stack2);
  56. push(70,stack2);
  57.  
  58.  
  59. EmptyTheStackAndPrint(stack2);
  60.  
  61. push(100,stack2);
  62. push(200,stack2);
  63.  
  64. EmptyTheStackAndPrint(stack2);
  65.  
  66. return 0;
  67. }
  68.  
  69.  
  70. struct Stack* buildStack(){
  71.  
  72.     struct Stack* stk = malloc(sizeof(struct Stack));
  73.     stk->stack = malloc(sizeof(int)*10);
  74.     stk->top = -1;
  75.     stk->capacity = 10;
  76.  
  77.  
  78.     return stk;
  79.  
  80. }
  81.  
  82.  
  83.  
  84. int isEmpty(struct Stack* S){
  85.         return S->top == -1;
  86. }
  87.  
  88.  
  89. void push(int x, struct Stack* S){
  90.     if(S->top+1<S->capacity){
  91.        S->top++;
  92.        int i = S->top;
  93.        S->stack[i] = x;
  94.    
  95.     }else{
  96.         Container* temp = malloc(sizeof(Container)*S->capacity*2);
  97.  
  98.         for(int i = 0; i < S->capacity; i++){
  99.             temp[i] = S->stack[i];
  100.         }
  101.         free(S->stack);
  102.         S->stack = temp;
  103.         S->capacity *=2;
  104.  
  105.     }
  106. }
  107.  
  108.  
  109. int pop(struct Stack* S){
  110.    
  111.     if(!isEmpty(S)){
  112.         int i = S->top;
  113.         S->top--;
  114.  
  115.         return S->stack[i];
  116.  
  117.     }
  118.  
  119.     printf("The Stack is empty!\n");
  120.     return -1;
  121.    
  122. }
  123.  
  124.  
  125. int top(struct Stack* S){
  126.  
  127.     if(!isEmpty){
  128.         int i = S->top;
  129.         return S->stack[i];
  130.     }
  131.  
  132.     printf("The Stack is empty!\n");
  133.     return -1;
  134.  
  135. }
  136.  
  137. void makeEmpty(struct Stack* S){
  138.  
  139.     S->top = -1;
  140.  
  141.     Container* temp1 = malloc(sizeof(int)*10);
  142.     free(S->stack);
  143.     S->stack = temp1;
  144.  
  145.     S->capacity = 10;
  146. }
  147.  
  148.  
  149. void EmptyTheStackAndPrint(struct Stack* S){
  150.  
  151.     while(S->top>-1){
  152.         int x = pop(S);
  153.         printf("%d   ",x);
  154.     }
  155.  
  156.     printf("\n");
  157.  
  158. }
  159.  
  160.  
  161.  
Advertisement
Add Comment
Please, Sign In to add comment