Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #define SIZE 5 /* Size of Stack */
- char s[SIZE][20]; /* Global declarations */
- int top = 0; /* -1 indicates stack is empty */
- void push(char *element) /* Function for PUSH operation */
- {
- if(isFull())
- printf("\n Stack is full!\n");
- else {
- ++top;
- strcpy(s[top],element);
- }
- }
- char* pop() /* Function for POP operation */
- {
- char element[20];
- if(isEmpty()) {
- printf("\nStack is Empty!\n");
- }
- else {
- printf("\n Popped Element is %s \n", s[top]);
- top--;
- }
- }
- int isFull() /* Function to Check if Stack is Full */
- {
- if(top == SIZE)
- return 1;
- return 0;
- }
- int isEmpty() /* Function to Check if Stack is Empty */
- {
- if(top == 0)
- return 1;
- return 0;
- }
- void display() /* Function to display status of Stack */
- {
- int i;
- if(isEmpty())
- printf(" \n Empty Stack\n");
- else
- {
- for (i = top; i>=1; i--)
- printf("%s\n", s[i]);
- }
- }
- void main() /* Main Program */
- {
- int opn;
- char element[20];
- do
- {
- printf("\n Stack Operations \n");
- printf("\n Press 1-Push, 2-Pop,3-Display,4-Exit\n");
- printf("\n Your option ? ");
- scanf("%d", &opn);
- switch (opn)
- {
- case 1:
- printf("\n Enter the element to be pushed ?");
- scanf(" %[^\n]s", &element);
- push(element);
- break;
- case 2:
- pop();
- break;
- case 3:
- printf("\n Status of Stack \n");
- display();
- break;
- case 4:
- printf("\n Terminating \n");
- break;
- default:
- printf("\n Invalid Option !!! Try Again !! \n");
- break;
- }
- printf("\n Press a Key to Continue . . . ");
- }
- while (opn != 4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement