Mary_99

TASK1A some try

Oct 7th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include <assert.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #define STACKSIZE 20
  10.  
  11. typedef struct {
  12.  
  13. int top; /* index of first free slot on stack */
  14. int data[STACKSIZE];
  15. }Stack;
  16.  
  17.  
  18. /* int maxsize;    // define max capacity of stack
  19.     int top;      
  20.     int *items;*/
  21.  
  22.  
  23.  
  24. void init(struct Stack* s);
  25. void destroy(struct Stack* s);
  26. void push(struct Stack* s, int element);
  27. int pop(struct Stack* s);
  28. bool isEmpty(struct Stack* s);
  29.  
  30. int main (int argc, char **argv)
  31. {
  32.     // create a stack of capacity 5
  33.     struct Stack *s = newStack(5);
  34.  
  35.     push(s, 1);
  36.     push(s, 2);
  37.     push(s, 3);
  38.  
  39.     printf("Top element is %d\n", peek(s));
  40.     printf("Stack size is %d\n", size(s));
  41.  
  42.     pop(s);
  43.     pop(s);
  44.     pop(s);
  45.  
  46.     if (isEmpty(s))
  47.  
  48.         printf("Stack is empty");
  49.     else
  50.         printf("Stack is not empty");
  51.  
  52.     return 0;
  53.  
  54.     return(0);
  55. }
  56.  
  57.  
  58. void init(struct Stack* s)
  59. {
  60.     s->top = 0;
  61. }
  62.  
  63. void destroy(struct Stack* s)
  64. {
  65.     s->top = 0;
  66. }
  67.  
  68.  
  69.  
  70. void push(struct Stack* s,int element)
  71. {
  72.     assert(s->top < STACKSIZE);
  73.     s->data[s->top++] = element;
  74. }
  75.  
  76. int pop(struct Stack* s)
  77. {
  78.     assert(s->top > 0);
  79.     return s->data[--s->top];
  80. }
  81.  
  82. bool isEmpty(struct Stack* s)
  83. {
  84.     return size(s) == 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment