pythonai
pythonai
read(calendar, size):
display(calendar, size);
freeMemory(calendar, size);
free(calendar);
return 0;}
3. Develop a menu driven Program in Cfor the following operations on STACK of Integers (Array
Implementation of Stack with maximum size MAX) 1. Push an Element on to Stack 2. Pop an Element
from Stack 3. Demonstrate how Stack can be used to check Palindrome 4. Demonstrate Overflow
and Underflow situations on Stack 5. Display the status of Stack 6. Exit Support the program with a
#include <stdlib.h>
#define MAX 100
stack[++top] =element ;
int pop() (
if (top = -1) {
printt"Stack Underflow\n");
return l ,
)
returnstack[top- -];
void display() t
1)
int f("Stack is empty\n");
return:
}
printf("Stack elements are : \n");
for (int i = top; i >= e; i--) f
printf("%d\n", stack[i]);
int isPalindrome(charstr[) {
int i = 0:
if (str[il != stack[il) {
return 0:
}
itt;
j--;
return 1:
int main() {
int choice, element;
char str[MAX];
while (1) {
printf("\nStack Operations : \n");
printf("1. Push\n") ;
printf("2. Pop\n");
printfC3. Check Palindrome \n");
Drintf("5. Exit\n"):
printf("Enter your choice: ");
scanf ("%d", &choice) ;
Switch (choice)
element = pop();
1f (element != -1)
Aisnlay();
break;
case 5:
exit(0):;
default:
printf("Invalid choice\n");
return 0;
Program 4: Infix to Postfix using Stack.
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should
free parenthesized expressions with the operators: +, , *, I, %
support for both parenthesized and
(Remainder), ^(Power) and alphanumeric operands.
#include<stdio.h>
#include<string.h>
int F(char ch)
switch(ch)
case '+:
case '-'return 2:
case *:
case :
case "%':return 4:
case 'S:
case ^':return 5;
case (':return 0:
case #return -1:
default: return 8;
int main(
int top=-1,j=0,i;
printf(" nEnter the infix statement.In");
scanf("%s",infix):
s[++top]=#*;
fori-0;i<strlen(infix); itt)
while(F(s[top))>G(infix[i])
postix[j++|=s[top--];
if (F(s[top]) !-G(infix(i)
s[++top]=infix(i];
else
top--;
while(s[top]!=#)
postfix[j++-s[top-J;
postfix[j]=0;
printf("nThe postfix expession is %s \n".postfix);
Output:
Enter the infix statement..
a+b"c
The n0st fix
Program 5: Evaluation of Postfix Expression
Develop a Program in C for the following Stack Applications:
a. Evaluate a Suffix-expression with single digit operands and operalors: t, -, *,/, %, ^.
#include<stdio.h>
#include<string.h>
#include<math.b>
int main()
char postfix[20):
int result, i, s[20], op l,op2, top=-1;
printf("nEnter the postfix expression:");
scanf("%s".postfix);
for(i-0;i<strlen(postfix);it+)
if(fisdigit(postfix[i1) // if operand push on stack
s[t+top]-postfix(i]-0';
else
op2-s[top
opl-s[top-):
switch(postfix(i)
case '+':result=opl+op2;
break:
case ':result=opl-op2:
break;
case *:result=oplop2:
break:
case '/:result=op l/op2;
break:
case %':result=op 1%op2;
break:
case ':result=pow(opl,op2):
break:
s[+ttopl=result;
12/35
printf("nResult-%d",result);
Output:
Enter the postfix expression : 235"+
Result=17
#include<stdio.h>
tower(intn, char s, char d, chart)
if(n=-0)
return;
tower(n-l,s,t,d);
printf("n Move Disc %d from %c to oc", n.s.d):
tower(n-1,t,d,s);
int main(0
int n;
printf("nEnter no of discs\n");
scanf("%d",&n);
tower(n,'A,C,B);
Output:
Enter no of discs
if(count==MAX)
printf("\n Queue is Full");
else
rear=(rear+1)% MAX:
q[rear]=item;
count++;
void del()
if(count==0)
printf("n Queue is Empty");
else
if(front>rear& &rear=-MAX-1)
front=0;
rear=-1;
count=0;
)
else
item=q[front);
printf("n Deleted item is:%c",item):
front=(front+1)% MAX;
count--;
void display()
int i. f=front, r=rear;
if(count==0)
printf("n Queue is Empty"):
else
printf("% ct".q[i):
f-(+1)% MAX;
void main()
do
void dfs(int u)
int v,i;
s[u]=1;
printf("%d",u):
for(v=0;v<n;v++)
void main(
int ij,source:
printf("Enter no of nodes in the graph \n");
scanf("%d",&n):
printf("Enter adjacency matrix n");
for(i-0;i<n;i++)
for(j-0;j<nj++)
scanf("%d",&a[ilil):
printf("Enter source node in the graph \n");
scanf("%d",&source);
printf("n The nodes reachable from %d:",source);
dfs(source):
printf("n"):
Output:
0 0 1
3 3
The nodes reachable from 1:1023 The nodes reachable from 2:23
PROGRAM 12:
Given a File of N employee records with a set K of Keys (4-digit) which uniquely
determine the records in fle F. Assume that file F is maintained in memory by
a Hash Table (HT) of m memory locations with L as the set of memory
addresses (2-digit) of locations in HT.
Let the keys in Kand addresses in L are Integers. Developa Program in C that
uses Hash function H: K Las H(K=K mod m (remainder method), and
implement hashing technigue to map a given key K to the address
Resolve the collision (if any) using linear probing.
#include<stdio.h>
#include<stdlib.h>
int kevl20], n, m:
int ht., index:
int count =0:
void insert(int key)
|67
index = key % m;
while (ht[index] !=-1)
index = (index + 1) % m;
ht[index] =key:
count++:
void display()
int i:
f (count =0)
int i;
printf("nEnter the number of employee records (N): "):
scanf("%d", &n): printf("nEnter the two digit memory locations (m) for hash
table: "):
scanf("%d", & m): ht = (int ) malloc(msizeof(int);
for (i = 0; i < m: i++)
=-J:
printf("nEnter the four digit key values (K) for N Employee Records:n "):
for (i = 0; i <n; i++)
scanf("%d", & keyli):
for (i = 0; i <n; it+)
if (count= m)
| 68
insert(keyli):
display):
QUTPUT
Enter the number of employee records (N) :10
Enter the two digit memory locations (m) for hash table:15
Enter the four digit key values (K) for NEmployee Records:
4020
4560
9908
6785
0423
7890
6547
3342
9043
6754
Hash Table contents are:
T[O] --> 4020
TI!] ->4560
T[2]--> 7890
TI3)-- 423
T[4] --> 6754
T[5] --> 6785 68-69/69
T[6] -> -1
T[7] --> 6547
TI8] --> 9908
T[9] -->1
T[10] -->-1
T[11] -->1
T[12] --> 3342
T[13] -> 043
T[14] --> -1