0% found this document useful (0 votes)
25 views11 pages

DSA 3rd Program

The document describes a C program that implements a stack of integers using an array with a maximum size of 3. It provides a menu-driven interface for operations such as pushing and popping elements, checking for palindromes, demonstrating overflow and underflow, and displaying the stack status. The program includes functions for each operation and handles user input to perform the desired actions.
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)
25 views11 pages

DSA 3rd Program

The document describes a C program that implements a stack of integers using an array with a maximum size of 3. It provides a menu-driven interface for operations such as pushing and popping elements, checking for palindromes, demonstrating overflow and underflow, and displaying the stack status. The program includes functions for each operation and handles user input to perform the desired actions.
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

Program 3

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

Code:

#include<stdio.h>

#include<stdlib.h>

#define MAX 3

int s[MAX];
int top = -1;

void push(int item);


int pop();
void palindrome();
void display();

void main()
{
int choice, item;
while (1)
{
printf("\n\n\n\n-----------Menu----------- : ");
printf("\n=>[Link] an Element to Stack and Overflow
demo ");
printf("\n=>[Link] an Element from Stack and
Underflow demo");
printf("\n=>[Link] demo ");
printf("\n=>[Link] ");
printf("\n=>[Link]");
printf("\nEnter your choice: ");
scanf("%d", & choice);
switch (choice)
{
case 1:
printf("\nEnter an element to be pushed: ");
scanf("%d", & item);
push(item);
break;
case 2:
item = pop();
if (item != -1)
printf("\nElement popped is: %d", item);
break;
case 3:
palindrome();
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("\nPlease enter valid choice ");
break;
}
}
}

void push(int item)


{
if (top == MAX - 1)
{
printf("\n-----------Stack overflow-----------");
return;
}

top = top + 1;
s[top] = item;
}

int pop()
{
int item;
if (top == -1)
{
printf("\n-----------Stack underflow-----------");
return -1;
}
item = s[top];
top = top - 1;
return item;
}

void display()
{
int i;
if (top == -1)
{
printf("\n-----------Stack is empty-----------");
return;
}
printf("\nStack elements are:\n ");
for (i = top; i >= 0; i--)
printf("| %d |\n", s[i]);
}

void palindrome()
{
int flag = 1, i;
printf("\nStack content are:\n");
for (i = top; i >= 0; i--)
printf("| %d |\n", s[i]);

printf("\nReverse of stack content are:\n");


for (i = 0; i <= top; i++)
printf("| %d |\n", s[i]);
for (i = 0; i <= top / 2; i++)
{
if (s[i] != s[top - i])
{
flag = 0;
break;
}
}
if (flag == 1)
{
printf("\nIt is palindrome number");
}
else
{
printf("\nIt is not a palindrome number");
}
}

OUTPUT:

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 1


Enter an element to be pushed: 11

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]
Enter your choice: 1
Enter an element to be pushed: 12

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 1


Enter an element to be pushed: 13

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 1


Enter an element to be pushed: 14
-----------Stack overflow-----------

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 4


Stack elements are:
| 13 |
| 12 |
| 11 |
-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 2


Element popped is: 13

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 4


Stack elements are:
| 12 |
| 11 |

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 2


Element popped is: 12
-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 2


Element popped is: 11

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 2


-----------Stack underflow-----------

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 4


-----------Stack is empty-----------

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 1


Enter an element to be pushed: 11

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 1


Enter an element to be pushed: 22

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 1


Enter an element to be pushed: 11

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 3


Stack content are:
| 11 |
| 22 |
| 11 |

Reverse of stack content are:


| 11 |
| 22 |
| 11 |

It is palindrome number

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 2


Element popped is: 11

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 2


Element popped is: 22

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 1


Enter an element to be pushed: 33

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 1


Enter an element to be pushed: 22

-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 3


Stack content are:
| 22 |
| 33 |
| 11 |

Reverse of stack content are:


| 11 |
| 33 |
| 22 |

It is not a palindrome number


-----------Menu----------- :
=>[Link] an Element to Stack and Overflow demo
=>[Link] an Element from Stack and Underflow demo
=>[Link] demo
=>[Link]
=>[Link]

Enter your choice: 5

You might also like