0% found this document useful (0 votes)
16 views

Stack

This C program implements a basic stack data structure using an array. It defines functions to push, pop and show elements in the stack. The main function contains a menu to allow the user to choose these stack operations and continuously performs the selected operation until the user chooses to exit.

Uploaded by

Mohit
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Stack

This C program implements a basic stack data structure using an array. It defines functions to push, pop and show elements in the stack. The main function contains a menu to allow the user to choose these stack operations and continuously performs the selected operation until the user chooses to exit.

Uploaded by

Mohit
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
#include<conio.h>
#include<stdlib.h>
#define max 10

int top=-1;
int stack[max];

void main()
{
int num,ch;
void push();
void pop();
void show();

clrscr();
do
{
printf("\nentr 1 to push elements");
printf("\nentr 2 to pop elements");
printf("\nentr 3 to show elements");
printf("\nentr 4 to exit");
printf("\nentr the choice");
scanf("%d",&ch);

switch (ch)
{
case 1:
{
printf("entr item to add in stack");
scanf("%d",&num);
push(num);
break;
}

case 2:
{
pop();
break;
}

case 3:
{
show();
break;
}

case 4:
{
exit(0);
}
default: printf("entr valid choice");
}
}
while(1);
} \* End of main*\

void push(int num)


{
if(top==max-1)
{
printf("stack is full");
return;
}
top=top+1;
stack[top]=num;
}

void pop()
{
int num;
if(top==-1)
{
printf("stack is empty");
return;
}
num=stack[top];
printf("\n item popped=%d",num);
top=top-1;
}

void show()
{
if (top==-1)
{
printf("stack is empty");
return;
}
while(top!=-1)
{
printf("%d",stack[top]);
top=top-1;
}
getch();
}

You might also like