Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <stdbool.h>
- void init_stack(int);
- void push(int);
- int pop();
- int peek();
- void print(int);
- int top = -1;
- int stack[];
- int main()
- {
- int item, stack_size;
- printf("Size of your stack : ");
- scanf("%d", &stack_size);
- printf("This Stack Allows Only Integer Value!");
- stack[stack_size];
- init_stack(stack_size); //initializing the stack array with zeros
- bool t = true;
- while(t)
- {
- printf("\n\n1. Push\n2. Pop\n3. Peek\n4. isEmpty\n5. isFull\n6. Print Stack\n7. Exit");
- printf("\n\nChoose : ");
- int c;
- scanf("%d", &c);
- switch(c)
- {
- case 1 :
- printf("\n\nitem : ");
- scanf("%d", &item);
- if(top < stack_size-1)
- {
- push(item);
- printf("\nSuccessful...");
- }
- else printf("\n\tStack overflow!");
- break;
- case 2 :
- if(top > -1)
- {
- item = pop();
- printf("\nPopped item is : %d", item);
- }
- else printf("\n\tStack underflow!");
- break;
- case 3 :
- if(top > -1)
- {
- item = peek();
- printf("\nTop item is : %d", item);
- }
- else printf("\nStack is empty!");
- break;
- case 4 :
- if(top > -1) printf("\nStack is not empty.");
- else printf("\nStack is empty!");
- break;
- case 5 :
- if(top < stack_size-1) printf("\nStack is not full.");
- else printf("\nStack is full!");
- break;
- case 6 :
- printf("\n\nCurrent stack :");
- print(stack_size);
- break;
- default :
- t = false;
- }
- }
- return 0;
- }
- void push(int item)
- {
- top++;
- stack[top] = item;
- }
- int pop()
- {
- int item = stack[top];
- stack[top] = 0; //deleting the item from the stack
- top--;
- return item;
- }
- int peek()
- {
- return stack[top];
- }
- void print(int n)
- {
- for(int i=0; i<n; i++)
- {
- printf("\t%d", stack[i]);
- }
- }
- void init_stack(int n)
- {
- for(int i=0; i<n; i++)
- {
- stack[i] = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement