Program
/*
Name:Ayush Madhwal
Course: MCA-II A
Roll No: 54
Subject: Data Structure Laboratory
Subject Code: PMC-203
*/
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
typedef struct singlelist {
int data;
struct singlelist *next;
} sl;
sl* insert(sl*, int);
void display(sl*);
sl* merge(sl*, sl*);
int main() {
sl *start1 = NULL, *start2 = NULL, *Merge = NULL;
int v, ch;
do {
printf("\n1: Create First Sorted Linked List");
printf("\n2: Create Second Sorted Linked List");
printf("\n3: Merge Linked Lists");
printf("\n0: Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data to insert in List 1: ");
scanf("%d", &v);
start1 = insert(start1, v);
break;
case 2:
printf("Enter data to insert in List 2: ");
scanf("%d", &v);
start2 = insert(start2, v);
break;
case 3:
Merge = merge(start1, start2);
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Wrong Choice\n");
}
getch();
system("cls");
} while (ch != 0);
return 0;
}
sl* insert(sl *head, int val) {
sl *newNode = (sl*)malloc(sizeof(sl));
newNode->data = val;
newNode->next = NULL;
if (head == NULL || val < head->data) {
newNode->next = head;
return newNode;
}
sl *current = head;
while (current->next != NULL && current->next->data < val) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
return head;
}
void display(sl *head) {
if (head == NULL) {
printf("Empty list\n");
return;
}
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
sl* merge(sl* l1, sl* l2) {
printf("List 1: ");
display(l1);
printf("\nList 2: ");
display(l2);
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;
sl *result = NULL, **lastPtr = &result;
while (l1 && l2) {
if (l1->data < l2->data) {
*lastPtr = l1;
l1 = l1->next;
} else {
*lastPtr = l2;
l2 = l2->next;
}
lastPtr = &(*lastPtr)->next;
}
*lastPtr = (l1 != NULL) ? l1 : l2;
printf("\nMerged List: ");
display(result);
return result;
}
Output
1: Create First Sorted Linked List
2: Create Second Sorted Linked List
3: Merge Linked Lists
0: Exit
Enter your choice: 1
Enter data to insert in List 1: 2
1: Create First Sorted Linked List
2: Create Second Sorted Linked List
3: Merge Linked Lists
0: Exit
Enter your choice: 3
List 1: 2 -> 5 -> 6 -> NULL
List 2: 3 -> 7 -> 9 -> NULL
Merged List: 2 -> 3 -> 5 -> 6 -> 7 -> 9 -> NULL
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void InsertNth(Node **head, int position, int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (position == 0) {
newNode->next = *head;
*head = newNode;
return;
}
Node* temp = *head;
int i = 0;
while (temp != NULL && i < position - 1) {
temp = temp->next;
i++;
}
if (temp == NULL) {
printf("Position not found\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
void display(Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
printf("Linked List: ");
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
Node* head = NULL;
int choice, data, position;
do {
printf("1. Insert at Nth position\n");
printf("2. Display list\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter position (0-based): ");
scanf("%d", &position);
printf("Enter data to insert: ");
scanf("%d", &data);
InsertNth(&head, position, data);
break;
case 2:
display(head);
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
getch();
system("cls");
} while (choice != 0);
return 0;
}
OUTPUT
1. Insert at Nth position
2. Display list
0. Exit
Enter your choice: 2
Linked List: 56 -> 43 -> NULL
1. Insert at Nth position
2. Display list
0. Exit
Enter your choice: 1
Enter position (0-based): 7
Enter data to insert: 4
Position not found
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void insertEnd(Node**,int);
void display(Node*);
void removeDuplicates(Node*);
int main() {
Node* head = NULL;
int choice, data;
do {
printf("\n--- MENU ---\n");
printf("1. Insert at end\n");
printf("2. Display list\n");
printf("3. Remove duplicates\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
insertEnd(&head, data);
break;
case 2:
display(head);
break;
case 3:
removeDuplicates(head);
printf("Duplicates removed.\n");
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
getch();
system("cls");
} while (choice != 0);
return 0;
}
void insertEnd(Node** head, int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
void display(Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
printf("Linked List: ");
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
void removeDuplicates(Node* head) {
Node *current = head, *prev = NULL, *temp = NULL, *dup =
NULL;
while (current != NULL && current->next != NULL) {
prev = current;
temp = current->next;
while (temp != NULL) {
if (current->data == temp->data) {
dup = temp;
prev->next = temp->next;
temp = temp->next;
free(dup);
} else {
prev = temp;
temp = temp->next;
}
}
current = current->next;
}
}
OUTPUT
--- MENU ---
1. Insert at end
2. Display list
3. Remove duplicates
0. Exit
Enter your choice: 2
Linked List: 3 -> 3 -> 5 -> 6 -> 7 -> 9 -> 7 -> NULL
--- MENU ---
1. Insert at end
2. Display list
3. Remove duplicates
0. Exit
Enter your choice: 3
Duplicates removed.
--- MENU ---
1. Insert at end
2. Display list
3. Remove duplicates
0. Exit
Enter your choice: 2
Linked List: 3 -> 5 -> 6 -> 7 -> 9 -> NULL
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct process {
int id;
int burst;
int waiting;
struct process *next;
};
struct process *createProcess(int id, int burst) {
struct process *p = (struct process*)malloc(sizeof(struct process));
p->id = id;
p->burst = burst;
p->waiting = 0;
p->next = NULL;
return p;
}
void addProcess(struct process **head, int id, int burst) {
struct process *newp = createProcess(id, burst);
if (*head == NULL) {
*head = newp;
newp->next = *head;
} else {
struct process *temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newp;
newp->next = *head;
}
}
void roundRobin(struct process *head, int timeSlot) {
int time = 0;
struct process *p = head;
int total = 0;
int completed = 0;
int n = 0;
struct process *temp = head;
do {
n++;
temp = temp->next;
} while (temp != head);
printf("\nProcess execution order:\n");
while (completed < n) {
if (p->burst > 0) {
if (p->burst > timeSlot) {
printf("P%d executed from %d to %d\n", p->id, time, time
+ timeSlot);
p->burst -= timeSlot;
time += timeSlot;
} else {
printf("P%d executed from %d to %d (Completed)\n", p-
>id, time, time + p->burst);
time += p->burst;
p->waiting = time;
p->burst = 0;
completed++;
}
}
p = p->next;
}
printf("\nWaiting times:\n");
p = head;
int totalWait = 0;
do {
printf("P%d waiting time: %d\n", p->id, p->waiting - 10); //
subtracting final slot
totalWait += (p->waiting - 10);
p = p->next;
} while (p != head);
printf("Average Waiting Time: %.2f\n", (float)totalWait / n);
}
int main() {
struct process *head = NULL;
int n, id, burst;
printf("Enter number of processes: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("Enter burst time for process %d: ", i);
scanf("%d", &burst);
addProcess(&head, i, burst);
}
roundRobin(head, 10);
return 0;
}
OUTPUT
Enter number of processes: 5
Enter burst time for process 1: 5
Enter burst time for process 2: 4
Enter burst time for process 3: 8
Enter burst time for process 4: 6
Enter burst time for process 5: 8
Process execution order:
P1 executed from 0 to 5 (Completed)
P2 executed from 5 to 9 (Completed)
P3 executed from 9 to 17 (Completed)
P4 executed from 17 to 23 (Completed)
P5 executed from 23 to 31 (Completed)
Waiting times:
P1 waiting time: -5
P2 waiting time: -1
P3 waiting time: 7
P4 waiting time: 13
P5 waiting time: 21
Average Waiting Time: 7.00
PROGRAM
#include <stdio.h>
#include<conio.h>
void swap(int*,int*);
int partition(int [],int,int);
void quickSort(int [],int,int);
void printArray(int[],int);
int main() {
int arr[100], n = 0, choice;
int sorted = 0;
do {
printf("\n--- Quick Sort Menu ---\n");
printf("1. Enter elements\n");
printf("2. Sort array using Quick Sort\n");
printf("3. Display array\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
sorted = 0;
break;
case 2:
if (n == 0) {
printf("Array is empty. Please enter elements first.\n");
} else {
quickSort(arr, 0, n - 1);
printf("Array sorted successfully.\n");
sorted = 1;
}
break;
case 3:
if (n == 0) {
printf("Array is empty.\n");
} else if (!sorted) {
printf("Array is not sorted yet. Showing unsorted array.\
n");
printArray(arr, n);
} else {
printArray(arr, n);
}
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
getch();
system("cls");
} while (choice != 0);
return 0;
}
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
void printArray(int arr[], int size) {
if (size == 0) {
printf("Array is empty.\n");
return;
}
printf("Array: ");
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
OUTPUT
--- Quick Sort Menu ---
1. Enter elements
2. Sort array using Quick Sort
3. Display array
0. Exit
Enter your choice: 1
Enter number of elements: 7
Enter 7 elements:7 6 5 4 3 2 1
--- Quick Sort Menu ---
1. Enter elements
2. Sort array using Quick Sort
3. Display array
0. Exit
Enter your choice: 2
Array sorted successfully.
--- Quick Sort Menu ---
1. Enter elements
2. Sort array using Quick Sort
3. Display array
0. Exit
Enter your choice: 3
Array: 1 2 3 4 5 6 7
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct Node {
int dest;
int weight;
struct Node* next;
};
struct Node* createNode(int dest, int weight) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->dest = dest;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
}
void addEdge(struct Node* graph[], int src, int dest, int weight) {
struct Node* newNode = createNode(dest, weight);
newNode->next = graph[src];
graph[src] = newNode;
}
void displayGraph(struct Node* graph[], int vertices) {
printf("\nWeighted Graph (Adjacency List):\n");
for (int i = 0; i < vertices; i++) {
struct Node* temp = graph[i];
printf("Vertex %d: ", i);
while (temp != NULL) {
printf("-> (%d, w=%d) ", temp->dest, temp->weight);
temp = temp->next;
}
printf("\n");
}
}
int main() {
struct Node* graph[MAX] = { NULL };
int vertices = 0, choice;
do {
printf("\n==== Menu ====\n");
printf("1. Set number of vertices\n");
printf("2. Add edge\n");
printf("3. Display graph\n");
printf("0. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter total number of vertices: ");
scanf("%d", &vertices);
for (int i = 0; i < vertices; i++)
graph[i] = NULL;
break;
case 2: {
int src, dest, weight;
if (vertices == 0) {
printf("Please set the number of vertices first.\n");
break;
}
printf("Enter source, destination and weight: ");
scanf("%d %d %d", &src, &dest, &weight);
if (src >= vertices || dest >= vertices) {
printf("Invalid vertices. Range is 0 to %d\n", vertices -
1);
} else {
addEdge(graph, src, dest, weight);
}
break;
}
case 3:
if (vertices == 0) {
printf("Graph is empty. Add vertices first.\n");
} else {
displayGraph(graph, vertices);
}
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 0);
return 0;
}
OUTPUT
==== Menu ====
1. Set number of vertices
2. Add edge
3. Display graph
0. Exit
Enter choice: 2
Enter source, destination and weight: 3
25
==== Menu ====
1. Set number of vertices
2. Add edge
3. Display graph
0. Exit
Enter choice: 3
Weighted Graph (Adjacency List):
Vertex 0: -> (2, w=5) -> (1, w=10)
Vertex 1:
Vertex 2:
Vertex 3: -> (2, w=5)
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define max 40
typedef struct vertex {
char c;
int visited;
struct vertex *next;
struct edege *s;
} v;
typedef struct edege {
struct vertex *data;
struct edege *next;
} e;
v *queue[max];
int R = -1, F = -1,;
v* insert(v*, char);
void createlink(v*, char, char);
void display(v*);
void displaygraph(v*);
void resetVisited(v*);
v* findvertex(v*, char);
void enqueue(v*);
v* dequeue();
void BFS(v*, char);
void main() {
int ch;
char c, s, d, startchar;
v *start = NULL;
do {
printf("\n1:Insert Vertex");
printf("\n2:Create Link");
printf("\n3:Display Vertices");
printf("\n4:Display Graph");
printf("\n5:BFS Traversal");
printf("\n0:Exit\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter vertex to insert: ");
scanf(" %c", &c);
start = insert(start, c);
break;
case 2:
printf("Enter source vertex: ");
scanf(" %c", &s);
printf("Enter destination vertex: ");
scanf(" %c", &d);
createlink(start, s, d);
break;
case 3:
display(start);
break;
case 4:
displaygraph(start);
break;
case 5:
printf("Enter start vertex for BFS: ");
scanf(" %c", &startchar);
resetVisited(start);
BFS(start, startchar);
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Enter valid choice..");
}
getch();
system("cls");
} while (ch != 0);
}
v* insert(v *s, char c) {
v *node, *temp;
node = (v*)malloc(sizeof(v));
node->c = c;
node->next = NULL;
node->visited = 0;
node->s = NULL;
if (node != NULL) {
if (s == NULL) {
s = node;
return s;
}
temp = s;
while (temp->next != NULL) {
if (temp->c == c) {
printf("Vertex already exists.\n");
return s;
}
temp = temp->next;
}
if (temp->c == c) {
printf("Vertex already exists.\n");
return s;
}
temp->next = node;
return s;
}
return s;
}
void createlink(v *h, char s, char d) {
v *temp = NULL, *source = NULL, *dest = NULL;
e *node = NULL;
if (h == NULL) {
printf("No vertices in the graph.\n");
return;
}
temp = h;
while (temp != NULL) {
if (temp->c == d) dest = temp;
if (temp->c == s) source = temp;
temp = temp->next;
}
if (source == NULL || dest == NULL) {
printf("Source or Destination vertex not found.\n");
} else {
node = (e*)malloc(sizeof(e));
node->data = dest;
node->next = source->s;
source->s = node;
}
}
void display(v *h) {
while (h != NULL) {
printf("%c ", h->c);
h = h->next;
}
printf("\n");
}
void displaygraph(v *h) {
e *temp;
if (h == NULL) {
printf("The graph is empty.\n");
} else {
while (h != NULL) {
printf("[%c]:", h->c);
temp = h->s;
while (temp != NULL) {
printf("->%c", temp->data->c);
temp = temp->next;
}
printf("\n");
h = h->next;
}
}
}
void enqueue(v *node) {
if (R == max - 1) {
printf("Queue Overflow\n");
return;
}
if (F == -1) F = 0;
queue[++R] = node;
}
v* dequeue() {
if (F == -1 || F > R) return NULL;
return queue[F++];
}
void resetVisited(v *head) {
while (head != NULL) {
head->visited = 0;
head = head->next;
}
}
v* findvertex(v *head, char c) {
while (head != NULL) {
if (head->c == c)
return head;
head = head->next;
}
return NULL;
}
void BFS(v *start, char ch) {
v *vstart = findvertex(start, ch);
if (!vstart) {
printf("Start vertex not found.\n");
return;
}
enqueue(vstart);
vstart->visited = 1;
printf("BFS Traversal: ");
while (F <= R) {
v *current = dequeue();
printf("%c ", current->c);
e *temp = current->s;
while (temp != NULL) {
if (temp->data->visited == 0) {
enqueue(temp->data);
temp->data->visited = 1;
}
temp = temp->next;
}
}
printf("\n");
F = R = -1;
}
OUTPUT
1:Insert Vertex
2:Create Link
3:Display Vertices
4:Display Graph
5:BFS Traversal
0:Exit
Enter your choice: 4
[a]:->d->c->b
[b]:->e
[c]:->b
[d]:->f->c
[e]:->f->d->c
[f]:->g
1:Insert Vertex
2:Create Link
3:Display Vertices
4:Display Graph
5:BFS Traversal
0:Exit
Enter your choice: 5
Enter start vertex for BFS: a
BFS Traversal: a d c b f e g
PROGRAM
void push(v *node) {
if (Top == max - 1) {
printf("Stack Overflow\n");
return;
}
stack[++Top] = node;
}
v* pop() {
if (Top == -1) return NULL;
return stack[Top--];
}
void enqueue(v *node) {
if (R == max - 1) {
printf("Queue Overflow\n");
return;
}
if (F == -1) F = 0;
queue[++R] = node;
}
void DFS(v *start, char ch) {
v *vstart = findvertex(start, ch);
if (!vstart) {
printf("Start vertex not found.\n");
return;
}
push(vstart);
vstart->visited = 1;
printf("DFS Traversal: ");
while (Top != -1) {
v *current = pop();
printf("%c ", current->c);
e *temp = current->s;
while (temp != NULL) {
if (temp->data->visited == 0) {
push(temp->data);
temp->data->visited = 1;
}
temp = temp->next;
}
}
printf("\n");
}
OUTPUT
1:Insert Vertex
2:Create Link
3:Display Vertices
4:Display Graph
5:DFS Traversal
0:Exit
Enter your choice: 4
[a]:->d->c->b
[b]:->e
[c]:->b
[d]:->f->c
[e]:->f->d->c
[f]:->g
[g]:->d
1:Insert Vertex
2:Create Link
3:Display Vertices
4:Display Graph
5:DFS Traversal
0:Exit
Enter your choice: 5
Enter start vertex for DFS: a
DFS Traversal: a b e f g c d
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define INF 9999
int graph[MAX][MAX];
int parent[MAX];
int vertices = 0;
void initGraph() {
for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX; j++)
graph[i][j] = (i == j) ? 0 : INF;
}
int find(int i) {
while (parent[i])
i = parent[i];
return i;
}
int unionSet(int i, int j) {
int a = find(i);
int b = find(j);
if (a != b) {
parent[b] = a;
return 1;
}
return 0;
}
void addEdge() {
int u, v, w;
printf("Enter source, destination, and weight: ");
scanf("%d%d%d", &u, &v, &w);
graph[u][v] = w;
graph[v][u] = w;
if (u >= vertices) vertices = u + 1;
if (v >= vertices) vertices = v + 1;
}
void displayMatrix() {
printf("\nAdjacency Matrix:\n ");
for (int i = 0; i < vertices; i++)
printf("%3d", i);
printf("\n");
for (int i = 0; i < vertices; i++) {
printf("%2d ", i);
for (int j = 0; j < vertices; j++) {
if (graph[i][j] == INF)
printf(" -");
else
printf("%3d", graph[i][j]);
}
printf("\n");
}
}
void kruskal() {
int min, a, b, u, v, count = 0, cost = 0;
for (int i = 0; i < MAX; i++)
parent[i] = 0;
printf("\nEdges in the Minimum Spanning Tree:\n");
while (count < vertices - 1) {
min = INF;
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
if (graph[i][j] < min && i != j) {
min = graph[i][j];
a = u = i;
b = v = j;
}
}
}
if (unionSet(u, v)) {
printf("(%d -- %d) weight: %d\n", a, b, min);
cost += min;
count++;
}
graph[a][b] = graph[b][a] = INF;
}
printf("Total cost of MST: %d\n", cost);
}
int main() {
int choice;
initGraph();
do {
printf("\n--- Kruskal's Algorithm Using Matrix Menu ---\n");
printf("1. Add Edge\n");
printf("2. Display Adjacency Matrix\n");
printf("3. Find MST using Kruskal's Algorithm\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: addEdge(); break;
case 2: displayMatrix(); break;
case 3: kruskal(); break;
case 0: printf("Exiting...\n"); break;
default: printf("Invalid choice!\n");
}
} while (choice != 0);
return 0;
}
OUTPUT
--- Kruskal's Algorithm Using Matrix Menu ---
1. Add Edge
2. Display Adjacency Matrix
3. Find MST using Kruskal's Algorithm
0. Exit
Enter your choice: 2
Adjacency Matrix:
0 1 2 3
0 0 10 6 5
1 10 0 - 15
2 6 - 0 4
3 5 15 4 0
--- Kruskal's Algorithm Using Matrix Menu ---
1. Add Edge
2. Display Adjacency Matrix
3. Find MST using Kruskal's Algorithm
0. Exit
Enter your choice: 3
Edges in the Minimum Spanning Tree:
(2 -- 3) weight: 4
(0 -- 3) weight: 5
(0 -- 2) weight: 6
Total cost of MST: 15