Pr1
#include <stdio.h>
#include <stdlib.h>
struct DAY
{
char *dayname;
int date;
char *activity;
};
void create(struct DAY *day)
{
day->dayname = (char *) malloc(sizeof(char) * 20);
day->activity = (char *) malloc(sizeof(char) * 50);
printf("Enter the name of the day, date for the day, activity for
the day\n");
scanf("%s%d%s", day->dayname,&day->date,day->activity);
}
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[i]);
}
}
void display(struct DAY *calendar, int size)
{
printf("Activity Details:\n");
for (int i = 0; i < size; i++)
printf("%d%s%d%s\n", i +
1,calendar[i].dayname,calendar[i].date,calendar[i].activity);
}
int main()
{
int size;
printf("Enter the number of days in a week\n");
scanf("%d", &size);
struct DAY *calendar = (struct DAY *) malloc(sizeof(struct DAY) *
size);
if (calendar == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
read(calendar, size);
display(calendar, size);
return 0;
}
Pr2
#include <stdio.h>
char str[100], pat[50], rep[50], ans[100];
int i, j, c, m, k, flag = 0;
void stringmatch()
{
i = m = c = j = 0;
while (str[c] != '\0')
{
if (str[m] == pat[i])
{
i++;
m++;
if (pat[i] == '\0')
{
flag = 1;
for (k = 0; rep[k] != '\0'; k++, j++)
ans[j] = rep[k];
i = 0;
c = m;
}
}
else
{
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
ans[j] = '\0';
}
void main()
{
printf("\nEnter a main string & pattern string & replace string\
n");
scanf("%s%s%s",str,pat,rep);
stringmatch();
if (flag == 1)
printf("\nThe resultant string is\n %s", ans);
else
printf("\nPattern string NOT found\n");
}
Pr 3
#include <stdio.h>
#define MAX 4
int s[MAX], item;
int ch, top = -1,status=0;
void push(int stack[], int item)
{
if (top == (MAX - 1))
printf("\nStack is Overflow");
else
{
stack[++top] = item;
status++;
}
}
void pop(int stack[])
{
int itemdel;
if (top == -1)
printf("Stack is Underflow");
else
{
itemdel = stack[top--];
status--;
printf("Popped element is %d\n", itemdel);
}
}
void palindrome(int stack[])
{
int flag = 1, i;
printf("Stack contents are:\n");
for (i = top; i >= 0; i--)
printf("%d\n", s[i]);
printf("Reverse of stack content are :\n");
for (i = 0; i <= top; i++)
printf("%d\n", s[i]);
for (i = 0; i <= top / 2; i++)
{
if (s[i] != s[top - i])
{
flag = 0;
break;
}
}
if (flag == 1)
printf("palindrome");
else
printf("Not a palindrome");
}
void display(int stack[])
{
int i;
if (top == -1)
printf("Stack is Empty");
else
{
printf("stack contents are\n");
for (i = top; i >= 0; i--)
printf("%d\n", stack[i]);
}
}
void main()
{
printf("Enter 2 elements to be pushed:\n");
scanf("%d", &item);
push(s, item);
scanf("%d", &item);
push(s, item);
display(s);
pop(s);
display(s);
printf("Status of Stack =%d Item(s)\n",status);
palindrome(s);
}
Pr4
#include <stdio.h>
#include <string.h>
int stkpre(char symbol)
{
switch (symbol)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
case '$':
return 5;
case '(':
return 0;
case '#':
return -1;
default:
return 8;
}
}
int inpre(char symbol)
{
switch (symbol)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 3;
case '^':
case '$':
return 6;
case '(':
return 9;
case ')':
return 0;
default:
return 7;
}
}
void infix_postfix(char infix[], char postfix[])
{
int top, j, i;
char s[30], symbol;
top = -1;
s[++top] = '#';
j = 0;
for (i = 0; i < strlen(infix); i++)
{
symbol = infix[i];
while (stkpre(s[top]) > inpre(symbol))
{
postfix[j] = s[top--];
j++;
}
if (stkpre(s[top]) != inpre(symbol))
s[++top] = symbol;
else
top--;
}
while (s[top] != '#')
{
postfix[j++] = s[top--];
}
postfix[j] = '\0';
}
void main()
{
char infix[20], postfix[20];
printf("Enter a valid infix expression\n");
scanf("%s",&infix);
infix_postfix(infix, postfix);
printf("The postfix expression is:\n");
printf("%s", postfix);
}
Pr5.A
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
double compute(char symbol, double op1, double op2)
{
switch (symbol)
{
case '+':
return op1 + op2;
case '-':
return op1 - op2;
case '*':
return op1 * op2;
case '/':
return op1 / op2;
case '$':
case '^':
return pow(op1, op2);
default:
return 0;
}
}
void main()
{
double s[20], res, op1, op2;
int top, i;
char postfix[20], symbol;
printf("\nEnter the postfix expression:\n");
scanf("%s",postfix);
top = -1;
for (i = 0; i < strlen(postfix); i++)
{
symbol = postfix[i];
if (isdigit(symbol))
s[++top] = symbol - '0';
else
{
op2 = s[top--];
printf("%d",op2);
op1 = s[top--];
printf("%d",op1);
s[++top] = res;
}
}
res = s[top--];
printf("\nThe result is : %f\n", res);
}
Pr5.B
#include <stdio.h>
#include <math.h>
int count = 0;
void tower(int n, int src, int temp, int dest)
{
if (n == 0)
return;
tower(n-1, src, dest, temp);
printf("Move disc %d from %c to %c",n,src,dest);
count++;
tower(n-1, temp, src, dest);
}
void main()
{
int n;
printf("Enter the number of discs:\n");
scanf("%d",&n);
tower(n,'A','B','C');
printf("\n total number of moves = %d",count);
}
Pr6
#include <stdio.h>
#define MAX 4
int ch, front = 0, rear = -1, count = 0, q[MAX], item;
void insert(int item, int *rear, int *q, int *count)
{
if (*count == MAX)
printf("Circular Queue is Full\n");
else
{
*rear = (*rear + 1) % MAX;
q[*rear] = item;
(*count)++;
}
}
void del(int *front, int *q, int *count)
{
if (*count == 0)
printf("Circular Queue is underflow\n");
else
{
item = q[*front];
printf("\nDeleted item is: %d", item);
*front = (*front + 1) % MAX;
(*count)--;
}
}
void display(int front, int q[], int count)
{
int i;
if (count == 0)
printf("\nCircular Queue is Empty");
else
{
printf("\nContents of Circualr Queue is:\n");
for (i = 1; i <= count; i++)
{
printf("%d\t", q[front]);
front = (front + 1) % MAX;
}
}
}
void main()
{
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter the item to be inserted: ");
scanf("%d", &item);
insert(item, &rear, q, &count);
break;
case 2:
del(&front, q, &count);
break;
case 3:
display(front, q, count);
break;
case 4:
exit(0);
break;
}
} while (ch != 4);
}
Pr7
#include <stdio.h>
#include <stdlib.h>
int MAX = 4, count = 0;
struct student {
char usn[10];
char name[30];
char branch[5];
int sem;
char phno[15];
struct student *next;
};
typedef struct student NODE;
NODE *getnode() {
NODE *newnode = (NODE *)malloc(sizeof(NODE));
if (!newnode) {
printf("Memory allocation failed!\n");
exit(1);
}
printf("Enter Student Details (USN, Name, Branch, Sem, Phone): ");
scanf("%s%s%s%d%s", newnode->usn, newnode->name, newnode->branch,
&newnode->sem, newnode->phno);
newnode->next = NULL;
return newnode;
}
void display(NODE *head) {
NODE *p = head;
if (head == NULL) {
printf("\nNo student data\n");
return;
}
printf("\n---- Student Details ----\n");
while (p != NULL) {
printf("%s\t%s\t%s\t%d\t%s\n", p->usn, p->name, p->branch, p-
>sem, p->phno);
p = p->next;
}
printf("\nThe number of nodes in the list is: %d\n", count);
}
NODE *insert_front(NODE *head) {
if (count == MAX) {
printf("\nList is Full!!\n");
return head;
}
NODE *newnode = getnode();
count++;
newnode->next = head;
return newnode;
}
NODE *insert_rear(NODE *head) {
if (count == MAX) {
printf("\nList is Full!!\n");
return head;
}
NODE *newnode = getnode();
count++;
if (head == NULL) {
return newnode;
}
NODE *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = newnode;
return head;
}
NODE *delete_front(NODE *head) {
if (head == NULL) {
printf("\nList is Empty!!\n");
return head;
}
NODE *p = head;
head = head->next;
free(p);
count--;
printf("\nFront (first) node is deleted\n");
return head;
}
NODE *delete_rear(NODE *head) {
if (head == NULL) {
printf("\nList is Empty!!\n");
return head;
}
if (head->next == NULL) {
free(head);
count--;
printf("\nLast (end) node is deleted\n");
return NULL;
}
NODE *p = head;
NODE *q = NULL;
while (p->next != NULL) {
q = p;
p = p->next;
}
q->next = NULL;
free(p);
count--;
printf("\nLast (end) node is deleted\n");
return head;
}
void free_list(NODE *head) {
NODE *temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
count = 0;
printf("\nAll nodes have been freed.\n");
}
int main() {
int ch;
NODE *head = NULL;
printf("*---------- Student Database ----------*\n");
do {
printf("\nMenu:\n");
printf("1. Insert at Front\n");
printf("2. Insert at Rear\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
head = insert_front(head);
break;
case 2:
head = insert_rear(head);
break;
case 3:
head = delete_front(head);
break;
case 4:
head = delete_rear(head);
break;
case 5:
display(head);
break;
case 6:
free_list(head);
printf("Exiting Program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (ch != 6);
return 0;
}
Pr8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int MAX = 4, count = 0;
struct emp
{
int ssn;
char name[20];
char dept[10];
char desig[15];
int sal;
char phno[10];
struct emp *left;
struct emp *right;
};
typedef struct emp NODE;
NODE *getnode()
{
NODE *newnode = (NODE *)malloc(sizeof(NODE));
if (!newnode)
{
printf("Memory allocation failed.\n");
exit(1);
}
newnode->left = newnode->right = NULL;
printf("\nEnter SSN, Name, Dept, Designation, Salary, Phone
Number:\n");
scanf("%d %s %s %s %d %s", &newnode->ssn, newnode->name, newnode-
>dept, newnode->desig, &newnode->sal, newnode->phno);
return newnode;
}
NODE *display(NODE *head)
{
NODE *p = head;
if (!head)
printf("\nNo Employee data\n");
else
{
printf("\n----EMPLOYEE DATA----\n");
while (p)
{
printf("\n%d\t%s\t%s\t%s\t\t%d\t\t%s", p->ssn, p->name, p-
>dept, p->desig, p->sal, p->phno);
p = p->right;
}
printf("\nThe number of nodes in the list is: %d\n", count);
}
return head;
}
NODE *insertEnd(NODE *head)
{
if (count == MAX)
{
printf("\nList is Full!\n");
return head;
}
NODE *newnode = getnode();
count++;
if (!head)
return newnode;
NODE *p = head;
while (p->right)
p = p->right;
p->right = newnode;
newnode->left = p;
return head;
}
NODE *insert_front(NODE *head)
{
if (count == MAX)
{
printf("\nList is Full!\n");
return head;
}
NODE *newnode = getnode();
count++;
if (!head)
return newnode;
newnode->right = head;
head->left = newnode;
return newnode;
}
NODE *delete_front(NODE *head)
{
if (!head)
{
printf("\nList is Empty\n");
return head;
}
NODE *temp = head;
head = head->right;
if (head)
head->left = NULL;
free(temp);
count--;
printf("\nFront (first) node deleted.\n");
return head;
}
NODE *delete_end(NODE *head)
{
if (!head)
{
printf("\nList is Empty\n");
return head;
}
NODE *p = head, *q = NULL;
while (p->right)
{
q = p;
p = p->right;
}
if (q)
q->right = NULL;
else
head = NULL; // If only one node exists.
free(p);
count--;
printf("\nLast (end) node deleted.\n");
return head;
}
void free_list(NODE *head)
{
NODE *temp;
while (head)
{
temp = head;
head = head->right;
free(temp);
}
printf("\nAll nodes freed.\n");
}
void main()
{
int ch;
NODE *head = NULL;
do
{
printf("\n1. Insert Front\n2. Insert End\n3. Delete Front\n4.
Delete End\n5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
head = insert_front(head);
break;
case 2:
head = insertEnd(head);
break;
case 3:
head = delete_front(head);
break;
case 4:
head = delete_end(head);
break;
case 5:
head = display(head);
break;
case 6:
free_list(head);
break;
default:
printf("\nInvalid choice!\n");
}
} while (ch != 6);
}
Pr9
#include <stdio.h>
#include <math.h>
struct node
{
int cf, px, py, pz;
int flag;
struct node *link;
};
typedef struct node NODE;
NODE *getnode()
{
NODE *x;
x = (NODE *)malloc(sizeof(NODE));
if (x == NULL)
{
printf("Insufficient memory\n");
exit(0);
}
return x;
}
void display(NODE *head)
{
NODE *temp;
if (head->link == head)
{
printf("Polynomial does not exist\n");
return;
}
temp = head->link;
printf("\n");
while (temp != head)
{
printf("%d*x^%d*y^%d*z^%d", temp->cf, temp->px, temp->py, temp->pz);
if(temp->link != head)
printf(" + ");
temp=temp->link;
}
printf("\n");
}
NODE *insert_rear(int cf, int x, int y, int z, NODE *head)
{
NODE *temp, *cur;
temp = getnode();
temp->cf = cf;
temp->px = x;
temp->py = y;
temp->pz = z;
cur = head->link;
while (cur->link != head)
{
cur = cur->link;
}
cur->link = temp;
temp->link = head;
return head;
}
NODE *read_poly(NODE *head)
{
int px, py, pz, cf, ch;
printf("\nEnter coeff: ");
scanf("%d", &cf);
printf("\nEnter x, y, z powers(0-indiacate NO term): ");
scanf("%d%d%d", &px, &py, &pz);
head = insert_rear(cf, px, py, pz, head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
scanf("%d", &ch);
while (ch != 0)
{
printf("\nEnter coeff: ");
scanf("%d", &cf);
printf("\nEnter x, y, z powers(0-indiacate NO term): ");
scanf("%d%d%d", &px, &py, &pz);
head = insert_rear(cf, px, py, pz, head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
scanf("%d", &ch);
}
return head;
}
NODE *add_poly(NODE *h1, NODE *h2, NODE *h3)
{
NODE *p1, *p2;
int x1, x2, y1, y2, z1, z2, cf1, cf2, cf;
p1 = h1->link;
while (p1 != h1)
{
x1 = p1->px;
y1 = p1->py;
z1 = p1->pz;
cf1 = p1->cf;
p2 = h2->link;
while (p2 != h2)
{
x2 = p2->px;
y2 = p2->py;
z2 = p2->pz;
cf2 = p2->cf;
if (x1 == x2 && y1 == y2 && z1 == z2)
break;
p2 = p2->link;
}
if (p2 != h2)
{
cf = cf1 + cf2;
p2->flag = 1;
if (cf != 0)
h3 = insert_rear(cf, x1, y1, z1, h3);
}
else
h3 = insert_rear(cf1, x1, y1, z1, h3);
p1 = p1->link;
}
p2 = h2->link;
while (p2 != h2)
{
if (p2->flag == 0)
h3 = insert_rear(p2->cf, p2->px, p2->py, p2->pz, h3);
p2 = p2->link;
}
return h3;
}
void evaluate(NODE *h)
{
NODE *head;
int x, y, z;
float result = 0.0;
head = h;
printf("\nEnter x, y, z, terms to evaluate:\n");
scanf("%d%d%d", &x, &y, &z);
while (h->link != head)
{
result = result + (h->cf * pow(x, h->px) * pow(y, h->py) *
pow(z, h->pz));
h = h->link;
}
result = result + (h->cf * pow(x, h->px) * pow(y, h->py) *
pow(z, h->pz));
printf("\nPolynomial result is: %f", result);
}
void main()
{
NODE *h, *h1, *h2, *h3;
int ch;
while (1)
{
printf("\n\n1.Evaluate polynomial\n2.Add two polynomials\
n3.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter polynomial to evaluate:\n");
h = getnode();
h->link = h;
h = read_poly(h);
display(h);
evaluate(h);
break;
case 2:
printf("\nEnter the first polynomial:");
h1 = getnode();
h1->link = h1;
h1 = read_poly(h1);
printf("\nEnter the second polynomial:");
h2 = getnode();
h2->link = h2;
h2 = read_poly(h2);
h3 = getnode();
h3->link = h3;
h3 = add_poly(h1, h2, h3);
printf("\nFirst polynomial is: ");
display(h1);
printf("\nSecond polynomial is: ");
display(h2);
printf("\nThe sum of 2 polynomials is: ");
display(h3);
break;
case 3:
exit(0);
break;
default:
printf("\nInvalid entry");
break;
}
}
}
Pr10
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int info;
struct node *llink, *rlink;
} NODE;
NODE *insert(int item, NODE *root) {
NODE *temp = (NODE *)malloc(sizeof(NODE)), *cur = root, *prev =
NULL;
temp->info = item, temp->llink = temp->rlink = NULL;
if (!root) return temp;
while (cur) prev = cur, cur = (item <= cur->info) ? cur->llink :
cur->rlink;
if (item < prev->info) prev->llink = temp; else prev->rlink = temp;
return root;
}
NODE *construct_BST(NODE *root) {
int n, a;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements: ");
while (n--) scanf("%d", &a), root = insert(a, root);
return root;
}
void preorder(NODE *root) { if (root) printf("%d\t", root->info),
preorder(root->llink), preorder(root->rlink); }
void inorder(NODE *root) { if (root) inorder(root->llink), printf("%d\
t", root->info), inorder(root->rlink); }
void postorder(NODE *root) { if (root) postorder(root->llink),
postorder(root->rlink), printf("%d\t", root->info); }
int search_element(NODE *root, int key) {
while (root && root->info != key) root = (key < root->info) ? root-
>llink : root->rlink;
return root != NULL;
}
int main() {
NODE *root = NULL;
int ch, key;
while (1) {
printf("\n1.Construct BST 2.Preorder 3.Inorder 4.Postorder
5.Search 6.Exit\nEnter choice: ");
scanf("%d", &ch);
if (ch == 6) break;
switch (ch) {
case 1: root = construct_BST(root); break;
case 2: preorder(root); break;
case 3: inorder(root); break;
case 4: postorder(root); break;
case 5: printf("Enter key: "); scanf("%d", &key);
printf(search_element(root, key) ? "Key found\n" :
"Not found\n"); break;
default: printf("Wrong choice\n");
}
}
return 0;
}
Pr11.A
#include <stdio.h>
#include <stdlib.h>
void bfs(int a[10][10], int n, int u)
{
int f, r, q[10], v;
int s[10] = {0}; // initialize all elem in s to 0, no node visited
printf("The nodes visited from %d:", u);
f = 0, r = -1; // Q empty
q[++r] = u; // Insert u into Q
s[u] = 1; // Insert u to s
printf("%d", u); // pritn node visited
while (f <= r)
{
u = q[f++]; // del an elem from Q
for (v = 1; v <= n; v++)
{
if (a[u][v] == 1) // If v is adjacent to u
{
if (s[v] == 0) // If v is not in S i.e, V has not been
visited
{
printf("%d", v); // print the node visited
s[v] = 1; // add v to s,mark as visited
q[++r] = v; // insert v into Q
}
}
}
}
printf("\n");
}
void main()
{
int n, a[10][10], source, i, j;
printf("Enter the number of nodes:");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &a[i][j]);
}
}
for (;;)
{
printf(" enter the source vertex\n");
scanf("%d", &source);
bfs(a, n, source);
}
}
Pr11.B
#include <stdio.h>
#include <stdlib.h>
int visited[10];
int a[10][10];
int n;
void readadjmatrix();
void dfs(int);
void main()
{
int start;
clrscr();
readadjmatrix();
printf("Enter the starting vertex:\n");
scanf("%d", &start);
dfs(start);
}
void readadjmatrix()
{
int i, j;
printf("Enter the number of vertices:\n");
scanf("%d", &n);
printf("Enter adjacency matrix\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
}
void dfs(int v)
{
int w;
visited[v] = 1;
for (w = 1; w <= n; w++)
{
if (visited[w] == 0 && a[v][w] == 1)
{
printf("%d", w);
dfs(w);
}
}
}
Pr12
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_SIZE 100
typedef struct employee {
int id;
char name[20];
} EMPLOYEE;
void initialize_hash_table(EMPLOYEE a[]) { for (int i = 0; i <
HASH_SIZE; i++) a[i].id = 0; }
int H(int k) { return k % HASH_SIZE; }
void insert_hash_table(int id, char name[], EMPLOYEE a[]) {
int h = H(id), i, index;
for (i = 0; i < HASH_SIZE; i++) {
index = (h + i) % HASH_SIZE;
if (a[index].id == 0) {
a[index].id = id;
strcpy(a[index].name, name);
return;
}
}
printf("Hash table full\n");
}
void display_hash_table(EMPLOYEE a[]) {
for (int i = 0; i < HASH_SIZE; i++)
if (a[i].id != 0)
printf("%d\t%d\t%s\n", i, a[i].id, a[i].name);
}
int main() {
EMPLOYEE a[HASH_SIZE];
char name[20];
int id, choice;
initialize_hash_table(a);
while (1) {
printf("1:Insert 2:Display 3:Exit\nEnter choice: ");
scanf("%d", &choice);
if (choice == 3) break;
switch (choice) {
case 1: printf("Enter emp id: "); scanf("%d", &id);
printf("Enter name: "); scanf("%s", name);
insert_hash_table(id, name, a);
break;
case 2: printf("Hash table contents:\n");
display_hash_table(a);
break;
default: printf("Invalid choice\n");
}
}
return 0;
}