• Home
  • MY TRYOUTS
  • tips
  • tech news
  • PROGRAMS
  • Downloads
  • About
  • Contact

Lab program-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<stdlib.h>
#include<stdio.h>
#define max_size 5

int stack[max_size],top=-1,item,i;

void push(int ele);
void pop();
void display();
void pali();
void main()
{
int choice;
while(choice)
{
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n 2.Pop\n 3.Palindrome\n 4.Display\n 5.Exit\n ");
printf("----------------------------------------------------------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:  printf("Enter the element to be inserted:\t");
    scanf("%d",&item);
    push(item);
    break;
case 2:
    pop();
    break;
case 3:
    pali();
    break;
case 4:
    display();
    break;
case 5:
    exit(0);
default:
    printf("\nInvalid choice:\n");
}
}
}  
//Inserting element into the stack

void push(int ele)
{
    if(top==(max_size-1))
    {
        printf("\nStack Overflow:\n");
    }
    else
    {
        top=top+1;
        stack[top]=ele;
    }
}

//deleting an element from the stack

void pop()
{
    
    if(top== - 1)
    {
        printf("Stack Underflow:");
    }
    else
    {
        item=stack[top];
        top=top-1;
        printf("\nThe poped element: %d\t",item);
    }
}



void pali()
{
int j,k,len=top+1,ind=0;
int num[len],rev[len],i=0;
while(top!=-1)
{
    num[i]= stack[top];
    top--;
    i++;
}
printf("Numbers in Stack are:\n”);
for(j=0;j<len;j++)
{
printf("%d\n",num[j]);
}
//reverse operation
for(k=len-1;k>=0;k--)
{
rev[ind]=num[k];
ind++;
}



printf("reverse Stack : \n");
for(k=0;k<len;k++)
{
printf("%d\n",rev[k]);
}
//check for palindrome
int length = 0;
for(i=0;i<len;i++)
{
if(num[i]==rev[i])
{
length = length+1;
} }
if(length==len)
{
printf("It is palindrome number\n");
}
else
{
printf("It is not a palindrome number\n");
}
top = len-1;
}


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











Powered by Create your own unique website with customizable templates.
  • Home
  • MY TRYOUTS
  • tips
  • tech news
  • PROGRAMS
  • Downloads
  • About
  • Contact