Expt 2 Stack using Array
Expt 2 Stack using Array
_____________________________________________________________________________________
Rizvi
RIZVI EDUCATION SOCIETY’S
College of Engineering
Approved by AICTE Recognized by DTE Affiliated to University of Mumbai Accredited B+ by NAAC
New Rizvi Educational Complex, Off Carter Rd, Bandra West, Mumbai-400050
Experiment No. 2
UIN 241A017
Criteria Points
Correctness of the program
Clarity of documentation
Delivery
Total
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Theory: A stack data structure can be implemented using a one-dimensional array. But a stack
implemented using arrays stores only a fixed number of data values. This implementation is very simple.
Just define a one dimensional array of specific size and insert or delete the values into that array by using
LIFO principle with the help of a variable called 'top'. Initially, the top is set to -1. Whenever we want to
insert a value into the stack, increment the top value by one and then insert. Whenever we want to delete a
value from the stack, then delete the top value and decrement the top value by one.
Algorithm:
Creation of Stack
● Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
● Step 2 - Declare all the functions used in stack implementation.
● Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
● Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1)
● Step 5 - In main method, display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Program:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
int stack[SIZE];
int top = -1;
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
void main()
{
int choice,value;
while(1)
{
printf("\n\t\tMenu\t\t\n");
printf("Select from the given options\n");
printf("Option 1: Insert an element\nOption 2: Delete an element\nOption 3: Display array.\nOption
4: Exit the program.\n");
scanf("%d",&choice);
switch(choice)
{ case 1: printf("Enter
value to insert: ");
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4: printf("\nExiting
the program"); exit(0);
default:
printf("\nInvalid option!!!Choose from the given options.\n");
break;
}
}
}
void push(int value)
{
if (top == SIZE-1)
{
printf("\nStack is FULL.Insertion is not possible\n");
}
else
{
top = top+1;
stack[top]=value;
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
void pop()
{ if(top ==
-1)
{
printf("\nStack is EMPTY\n");
}
else
{
printf("\nDeleted :%d",stack[top]);
top = top-1;
}
}
void display()
{
int i;
if(top == -1)
{
printf("\nStack is EMPTY\n");
}
else
{
printf("\nArray elements\n");
for(i=top;i>=0;i--)
{
printf("%d\t",stack[i]);
}
}
}
Output:
Rizvi College of Engineering, Mumbai
_____________________________________________________________________________________
Conclusion:
COs Covered: