Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define stack_max 10
- //structure variable
- typedef struct{
- int top;
- int data[stack_max];
- }stack;
- //push function
- void push(stack *s, int item)
- {
- if(s->top < stack_max){
- s->data[s->top] = item;
- s->top++;//when, pushing will be done the value of top will be one more than the length of the array
- //so in the pop function first decrease one from top to excess the array
- }
- else{
- printf("stack is full!!\n");
- }
- //printf("%d\n", s->top);
- }
- //pop function
- int pop(stack *s)
- {
- int item;
- if(s->top == 0){
- printf("Stack is empty\n");
- return -1;
- }
- else{
- //decreasing top value one
- s->top--;
- item = s->data[s->top];
- }
- return item;
- }
- int main()
- {
- stack my_stack;
- int item;
- my_stack.top = 0;
- //calling push function
- push(&my_stack, 1);
- //printf("%d\n", my_stack.top);
- push(&my_stack, 2);
- //printf("%d\n", my_stack.top);
- push(&my_stack, 3);
- //printf("%d\n", my_stack.top);
- //calling pop function
- item = pop(&my_stack);
- printf("%d\n", item);
- item = pop(&my_stack);
- printf("%d\n", item);
- item = pop(&my_stack);
- printf("%d\n", item);
- /*item = pop(&my_stack);
- printf("%d\n", item);*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement