3_6 programs
3_6 programs
3_6 programs
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
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<math.h>
#define MAX 5
int top=-1, a[MAX];
int power(int, int);
void push(int);
int pop(void);
void display(void);
void palindrome(int);
void main()
{
int ch,item,num,itemdel;
while(1)
{
printf("\n Stack Operations Menu\n");
printf("1.Push\n2.Pop\n3.Display\n4.Palindrome\n5.Exit\n");
printf("enter your choice[1/2/3/4/5]:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter item to be inserted:");
scanf("%d",&item);
push(item);
break;
case 2: itemdel=pop();
if(itemdel)
printf("Deleted item :%d\n",itemdel);
else
printf("Stack overflow");
break;
case 3: display();
break;
case 4: printf("Enter any num:\n");
scanf("%d",&num);
palindrome(num);
break;
case 5:exit(0);
}
}}
int power(int x,int n)
{
if(n==0)
return 1;
return(x*power(x,n-1));
}
void push(int item)
{
if(top==MAX-1)
printf("stack overflow\n");
else
{
top=top+1;
a[top]=item;
}}
int pop(void)
{
int itemdel;
if(top==-1)
return 0;
else{
itemdel=a[top];
top=top-1;
return itemdel;
}
}
void display(void)
{
int i;
if(top==-1)
printf("Stack is Empty\n");
else{
printf("Elements of stack are:\n");
for(i=top;i>=0;i--)
printf("%d\n",a[i]);
}}
void palindrome(int num)
{
int count=0,rem,i,rev=0,n=num,item, m=1;
while(n!=0)
{
rem=n%10;
push(rem);
n=n/10;
count++;
}
for(i=0;i<count;i++){
item=pop();
Without recursion code
rev=item*power(10,i)+rev; OR rev=item*m+rev;
m*=10;
}
printf("Reversed Number is =%d\n",rev);
if(num==rev)
printf("It is palindrome");
else
printf("Not a palindrome");
}
6) Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
char circularQueue[MAX];
int front = -1, rear = -1;
// Function prototypes
void insertElement(char element);
void deleteElement();
void displayQueue();
void checkOverflow();
void checkUnderflow();
int main() {
int choice;
char element;
do
{
printf("\nCircular Queue Operations Menu\n");
printf("1. Insert Element\n");
printf("2. Delete Element\n");
printf("3. Display Queue\n");
printf("4. Check Overflow\n");
printf("5. Check Underflow\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to insert: ");
scanf(" %s", &element);
insertElement(element);
break;
case 2:
deleteElement();
break;
case 3:
displayQueue();
break;
case 4:
checkOverflow();
break;
case 5:
checkUnderflow();
break;
case 6:
printf("Exiting the program. Goodbye!\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while(choice!=6);
return 0;
}
void insertElement(char element)
{
if ((front == 0 && rear == MAX - 1) || (rear == (front - 1) % (MAX - 1))) {
printf("Queue is full. Overflow!\n");
return;
} else if (front == -1) {
front = rear = 0;
} else if (rear == MAX - 1 && front != 0) {
rear = 0;
} else {
rear++;
}
circularQueue[rear] = element;
printf("Element '%c' inserted successfully.\n", element);
}
void deleteElement()
{
if (front == -1) {
printf("Queue is empty. Underflow! \n");
return;
}
printf("Deleted element: %c\n", circularQueue[front]);
if (front == rear)
{
front = rear = -1;
} else if (front == MAX - 1) {
front = 0;
} else {
front++;
}
}
void displayQueue()
{
int i;
if (front == -1) {
printf("Queue is empty.\n");
return;
}
printf("Circular Queue: ");
if (rear >= front) {
for (i = front; i <= rear; i++)
printf("%c ", circularQueue[i]);
} else {
for (i = front; i < MAX; i++)
printf("%c ", circularQueue[i]);
for(i=0; i<=rear;i++)
printf("%c ", circularQueue[i]);
}
printf("\n");
}
void checkOverflow()
{
if ((front == 0 && rear == MAX - 1) || (rear == (front - 1) % (MAX - 1)))
{
printf("Queue is full. Overflow! \n");
} else {
printf("Queue is not full. No Overflow.\n");
}
}
void checkUnderflow()
{
if (front == -1) {
printf("Queue is empty. Underflow!\n");
} else {
printf("Queue is not empty. No Underflow.\n");
}
}