Program 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

/* Lab Program 3

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 onto the 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.

*/

// Header files

#include<stdio.h>

#include<string.h>

#define MAX_SIZE 6

//Prototypes

void push(int[],int*,int);

int pop(int[],int*);

void display(int[],int);

int palindrome(int[],int*,char[]);

//Main function

int main()

int stack[MAX_SIZE],ele,deleted_item,top=-1,flag=-1,done=0,choice;
char palstr[MAX_SIZE];

while(!done)

printf("\nSTACK OPERATIONS USING ARRAY");

printf("\n1.Push\n2.Pop\n3.Palindrome\n4.Exit\n");

printf("Enter the choice:\n");

scanf("%d",&choice);

switch(choice)

case 1: printf("Enter the element to be pushed\n");

scanf("%d",&ele);

push(stack,&top,ele);

display(stack,top);

break;

case 2:deleted_item=pop(stack,&top);

if(deleted_item!=-1)

printf("Deleted Item is: =%d\n",deleted_item);

display(stack,top);

break;

case 3:printf("Enter the String\n");

scanf("%s",palstr);

top=-1;

flag=palindrome(stack,&top,palstr);

if(flag==1)
printf("%s is a palindrome\n",palstr);

else

printf("%s is not a palindrom\n",palstr);

top=-1;

break;

case 4:done=1;

break;

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

return 0;

// Push function definition

void push(int stack[],int *top,int ele)

if(*top==MAX_SIZE-1)

printf("Stack Overflow\n");

else

++(*top);

stack[*top]=ele;

}
return;

//Pop function definition

int pop(int stack[],int*top)

int deleted_item;

if(*top==-1)

printf("Stack Underflow\n");

return -1;

else

deleted_item=stack[*top];

(*top)--;

return deleted_item;

//Display function definition

void display(int stack[],int top)

int i;

if(top==-1)

printf("Stack is Empty\n");
else

printf("Stack Elements are\n");

for(i=top;i>=0;i--)

printf("%d\t",stack[i]);

return;

//Palindrome function definition

int palindrome(int stack[],int *top,char pal[])

int i,p;

for(i=0;i<strlen(pal);i++)

push(stack,top,pal[i]);

for(i=0;i<strlen(pal);i++)

if(pal[i]!=pop(stack,top))

return -1;

return 1;

/* OUTPUT

[root@localhost 2022batchDSLab]# cc 3_Stack.c

[root@localhost 2022batchDSLab]# ./a.out


STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the element to be pushed

11

Stack Elements are

11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the element to be pushed

22

Stack Elements are

22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:


1

Enter the element to be pushed

33

Stack Elements are

33 22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the element to be pushed

44

Stack Elements are

44 33 22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the element to be pushed

55

Stack Elements are

55 44 33 22 11

STACK OPERATIONS USING ARRAY


1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the element to be pushed

66

Stack Elements are

66 55 44 33 22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the element to be pushed

77

Stack Overflow

Stack Elements are

66 55 44 33 22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

2
Deleted Item is: =66

Stack Elements are

55 44 33 22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the element to be pushed

66

Stack Elements are

66 55 44 33 22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the element to be pushed

77

Stack Overflow

Stack Elements are

66 55 44 33 22 11

STACK OPERATIONS USING ARRAY

1.Push
2.Pop

3.Palindrome

4.Exit

Enter the choice:

Deleted Item is: =66

Stack Elements are

55 44 33 22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Deleted Item is: =55

Stack Elements are

44 33 22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Deleted Item is: =44

Stack Elements are

33 22 11

STACK OPERATIONS USING ARRAY


1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Deleted Item is: =33

Stack Elements are

22 11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Deleted Item is: =22

Stack Elements are

11

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Deleted Item is: =11

Stack is Empty
STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Stack Underflow

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the String

amma

amma is a palindrome

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the String


appa

appa is a palindrome

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

Enter the String

tina

tina is not a palindrom

STACK OPERATIONS USING ARRAY

1.Push

2.Pop

3.Palindrome

4.Exit

Enter the choice:

*/

You might also like