Lab 3
Lab 3
Lab 3
VANDANA
DEPT OF AI/DS
PROGRAM STATEMENT
3) Develop a menu driven Program in C for the following operations on STACK of Integers
(Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations.
INTRODUCTION TO STACK:
A stack is an abstract data type (ADT), commonly used in most programming languages.
It is named stack as it behaves like a real-world stack. A real-world stack allows
operations at one end only. For example, we can place or remove a card or plate from
top of the stack only. Likewise, Stack ADT allows all data operations at one end only. At
any given time, we can only access the top element of a stack. This feature makes it
LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed
(inserted or added) last is accessed first. In stack terminology, insertion operation is
called PUSH operation and removal operation is called POP operation. A stack can be
implemented by means of Array, Structure, Pointer and Linked-List. Stack can either be
a fixed size one or it may have a sense of dynamic resizing. Here, we are going to
implement stack using arrays which makes it a fixed size stack implementation
Basic Operations:
push() - pushing (storing) an element on the stack.
pop() -removing (accessing) an element from the stack. To use a stack efficiently
we need to check status of stack as well. For the same purpose, the following
functionality is added to stacks;
peek() − get the top data element of the stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty
Algorithm:
Step 1: Start.
Step 2: Initialize stack size MAX and top of stack -1.
Step 3: Push integer element on to stack and display the contents of the stack. if
stack is full give a message as “Stack is Overflow “
Step 4: Pop element from stack along with display the stack contents. if stack is
empty give a message as “Stack is Underflow”
Step 5: Check whether the stack contents are Palindrome or not.
Step 6: Stop
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size],top=-1,flag=1;
int i,temp,item,rev[max_size],num[max_size];
void push();
void pop();
void display();
void pali(); Program Code:
int main() case 1: push();
{ break;
int choice; case 2: pop();
printf("\n\n--------STACK if(flag)
OPERATIONS--------\n"); printf("\nThe poped element: %d\t",item);
printf("1.Push\n"); temp=top;
printf("2.Pop\n"); break;
printf("3.Palindrome\n"); case 3: pali();
printf("4.Display\n"); top=temp;
printf("5.Exit\n"); break;
printf(" "); case 4: display();
while(1) break;
{ case 5: exit(0);
printf("\nEnter your choice:\t"); break;
scanf("%d",&choice); default: printf("\nInvalid choice:\n");
switch(choice) break;
{ }}}
void push() //Inserting element into the stack
{
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1; Increase top by 1 to increase the top by 1 and
stack[top]=item; then in that index place the element(item)
}
temp=top;
} Program Code:
void pop() //deleting an element from the stack
{
if(top==-1)
{
printf("Stack Underflow:");
flag=0; Store the element that needs to
} be popped in a temporary
else variable called “item”.
{
Decreasing the top by 1 , which
item=stack[top];
deletes the element from the
top=top-1;
stack
}
}
Program Code:
void pali() for(i=0;i<=temp;i++) Check for Palindrome: The
{ i=0; { function then iterates
if(top==-1) if(stack[top--]==rev[i]) through the original stack
{ { and the reversed array (rev).
printf("Push some elements into the if(i==temp) If the elements match, it
continues checking. If all
stack first\n"); {
elements match, it prints
} printf("Palindrome\n"); "Palindrome" and returns
else return; from the function. Otherwise,
{ }}} it prints "Not Palindrome".
while(top!=-1) printf("Not Palindrome\n");
{ }}
rev[top]=stack[top]; Reverse the stack: If the stack is not empty, the function enters
pop(); the else block and reverses the elements in the stack. It does this
} by copying elements from the original stack to another array called
top=temp; rev while popping elements off the original stack.
After reversing, the top variable is set to temp to restore the
stack to its original state.
void display()
{
int i; top=temp;
if(top==-1)
{
printf("\nStack is Empty:"); Since the last element has to be
} printed first, the loop starts at top
else and ends at 0
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
Program Code:
}
}
Output:
Run the code : Use option: 1 and insert any 4 elements of your choice,
then use option 2: to pop the element, use option 4: to display the
contents of your stack and lastly check option:3 to check for
palindrome.
THANK YOU