You are on page 1of 6

DATA STRUCTURES WITH C LABORATORY CODE-18CSL38

LAB PROGRAM :3
3Q) Design, Develop and Implement 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.
#include<stdio.h>
#include<conio.h>

#define MAX 4
int stack[MAX], item,ch, top = -1, count = 0, status =
0;

/*Start of push function*/


void push(int stack[], int item)
{
if (top == (MAX-1))
printf("\n\n***Stack Overflow***");
else
{
stack[++top] = item;
status++;
}
}/*End of push function*/

/*Start of pop function*/


int pop(int stack[])
{
int ele;

ISE,RYMEC Page 1
DATA STRUCTURES WITH C LABORATORY CODE-18CSL38

if(top == -1)
printf("\n\n***Stack Underflow***");
else
{
ele = stack[top--];
status--;
printf("\nPopped element is %d", ele);
}
return ele;
}/*End of pop function*/

/* Function to check stack elements are palindrome of


not*/
void palindrome(int stack[])
{
int i, temp;
temp = status;
if(temp>0)
{
for(i=0; i<temp; i++)
{
if(stack[i] == pop(stack))
count++;
}
if(temp==count)
printf("\nStack contents are Palindrome");
else
printf("\nStack contents are not palindrome");
}
else
printf("\n***No Elements in stack to check for
palindrome***\n");
}/*End of palindrome function*/

/*function to display the content of stack*/

ISE,RYMEC Page 2
DATA STRUCTURES WITH C LABORATORY CODE-18CSL38

void display(int stack[])


{
int i;

if(top == -1)
printf("\nOOPS!!!!Stack is Empty");
else
{
printf("\nThe stack contents are:");
for(i=top; i>=0; i--)
printf("\n ----\n| %d |", stack[i]);
printf("\n");
}
} /*End of Display function*/

/*start of main function*/


void main()
{
clrscr();
for(;;)
{
printf("\n\n---------MAIN MENU---------\n");
printf("\n1. PUSH \n2. POP \n3. DISPLAY\n4.
PALINDROME \n5. Exit");
printf("\n---------------------------\n");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter single element to
be pushed: ");
scanf("%d", &item);
push(stack,item);
display(stack);
break;

ISE,RYMEC Page 3
DATA STRUCTURES WITH C LABORATORY CODE-18CSL38

case 2: pop(stack);
break;

case 3:display(stack);
break;

case 4:palindrome(stack);
break;

case 5:exit(0);
break;

default: printf("\n Invalid choice \n");


break;

}/*End of switch*/
}/*End of for*/
}/*End of main function*/
OUTPUT ---------------------------

---------MAIN MENU--------- Enter Your Choice: 1

1. PUSH Enter single element to be pushed: 3


2. POP
3. DISPLAY The stack contents are:
4. PALINDROME ----
5. Exit |3 |
--------------------------- ----
|2 |
Enter Your Choice: 1

Enter single element to be pushed: 2 ---------MAIN MENU---------

The stack contents are: 1. PUSH


---- 2. POP
|2 | 3. DISPLAY
4. PALINDROME
5. Exit
---------MAIN MENU--------- ---------------------------

ISE,RYMEC Page 4
DATA STRUCTURES WITH C LABORATORY CODE-18CSL38

1. PUSH Enter Your Choice: 1


2. POP Enter single element to be pushed: 2
3. DISPLAY
4. PALINDROME
5. Exit
Popped element is 2
Popped element is 3
The stack contents are: Popped element is 2
---- Stack contents are Palindrome
|2 |
---- ---------MAIN MENU---------
|3 |
---- 1. PUSH
|2 | 2. POP
3. DISPLAY
4. PALINDROME
---------MAIN MENU--------- 5. Exit
---------------------------
1. PUSH
2. POP Enter Your Choice: 3
3. DISPLAY
4. PALINDROME OOPS!!!!Stack is Empty
5. Exit
--------------------------- ---------MAIN MENU---------

Enter Your Choice: 3 1. PUSH


2. POP
The stack contents are: 3. DISPLAY
---- 4. PALINDROME
|2 | 5. Exit
---- ---------------------------
|3 |
---- Enter Your Choice: 2
|2 |

***Stack Underflow***
---------MAIN MENU---------
---------MAIN MENU---------
1. PUSH
2. POP 1. PUSH
3. DISPLAY 2. POP
4. PALINDROME 3. DISPLAY
5. Exit 4. PALINDROME
--------------------------- 5. Exit
Enter Your Choice: 4 ---------------------------

ISE,RYMEC Page 5
DATA STRUCTURES WITH C LABORATORY CODE-18CSL38

Enter Your Choice:5

ISE,RYMEC Page 6

You might also like