Download as DOCX, PDF, TXT or read online from Scribd
Download as docx, pdf, or txt
You are on page 1of 4
ASSIGNMENT NO 5
STACK
1.STATIC IMPLEMENTATION OF STACK:
//program for static implementation of stack #include<stdio.h> #include<conio.h> #define max 20 struct stack { int data[max]; int top; }; //function for initialize a stack (init function) void init(struct stack *s) { s->top=-1; } //function for checking stack is empty or not int isEmpty(struct stack *s) { if(s->top==-1) { return 1; } else { return 0; } } //function for checking stack is full or not int isFull(struct stack *s) { if(s->top==max-1) { return 1; } else { return 0; } } //function to push element in stack void push(struct stack *s,int item) { s->data[++s->top]=item; } //function to pop element from the stack int pop(struct stack *s) { return(s->data[s->top--]); } //display stack void dispItem(struct stack *s) { int i; if(s->top==-1) { printf("stack is empty"); } else { printf("\nstack elements:\n"); for(i=s->top;i>=0;i--) { printf("%d",s->data[i]); printf("\n"); } } } void main() { struct stack s1; int ch,x; clrscr(); init(&s1); do { printf("1-push\n2-pop\n3-exit"); printf("\nEnter the choice:"); scanf("%d",&ch); switch(ch) { case 1: if(isFull(&s1)) { printf("stack is full"); } else { printf("element to be insert in stack:"); scanf("%d",&x); push(&s1,x); //printf("stack elements:"); dispItem(&s1); } break; case 2: if(isEmpty(&s1)) { printf("stack is empty"); } else { printf("\npoped element is %d",pop(&s1)); dispItem(&s1); } break; case 3: break;; } }while(ch<4); getch(); } Output: