DSA LAB Programs
DSA LAB Programs
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char* dayName;
int date;
char* activity;
} DayInfo;
DayInfo* create() {
DayInfo* day = (DayInfo*)malloc(sizeof(DayInfo));
if (day == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
day->dayName = NULL;
day->activity = NULL;
return day;
}
int main() {
int numDays = 7;
DayInfo* week = (DayInfo*)malloc(numDays * sizeof(DayInfo));
if (week == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
return EXIT_FAILURE;
}
for (int i = 0; i < numDays; i++) {
week[i] = *create();
read(&week[i]);
}
display(week, numDays);
int main() {
int i = 0, j, k = 0, flag = 0;
char STR[50], PAT[15], REP[15], RESULT[50];
if (PAT[j] == '\0') {
// If the loop reached the end of the pattern, it means a match
is found
flag = 1;
RESULT[k] = '\0';
// Concatenate the substitution key to the result
stringCat(RESULT, REP);
k = k + stringLen(REP);
// Move the main string index to skip the matched pattern
i = i + (j - 1);
} else {
// If no match, copy the character to the result
RESULT[k++] = STR[i];
}
RESULT[k] = '\0';
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void main()
{
int stack[MAX], top = -1, ele, ch;
for(;;)
//while(1)
{
printf("\nMenu\n");
printf("1. Push\n2. Pop\n3. Check Palindrome\n4. Stack
Status\n5. Display\n6. Exit\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(stack_full(top))
printf("Stack Full\n");
else
{
printf("Enter an Element\n");
scanf("%d", &ele);
push(stack, &top, ele);
}
break;
case 2:
if(stack_empty(top))
printf("Stack Empty\n");
else
{
ele = pop(stack, &top);
printf("Deleted Element is %d", ele);
}
break;
case 3:
if(stack_empty(top))
printf("Stack Empty\n");
else if(is_palindrome(stack, top))
{
printf("Stack is Palindrome\n");
display(stack, top);
}
else
printf("Stack is not Palindrome\n");
break;
case 4:
if(stack_empty(top))
printf("Stack Empty\n");
else if(stack_full(top))
printf("Stack Full\n");
else
printf("Stack contains %d elements\n", top+1);
break;
case 5:
if(stack_empty(top))
printf("Stack Empty\n");
else
display(stack, top);
break;
case 6:
exit(0);
default:
printf("Entered Menu is not available");
break;
}
}
}
int stack_full(int top)
{
if(top == MAX-1)
return 1;
return 0;
}
stack[++(*top)] = ele;
}
if(not_palindrome)
return 0;
else
return 1;
}
// Main function
void main() {
char infix[30];
printf("Enter Infix Expression\n");
gets(infix);
convert(infix);
}
#include<stdio.h>
#include<stdlib.h>
//#include<math.h>
#include<ctype.h>
// Main function
void main()
{
// Accepting postfix expression from the user
printf("\nEnter a valid postfix expression:\n");
scanf("%s", postfix);
// Processing each symbol in the postfix expression
for (i = 0; postfix[i] != '\0'; i++)
{
symb = postfix[i];
if (isdigit(symb))
{
// If the symbol is a digit, push its integer value onto the stack
push(symb -'0' );
}
else
{
// If the symbol is an operator, pop two operands, perform the
operation, and push the result back onto the stack
op2 = pop();
op1 = pop();
switch (symb)
{
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
case '^':
for(int i= 0;i<op2;i++)
{
result*=op1;
}
push(result);
break;
default:
push(0);
}
}
}
TOH
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 4; // Number of disks
return 0;
}
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
// Function prototypes
void cqInsert(char queue[], int *rear, int *count);
void cqDelete(char queue[], int *front, int *count);
void display(char queue[], int front, int count);
int main() {
char queue[MAX];
int front = 0, rear = -1, ch, count = 0;
for (;;) {
// Display menu options
printf("\nMenu\n");
printf("1. Insert\n2. Delete\n3. Demonstrate Overflow and
Underflow\n4. Display\n5. Exit\n");
scanf("%d", &ch);
switch (ch) {
case 1:
// Insert operation
cqInsert(queue, &rear, &count);
break;
case 2:
// Delete operation
cqDelete(queue,&front, &count);
break;
case 3:
// Demonstrate Overflow and Underflow
if (count == MAX) {
printf("Queue is Full (Overflow)\n");
} else if (count == 0) {
printf("Queue is Empty (Underflow)\n");
} else {
printf("Queue is neither Full nor Empty\n");
}
break;
case 4:
// Display operation
display(queue, front, count);
break;
case 5:
// Exit the program
exit(0);
}
}
}
// Main function
void main() {
int n1, n2;
POLY *POLY1 = getNode();
POLY *POLY2 = getNode();
POLY *POLYSUM = getNode();
evaluate(POLYSUM);
}
temp->link = head;
return;
}
while(temp != head) {
printf("%f*X^%d*Y^%d*Z^%d\t", temp->coef, temp->expx,
temp->expy, temp->expz);
temp = temp->link;
}
printf("\n");
return;
}
while(temp1 != h1) {
temp2 = h2->link;
while(temp2 != h2) {
switch(compare(temp1, temp2)) {
case 1:
cf = temp1->coef + temp2->coef;
if(cf) {
attach(cf, temp1, &tempres);
}
temp1 = temp1->link;
h2 = delete(h2, temp2);
temp2 = h2->link;
break;
case 2:
temp2 = temp2->link;
break;
}
}
if(temp1 != h1) {
attach(temp1->coef, temp1, &tempres);
temp1 = temp1->link;
}
}
temp2 = h2->link;
while(temp2 != h2) {
attach(temp2->coef, temp2, &tempres);
temp2 = temp2->link;
}
tempres->link = result;
return result;
}
while(present != temp) {
previous = present;
present = present->link;
}
previous->link = present->link;
free(present);
return head;
}
printf("\nEnter exponents\n");
scanf("%d%d%d", &x, &y, &z);
while(temp != head) {
result += (temp->coef) * pow(x, temp->expx) * pow(y,
temp->expy) * pow(z, temp->expz);
temp = temp->link;
}
// Main function
void main() {
int ch;
HEAD *head = (HEAD *) malloc(sizeof(HEAD));
head->count = 0;
head->llink = NULL;
head->rlink = NULL;
for(;;) {
printf("\n\nMenu\n");
printf("\n1. Insert Front\n2. Insert Rear\n3. Delete Front\n4.
Delete Rear\n5. Display\n6. Exit\n");
scanf("%d", &ch);
switch(ch) {
case 1:
insfront(head);
break;
case 2:
insrear(head);
break;
case 3:
if(head->rlink == NULL)
printf("List Empty");
else
delfront(head);
break;
case 4:
if(head->rlink == NULL)
printf("List Empty");
else
delrear(head);
break;
case 5:
if(head->rlink == NULL)
printf("List Empty");
else
display(head);
break;
case 6:
exit(0);
}
}
}
if(next != NULL)
next->llink = new;
new->rlink = next;
head->rlink = new;
(head->count)++;
}
(head->count)++;
new->rlink = NULL;
if(head->rlink == NULL) {
head->rlink = new;
return;
}
temp = head->rlink;
while(temp->rlink != NULL)
temp = temp->rlink;
temp->rlink = new;
new->llink = temp;
}
head->rlink = temp->rlink;
free(temp);
(head->count)--;
}
if(present->rlink == NULL) {
head->rlink = NULL;
} else {
while(present->rlink != NULL) {
previous = present;
present = present->rlink;
}
previous->rlink = NULL;
}
(head->count)--;
free(present);
}
// Function to display all the records in the list
void display(HEAD *head) {
NODE *temp = head->rlink;
printf("SSN\tName\tDepartment\tDesignation\tSalary\t\tPhNo\n")
;
while(temp != NULL) {
printf("%s\t%s\t%s\t\t%s\t\t%f\t%ld\n", (temp->ssn),
(temp->name), (temp->department), (temp->designation),
(temp->sal), (temp->phno));
temp = temp->rlink;
}
}
struct node
{
int coef;
int xexp, yexp, zexp;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if(x == NULL)
{
printf("Running out of memory \n");
return NULL;
}
return x;
}
NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
cur = head->link;
while(cur->link != head)
{
cur = cur->link;
}
cur->link = temp;
temp->link = head;
return head;
}
while(temp != head)
{
printf("%dx^%dy^%dz^%d", temp->coef, temp->xexp,
temp->yexp, temp->zexp);
temp = temp->link;
if(temp != head)
printf(" + ");
}
}
poly = head->link;
while(poly != head)
{
sum += poly->coef * pow(x,poly->xexp)* pow(y,poly->yexp)
* pow(z,poly->zexp);
poly = poly->link;
}
return sum;
}
void main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode(); /* For polynomial evalaution */
head1 = getnode(); /* To hold POLY1 */
head2 = getnode(); /* To hold POLY2 */
head3 = getnode(); /* To hold POLYSUM */
head->link=head;
head1->link=head1;
head2->link=head2;
head3->link= head3;
while(1)
{
printf("\n~~~Menu~~~");
printf("\n1.Represent and Evaluate a Polynomial P(x,y,z)");
printf("\n2.Find the sum of two polynomials POLY1(x,y,z)");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n~~~~Polynomial evaluation
P(x,y,z)~~~\n");
head = read_poly(head);
printf("\nRepresentation of Polynomial for
evaluation: \n");
display(head);
res = poly_evaluate(head);
printf("\nResult of polynomial evaluation is : %d
\n", res);
break;
// Main function
void main() {
TNODE *root = NULL;
int ch, flag;
int ele, key;
for (;;) {
printf("\n1.Insert\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\
n6.Delete\n7.Exit\n");
printf("\nEnter your choice\n");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to be inserted\n");
scanf("%d", &ele);
root = insert(root, ele);
break;
case 2:
if (root == NULL)
printf("Empty tree\n");
else {
printf("The contents are\n");
inorder(root);
}
break;
case 3:
if (root == NULL)
printf("Empty tree\n");
else {
printf("The contents are\n");
preorder(root);
}
break;
case 4:
if (root == NULL)
printf("Empty tree\n");
else {
printf("The contents are\n");
postorder(root);
}
break;
case 5:
printf("Enter the element to be searched\n");
scanf("%d", &key);
flag = search(root, key);
if (flag)
printf("Found\n");
else
printf("Not Found\n");
break;
case 6:
printf("Enter the element to be deleted\n");
scanf("%d", &ele);
root = delete(root, ele);
break;
case 7:
exit(0);
}
}
}
if (root == NULL) {
new = getnode();
new->data = ele;
new->rlink = new->llink = NULL;
return new;
}
return root;
}
return 0;
}
if (root == NULL)
printf("Element not found\n");
else if (ele < root->data)
root->llink = delete(root->llink, ele);
else if (ele > root->data)
root->rlink = delete(root->rlink, ele);
else {
if (root->llink != NULL && root->rlink != NULL) {
temp = findMin(root->rlink);
root->data = temp->data;
root->rlink = delete(root->rlink, temp->data);
} else {
temp = root;
if (root->llink == NULL)
root = root->rlink;
else if (root->rlink == NULL)
root = root->llink;
free(temp);
}
}
return root;
}
if (root->llink != NULL)
return findMin(root->llink);
else
return root;
}
11. Develop a Program in C for the following operations on
Graph(G) of Cities a. Create a Graph of N cities using
Adjacency Matrix. b. Print all the nodes reachable from a
given starting node in a graph using DFS/BFSmethod.
#include<stdio.h>
#include<stdlib.h>
void bfs(int v)
{
int i, cur;
visited[v] = 1;
q[++rear] = v;
while(front!=rear)
{
cur = q[++front];
for(i=1;i<=n;i++)
{
if((a[cur][i]==1)&&(visited[i]==0))
{
q[++rear] = i;
visited[i] = 1;
printf("%d ", i);
}
}
}
}
void dfs(int v)
{
int i;
visited[v]=1;
s[++top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& visited[i] == 0 )
{
printf("%d ", i);
dfs(i);
}
}
}
int main()
{
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);
#include <stdio.h>
#include <stdlib.h>
int key[10], n, m;
int *ht, index;
int count = 0;
void display() {
int i;
if (count == 0) {
printf("\nHash Table is empty");
return;
}
int main() {
int i;
FILE *file = fopen("employee_records.txt", "r");
if (file == NULL) {
printf("Could not open the file.");
return 1;
}