Q. Program to perform push,pop,traverse operation on stack using array? #include<stdio.h> #include<conio.
h> #define MAX 5 int top=-1; int stack[MAX]; int overflow(); int underflow(); void push(); void pop(); void display(); void main() { int choice; char c='y'; clrscr(); while(c=='y') { printf("press 1 for push operation"); printf("press 2 for pop operation"); printf("press 3 for display \n"); printf("enter your choice"); scanf("%d",&choice); { case1: push(); break; case2: pop(); break; case3: display(); break; default: printf("\n you have entered wrong choice"); } printf("\n do you want to continue?y \n"); fflush(stdin); scanf("%c",&c); } getch(); } int overflow() { if(top==[MAX-1]) { printf("stack overflow"); return 1; } else return 0; } int underflow(); { if(top==-1) { printf("stack is underflow");
return 1; } else { return 0; } void push() { int value; if(!overflow()) { printf("\n enter element to be pushed"); scanf("%d",&value); top=top+1; stack[top]=value; } void pop() { if(!underflow()) { printf("\n popped element is %d",stack[top]); top=top-1; } } void display() { int i; if(!underflow()) { printf("\n the elements are:"); for(i=0;i<=top;i++) printf("%d",stack[i]); }
Q. Program to perform push operation in stack using array?
#include<stdio.h> #include<conio.h> #define size 5 void push(); int overflow(); int stack[size],top=-1; void main() { char c='y'; clrscr(); while(c=='y') { push(); printf("do you want to insert more elements"); fflush(stdin); scanf("%c",&c); } getch(); } void push() { int a; if(!overflow()) { printf("enter elements"); scanf("%d",&a); top=top+1; stack[top]=a; } else { printf("stack overflow"); } } int overflow() { if(top==size-1) { return 1; } else { return 0; } }