Stack
Stack
Stack
1. Push
2. Pop
3. Display
A shown in the figure above , STACK is represented in the form of array,say a[10]. Operation
(addition, deletion and display of contents) can be done mainly with the help of the index i. As we
already know that in a Stack, we add new element in the top of the stack and deletion of element is
also done from the top of the stack which means we obey Last in first out OR First in last Out
mechanism of operation.
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
Stack hi size neisa anih vangin a full ve thei a chumi check nan chuan overflow condition
‘if(top>=n-1)’ kan nei ani. top hi variable ani a, he varia ble hian a index value hrang hrnag a entir
zel a, pakht kan push a piangin top hi kan increase zel ang a, ala full loh chuan stack[top] ah kan element thar
kha kan asiign zel ang. stack[top]=x.
Stack atanga item delete dan ani e. Anihna tak ah chuan engemah kan delete theilo a, kan stack huam chin kan
sawr zim mai chauh ani. top in a huam chin kha pakhati kan ti tlem a, a chungber kha stack zingah kan chhiar
talo mai ani.
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
Now please see and examine the different operations in the DS, work out the following Program and
show me how you’ve done them.
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
Output
98
24
12
Press Next Choice
Enter the Choice:2
24
12
Press Next Choice
Enter the Choice:4
EXIT POINT