0% found this document useful (0 votes)
40 views4 pages

Bharat

This program implements a stack using an array and provides functions to perform push, pop, and display operations on the stack. The key aspects are: 1. An array called stack is used to store the stack elements and an integer variable top tracks the top index of the stack. 2. Functions are defined for push(), pop(), and display() to add/remove elements and view the stack. 3. Overflow and underflow conditions are checked before each operation to ensure valid access to the stack. 4. The main() function provides a menu to choose between the stack operations and loops until the user wants to exit.

Uploaded by

viv0pahwa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Bharat

This program implements a stack using an array and provides functions to perform push, pop, and display operations on the stack. The key aspects are: 1. An array called stack is used to store the stack elements and an integer variable top tracks the top index of the stack. 2. Functions are defined for push(), pop(), and display() to add/remove elements and view the stack. 3. Overflow and underflow conditions are checked before each operation to ensure valid access to the stack. 4. The main() function provides a menu to choose between the stack operations and loops until the user wants to exit.

Uploaded by

viv0pahwa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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; } }

You might also like