0% found this document useful (0 votes)
3 views3 pages

C Stack Operations and Palindrome Check

The document contains a C program that implements a stack data structure with operations such as push, pop, display, and a function to check if a number is a palindrome. It uses an array to store stack elements and includes error handling for stack overflow and underflow. The main function provides a menu-driven interface for users to interact with the stack operations.

Uploaded by

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

C Stack Operations and Palindrome Check

The document contains a C program that implements a stack data structure with operations such as push, pop, display, and a function to check if a number is a palindrome. It uses an array to store stack elements and includes error handling for stack overflow and underflow. The main function provides a menu-driven interface for users to interact with the stack operations.

Uploaded by

Pratibha S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include<stdlib.h>
#include<math.h>
# define maxsize 5

int stack[maxsize];
int top=-1;

void push(int item)


{
if(top==maxsize-1)
printf("\n Stack overflow");
else
stack[++top]=item;
}

int pop()
{

if(top==-1)
{
printf("\n Stack underflow");
return -1;
}
else
return stack[top--];
}

void display()
{
int i;
if(top==-1)
printf("\nStack Underflow");
else
{
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
}
}

void palindrome()
{
int num,i,rem,rev,temp;;
printf("\n Enter a number");
scanf("%d",&num);
temp=num;
while(num!=0)
{
rem=num%10;
push(rem);
num=num/10;
}
rev=0, i=0;
while (top!=-1)
{
rev=rev+pop()*pow(10,i);
i=i+1;
}
if(temp==rev)
printf("\n No is palindrome");
else
printf("\n No is not a plaidrome");
}

void main()
{
int choice, item;
while(1)
{
printf("\n STACK OPERATIONS\n");
printf("\n 1. PUSH");
printf("\n [Link]");
printf("\n [Link]");
printf("\n [Link]");
printf("\n [Link]");
printf("\n \nEnter ypur choice");
scanf("%d", &choice);

switch(choice)
{
case 1: printf("Enter element to be pushed");
scanf("%d", &item);
push(item);
break;
case 2: printf("\n Popped element is %d", pop());
break;
case 3: display();
break;
case 4: palindrome();
break;
case 5: exit(0);

default: printf(“Invalid choice”);


} //switch end
}//while end
}//main end

You might also like