0% found this document useful (0 votes)
30 views5 pages

DSA

The document describes a C program that implements a menu-driven stack using an array. The menu allows the user to push elements onto the stack, pop elements off the stack, check if the stack is a palindrome, and display the status of the stack. Functions are defined to support each operation on the stack, including push(), pop(), palindrome(), and display(). The main() function contains a switch menu to call the appropriate function based on the user's selection.

Uploaded by

Karthik Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

DSA

The document describes a C program that implements a menu-driven stack using an array. The menu allows the user to push elements onto the stack, pop elements off the stack, check if the stack is a palindrome, and display the status of the stack. Functions are defined to support each operation on the stack, including push(), pop(), palindrome(), and display(). The main() function contains a switch menu to call the appropriate function based on the user's selection.

Uploaded by

Karthik Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

3.

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.

Support the program with appropriate functions for each of the above operations.

#include<stdio.h>
#include<conio.h>

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

/*PUSH FUNCTION*/
void push(int stack[], int item)
{
if (top == (MAX-1))
printf("\n\nStack is Overflow");
else
{
stack[++top] = item;
status++;
}
}
/*POP FUNCTION*/
int pop(int stack[])
{
int ret;
if(top == -1)
printf("\n\nStack is Underflow");
else
{
ret = stack[top--];
status--;
printf("\nPopped element is %d", ret);
}
return ret;
}
/* FUNCTION TO CHECK STACK IS PALINDROME OR NOT */
void palindrome(int stack[])
{
int i, temp;
temp = status;
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");
}

/*FUNCTION TO DISPLAY STACK*/


void display(int stack[])
{
int i;
printf("\nThe stack contents are:");
if(top == -1)
printf("\nStack is Empty");
else
{
for(i=top; i>=0; i--)
printf("\n ------\n| %d |", stack[i]);
printf("\n");
}
}
/*MAIN PROGRAM*/
void main()
{
clrscr();
do{
printf("\n\n----MAIN MENU --------------- \n");
printf("\n1. PUSH (Insert) in the Stack\");
printf("\n2. POP (Delete) from the Stack");
printf("\n3. PALINDROME check using Stack");
printf("\n4. Exit (End the Execution)");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nEnter a element to be pushed: ");
scanf("%d", &item);
push(stack, item);
display(stack);
break;

case 2:
item=pop(stack);
display(stack);
break;

case 3:
palindrome(stack);
break;
case 4:
exit(0);
break;
default:
printf("\nEND OF EXECUTION");
break;
}//end switch
}while (ch != 4);
getch();
}

SAMPLE OUTPUT:
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)

Enter Your Choice: 1


Enter an element to be pushed: 1
The stack contents are:
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)

Enter Your Choice: 1


Enter an element to be pushed: 2
The stack contents are:
|2|
|1|
(-------- AFTER THE 4 TIMES PUSH OPERATION )
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)

Enter Your Choice: 1


Enter an element to be pushed: 2
Stack is Overflow
The stack contents are:
|1|
|2|
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter 2
Your
Choice:
Popped 1
element
is
The
stack
contents
are:
|2|
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)

Enter Your Choice: 1


Enter an element to be pushed: 2
The stack contents are:
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter a element to be pushed: 1
The stack contents are:
|1|
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)

Enter Your Choice: 3


Stack contents are is Palindrome
(-------- CHECKING FOR PALINDROME OR NOT USING STACK )
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
|1|
(AFTER 3 TIMES PUSH)
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)

Enter Your Choice: 1


Enter an element to be pushed: 3
The stack contents are:
|3|
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)

Enter Your Choice: 3


Stack contents are not Palindrome

You might also like