Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdbool.h>
- #include <assert.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define STACKSIZE 20
- typedef struct {
- int top; /* index of first free slot on stack */
- int data[STACKSIZE];
- }Stack;
- /* int maxsize; // define max capacity of stack
- int top;
- int *items;*/
- void init(struct Stack* s);
- void destroy(struct Stack* s);
- void push(struct Stack* s, int element);
- int pop(struct Stack* s);
- bool isEmpty(struct Stack* s);
- int main (int argc, char **argv)
- {
- // create a stack of capacity 5
- struct Stack *s = newStack(5);
- push(s, 1);
- push(s, 2);
- push(s, 3);
- printf("Top element is %d\n", peek(s));
- printf("Stack size is %d\n", size(s));
- pop(s);
- pop(s);
- pop(s);
- if (isEmpty(s))
- printf("Stack is empty");
- else
- printf("Stack is not empty");
- return 0;
- return(0);
- }
- void init(struct Stack* s)
- {
- s->top = 0;
- }
- void destroy(struct Stack* s)
- {
- s->top = 0;
- }
- void push(struct Stack* s,int element)
- {
- assert(s->top < STACKSIZE);
- s->data[s->top++] = element;
- }
- int pop(struct Stack* s)
- {
- assert(s->top > 0);
- return s->data[--s->top];
- }
- bool isEmpty(struct Stack* s)
- {
- return size(s) == 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment