Term Work
Term Work
Section: J1
Roll no : 29
Q1. Write a C program to implement priority queue using doubly linked list
(Priority
are equal. Then FIFO rules are used with following functions,
CODE :
#include <stdio.h>
#include <stdlib.h>
int id;
} Node;
newNode->id = id;
return newNode;
}
if (head == NULL) {
head = newNode;
return;
current = current->next;
if (current == NULL) {
while (tail->next) {
tail = tail->next;
tail->next = newNode;
newNode->prev = tail;
newNode->next = head;
head->prev = newNode;
head = newNode;
} else {
newNode->next = current;
newNode->prev = current->prev;
current->prev->next = newNode;
current->prev = newNode;
void delete() {
if (head == NULL) {
printf("Queue is empty.\n");
return;
head = head->next;
if (head) {
head->prev = NULL;
free(temp);
void display() {
if (head == NULL) {
printf("Queue is empty.\n");
return;
while (current) {
printf("%d ", current->id);
current = current->next;
printf("\n");
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &id);
insert(id);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
return 0;
}
Output:
1. Insert
2. Delete
3. Display
4. Exit
10 15 20
15 20
20
Queue is empty.
Invalid choice.
Section: J1
Roll no : 29
Q2. Write a C program to find union (of two linked lists) based on their
information field
that implements singly linked list (with information field Emp_Id and Name of
employee
Code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Emp_Id;
char Name[50];
} Node;
newNode->Emp_Id = id;
strcpy(newNode->Name, name);
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
while (head) {
if (head->Emp_Id == id) {
return 1;
head = head->next;
return 0;
temp = list1;
while (temp) {
temp = temp->next;
temp = list2;
while (temp) {
if (!exists(unionHead, temp->Emp_Id)) {
temp = temp->next;
return unionHead;
while (head) {
head = head->next;
int main() {
scanf("%d", &n1);
scanf("%d", &n2);
display(unionList);
return 0;
}
Output :
Section: J1
Roll no : 29
Q3. Write a C program to create a linked list P, then write a ‘C’ function named
split to
create two linked lists Q & R from P So that Q contains all elements in odd
positions of P
and R contains the remaining elements. Finally print both linked lists i.e. Q and
R.
code :
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
return;
while (temp->next) {
temp = temp->next;
temp->next = newNode;
int position = 1;
while (P) {
if (position % 2 == 1) {
insert(Q, P->data);
} else {
insert(R, P->data);
P = P->next;
position++;
while (head) {
head = head->next;
printf("\n");}
int main() {
int n, data;
scanf("%d", &n);
scanf("%d", &data);
insert(&P, data); }
display(Q);
display(R);
return 0;}
Output
123456
Section: J1
Roll no : 29
Q4. Write a C program to create a binary search tree and perform following
operations:
5) Count total numbers of nodes from right hand side of root node
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
newNode->data = data;
return newNode;
}
Node* insert(Node *root, int data) {
if (root == NULL) {
return createNode(data);
return root;
root = root->left;
return root;
} else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
root->data = temp->data;
return root;
if (!root) return 0;
int count = 0;
count = 2;
count = 1;
}
inorder(root->left);
inorder(root->right);
int main() {
Node *root = NULL;
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
break;
case 2:
if (root) {
} else {
printf("Tree is empty.\n"); }
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
inorder(root);
printf("\n");
break;
case 8:
exit(0);
default:
printf("Invalid choice.\n");
}}
return 0;}
Output :
Smallest node: 30
Height of BST: 2
Section: J1
Roll no : 29
Code:
#include <stdio.h>
#include <stdlib.h>
} Edge;
int V, E;
Edge* edges;
} Graph;
graph->V = V;
graph->E = E;
graph->edges = (Edge*)malloc(E * sizeof(Edge));
return graph;
int parent;
int rank;
} Subset;
if (subsets[i].parent != i) {
return subsets[i].parent;
subsets[rootX].parent = rootY;
subsets[rootY].parent = rootX;
} else {
subsets[rootY].parent = rootX;
subsets[rootX].rank++;
int V = graph->V;
Edge result[V];
int e = 0;
int i = 0;
subsets[v].parent = v;
subsets[v].rank = 0;
}
if (x != y) {
result[e++] = nextEdge;
Union(subsets, x, y);
int minCost = 0;
minCost += result[i].weight;
free(subsets);
}
int main() {
int V, E;
KruskalMST(graph);
free(graph->edges);
free(graph);
return 0;
}
OUTPUT:
0 1 10
026
035
1 3 15
234
2 -- 3 == 4
0 -- 3 == 5
0 -- 1 == 10
Section: J1
Roll no : 29
Q6. Write a C program to sort N names (as a string) given by user in an array,
using Quick
sort technique.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char temp[100];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
char pivot[100];
strcpy(pivot, arr[high]);
int i = low - 1;
for (int j = low; j < high; j++) {
i++;
swap(arr[i], arr[j]);
return i + 1;
quickSort(arr, pi + 1, high);
int main() {
int n;
scanf("%d", &n);
char names[n][100];
printf("Enter %d names:\n", n);
scanf("%s", names[i]);
quickSort(names, 0, n - 1);
printf("Sorted names:\n");
printf("%s\n", names[i]);
return 0;
}
OUTPUT :
Enter 5 names:
Ravi
Priya
Ananya
Arjun
Rahul
Sorted names:
Ananya
Arjun
Priya
Rahul
Ravi
Name : Anishk Singh
Section: J1
Roll no : 29
Q7. Write a C program using circular linked list allocate time slots of 10 ms for
given
processes in time sharing environment and then print which process will be
completed in
Code:
#include <stdio.h>
#include <stdlib.h>
int id;
int burstTime;
} Process;
newNode->id = id;
newNode->burstTime = burstTime;
return newNode;
}
void insert(Process** head, int id, int burstTime) {
if (*head == NULL) {
*head = newNode;
return;
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
if (!head) return;
int timeElapsed = 0;
do {
if (current->burstTime > 0) {
current->burstTime -= timeSlice;
timeElapsed += timeSlice;
if (current->burstTime == 0) {
current = current->next;
int main() {
scanf("%d", &n);
scanf("%d", &id);
scanf("%d", &burstTime);
simulateTimeSharing(head);
return 0;
}
OUTPUT:
Process ID: 1
Burst Time: 25
Process ID: 2
Burst Time: 15
Process ID: 3
Burst Time: 30
Process P1 completed in 25 ms
Process P2 completed in 40 ms
Process P3 completed in 70 ms
Name : Anishk Singh
Section: J1
Roll no : 29
Q8. Write a C program to store the details of a weighted graph (Using linked
list).
Code:
#include <stdio.h>
#include <stdlib.h>
int dest;
int weight;
} EdgeNode;
int vertex;
EdgeNode* edges;
} GraphNode;
GraphNode* head;
} Graph;
Graph* createGraph() {
graph->head = NULL;
return graph;
newNode->vertex = vertex;
newNode->edges = NULL;
newNode->next = NULL;
return newNode;
newNode->dest = dest;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
}
void addEdge(Graph* graph, int src, int dest, int weight) {
srcNode = srcNode->next;
if (!srcNode) {
srcNode = createGraphNode(src);
srcNode->next = graph->head;
graph->head = srcNode;
newEdge->next = srcNode->edges;
srcNode->edges = newEdge;
while (current) {
while (edge) {
edge = edge->next;
}
current = current->next;
int main() {
scanf("%d", &n);
printf("Graph representation:\n");
displayGraph(graph);
return 0;
}
OUTPUT:
0 1 10
0 2 20
1 2 30
2 3 40
Name : Anishk Singh
Section: J1
Roll no : 29
Code:
#include <stdio.h>
#include <stdlib.h>
int** adjMatrix;
int vertices;
} Graph;
g->vertices = vertices;
g->adjMatrix[i][j] = 0;
}
}
if (src >= 0 && src < g->vertices && dest >= 0 && dest < g->vertices) {
g->adjMatrix[src][dest] = 1;
g->adjMatrix[dest][src] = 1;
} else {
int stack[g->vertices];
stack[++top] = startVertex;
visited[startVertex] = 1;
stack[++top] = i;
visited[i] = 1;
}
}
printf("\n");
void displayGraph(Graph* g) {
printf("Adjacency Matrix:\n");
printf("\n");
int main() {
Graph g;
scanf("%d", &g.vertices);
initializeGraph(&g, g.vertices);
int visited[g.vertices];
visited[i] = 0;
do {
printf("\nMenu:\n");
printf("1. Add edge\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
displayGraph(&g);
break;
case 3:
scanf("%d", &startVertex);
visited[i] = 0;
break;
case 4:
printf("Exiting...\n");
break;
default:
break;
free(g.adjMatrix[i]);
free(g.adjMatrix);
return 0;
}
OUTPUT :
1. Add edge
2. Display graph
3. DFS traversal
4. Exit
1. Add edge
2. Display graph
3. DFS traversal
4. Exit
Adjacency Matrix:
01000
10000
00000
00000
00000
1. Add edge
2. Display graph
3. DFS traversal
4. Exit
Section: J1
Roll no : 29
Code:
#include <stdio.h>
#include <stdlib.h>
int** adjMatrix;
int vertices;
} Graph;
g->vertices = vertices;
g->adjMatrix[i][j] = 0;
}
}
if (src >= 0 && src < g->vertices && dest >= 0 && dest < g->vertices) {
g->adjMatrix[src][dest] = 1;
g->adjMatrix[dest][src] = 1;
} else {
visited[i] = 0;
visited[startVertex] = 1;
queue[++rear] = startVertex;
visited[i] = 1;
queue[++rear] = i;
printf("\n");
free(visited);
free(queue);
void displayGraph(Graph* g) {
printf("Adjacency Matrix:\n");
printf("\n");
int main() {
Graph g;
scanf("%d", &g.vertices);
initializeGraph(&g, g.vertices);
do {
printf("\nMenu:\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
displayGraph(&g);
break;
case 3:
scanf("%d", &startVertex);
BFS(&g, startVertex);
break;
case 4:
printf("Exiting...\n");
break;
default:
break;
free(g.adjMatrix[i]);
free(g.adjMatrix);
return 0;
}
OUTPUT:
Enter number of vertices: 4
Menu:
1. Add edge
2. Display graph
3. BFS traversal
4. Exit
Adjacency Matrix:
0100
1010
0100
0000
BFS Traversal: 0 1 2