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

Stack Implementation Using Array

Data structure Topic

Uploaded by

55- Sheldon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Stack Implementation Using Array

Data structure Topic

Uploaded by

55- Sheldon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<stdio.

h>
int stack[100],choice,n,top,x,i;
void push();
void pop();
void peek();
void display();
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.PEEK\n\t
4.DISPLAY\n\t 5.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peek();
break;
}

case 4:
{
display();
break;
}

case 5:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice");
}
}
while(1);
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 peek()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The topmost elements is %d",stack[top]);
}
}
void display()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
{
printf("\n%d",stack[i]);
}
}
}

OUTPUT:
Enter the size of STACK[MAX=100]:10
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12
Enter the Choice:1
Enter a value to be pushed:24
Enter the Choice:1
Enter a value to be pushed:98
Enter the Choice:3
The elements in STACK
98
24
12
Press Next Choice
Enter the Choice:2
The popped elements is 98
Enter the Choice:3
The elements in STACK
24
12
Press Next Choice
Enter the Choice:4
EXIT POINT
}
}

You might also like