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

Ex No 1 Implementation of Stack Using Array

The document describes a C program to implement a stack using an array. It includes functions to push elements onto the stack, pop elements off the stack, and peek at the top element. The program uses an array of size 100 to represent the stack. It includes a main menu to select push, pop, peek, or exit operations and uses switch statements to call the corresponding functions. The push function checks for overflow before adding an element to the top of the stack. The pop function checks for underflow before removing the top element. The peek function prints the top element or a message if the stack is empty.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
434 views

Ex No 1 Implementation of Stack Using Array

The document describes a C program to implement a stack using an array. It includes functions to push elements onto the stack, pop elements off the stack, and peek at the top element. The program uses an array of size 100 to represent the stack. It includes a main menu to select push, pop, peek, or exit operations and uses switch statements to call the corresponding functions. The push function checks for overflow before adding an element to the top of the stack. The pop function checks for underflow before removing the top element. The peek function prints the top element or a message if the stack is empty.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex No.

1 Implementation of Stack using Array


Date:

Aim
To write a c program to implement stack using arrays.
Algorithm
Step 1: Start.
Step 2: Declare Stack [100]; //Maximum size of Stack 
Step 3: Use switch to select the stack operations “1. PUSH, 2. POP,  
3. PEEK, 4. EXIT”.
Step 4: If the user selects 1 then perform PUSH () operation.
PUSH () operation:
Step 1: Check if the stack is full or not by comparing top 
             with (MAX-1)
If the stack is full, then print "Stack Overflow" i.e.,        
stack is full and cannot be pushed with another   
 element 
Step 2: Else, the stack is not full
Increment top by 1 and set, stack[top] = x 
which pushes the element x into the address pointed  
                     by top.
// The element x is stored in stack[top]
Step 5: If the user selects 2 then perform POP () operation.
POP () operation :
Step 1: Check if the stack is empty or not by comparing top 
with base of array i.e., -1
 If top is less than -1, then stack is empty, print "Stack 
Underflow"
  Step 2: Else, if top is greater than zero the stack is not 
empty, then store the value pointed by top in a 
variable x=stack[top] and decrement top by 1. The popped
element is x.
Step 6: If the user selects 3 then perform PEEK () operation.
PEEK () operation:
Step 1: If top is greater than zero the stack is not 
empty, then. Print the value stored in the stack pointed by 
top. Else print “The STACK is empty” 
Step 7: If the user selects 4 then exit.
Step 8: if the user selects values other than 1,2,3,4 then print the default 
message.
Step 9: Stop.

Implementation
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void peek(void);
int main()
{
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:
{
peek();
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 peek()
{
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

Result
Thus, the C program for Implementation of Stacks Using Arrays was written, executed
and the output was verified successfully.

You might also like