0% found this document useful (0 votes)
3 views

pythonai

The document contains multiple C programming examples covering various data structures and algorithms, including stack operations, infix to postfix conversion, evaluation of postfix expressions, and graph traversal using DFS. It also includes a hash table implementation with collision resolution through linear probing. Each section provides code snippets and expected outputs for different functionalities.

Uploaded by

Dinesh P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

pythonai

The document contains multiple C programming examples covering various data structures and algorithms, including stack operations, infix to postfix conversion, evaluation of postfix expressions, and graph traversal using DFS. It also includes a hash table implementation with collision resolution through linear probing. Each section provides code snippets and expected outputs for different functionalities.

Uploaded by

Dinesh P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

void read(struct Day *calendar, int size) {

for (int i=0; i <size; i++){


printf("Enter details for Day %d:\n", i+ 1);
create(&calendar();

void display(struct Day *calendar, int size) {


printf("\nWeek's Activity Details:\n");
for (int i=0; i <size; i++) {
printf("Day %d:\n", i+ 1);
printf("Day Name: %s\n", calendar[i].day Name);
printf("Date: %d\n", calendar[i].date):
printf("Activity: %s\n", calendar[i].activity);
printf("\n");
}
void freeMemory(struct Day *calendar, int size) {
for (int i=0; i< size; i++) {
free(calendar[i].dayName);
free(calendar[i].activity);
int main() {
int size;
printf("Enter the number of days in the week: "):
scanf("% d",&size); struct Day *calendar =(struct Day *)malloc{sizeof(struct Day) *size); if
(calendar ==NULL) {
printf("Memory allocation failed. Exiting program.\n");
return l;

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

JOIN OUR NAMMA VTU COMMUNITY CLICK HERE TO JOIN

ppropriate functions for each of the above operations charges

#include <stdio. h>

#include <stdlib.h>
#define MAX 100

int stack [MAX];


int top = -1;

void push(int element) {


if (top == MAX- 1) {
printf("Stack Overflow\n") ;
return;

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)

printf("Enter element to push: ");


scanf("%d,&element);
push(element);
break;
case 2:

JOIN OUR NAMMA VTU COMMUNITY CLICK HERE TO JOIN

element = pop();
1f (element != -1)

printf(" Popped element: %d\n", element );


break;
case 3:
pranttCEnter a string: ");
if (isPalindrome (str))
printf(" The entered string is a palindrome. \n");
elst
printf(" The entered string is not a palindrome. \n");
break
Cast

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 G(char ch)


10/35
switch(ch)
case '+.
case'-":return 1;
case :
case ' :
case "%':return 3;
case 'S':
case ^:return 6:
case (:return 9;
case ")':return 0;
default: return 7:

int main(

char s[20],infix[ 100,postfix(1001:

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

b. Solving Tower of Hanoi problem with n disks.

#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

Move Disc 1 from A to C


Move Disc 2 from A to B
Move Disc 1 from C to B
Move Disc 3 from A to C
Move Disc 1 from B to A
Move Disc 2 from B to C
Move Disc 1 from A to C
SOLUTON 2
#includeKstdio.h>
#includeKconio.h>
#define MAX 4
int ch, front-=0,rear=l,count=0;
char q[MAX], item;
void insert()

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

DEPARTMENT OF cS&E. SVCE 68

DATA STRUCTURES LABORATORY 18CSL38

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("\n Contents of Queue is:\n"):


for(i=f;i<=r;i++)

printf("% ct".q[i):
f-(+1)% MAX;

void main()
do

printf("n l:Insert\n 2:Delete\n 3:Displayin 4:Exit\n");


printf("\n Enter the choice:"):
scanf("%d",&ch);
switch(ch)

case 1: printf("\n Enter the character to be inserted:"):


scanf("%c",&item):
insert():
break;
case 2: del():
break;
case 3: display():
break;
case 4: exit(0):
break:
J}while(ch!=4);
11. DES Method
Design, Develop and Implement a Program in C for the following operations on Graph(G) of Cities.
a. Create a Graph ofN cities using Adjacency Matrix.
b. Print allthe nodes reachable from a given starting node in a digraph using DFS/BFS method.
#include<stdio.h>
int a[10][10],n,s[10]:

void dfs(int u)

int v,i;
s[u]=1;
printf("%d",u):
for(v=0;v<n;v++)

if(a[u][v]==1 && slv]--0)


dfs(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

Enter no of nodes in the graph Enter no ofnodes in the graph

Enter adjacency matrix Enter adjacency matrix


A1 1
1 01
1 1
9 1 1e
Enter source node in the graph Enter source node in the graph

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

DATA STRUCTURES AND APPLICATIONS LABORATORY(BCSL305)

index = key % m;
while (ht[index] !=-1)
index = (index + 1) % m;

ht[index] =key:
count++:

void display()
int i:
f (count =0)

printf("nHash Table is empty"):


return;

printf("nHash Table contents are:jn "):


for (i = 0; i< m; i++)
printf("n T[%d] --> %d". i, htli):
void main()

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)

printf("n--..-.-Hash table is full. Cannot insert the record %d key-.", i+ D):


break:

| 68

DATA STRUCTURES AND APPLICATIONS LABORATORY(BCSL305)

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

You might also like