Data Structure Programs
Data Structure Programs
#include <stdio.h>
int main() {
int graph[nV][nV] = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}
};
floydWarshall(graph);
return 0;
}
Output:
0 3 7 5
2 0 6 4
3 1 0 5
5 3 2 0
Program: (Travelling Salesman Problem)
#include <stdio.h>
#include <stdlib.h>
int main() {
int cost[nV][nV] = {
{0, 7, 3, 12},
{7, 0, 2, 9},
{3, 2, 0, 5},
{12, 9, 5, 0}
};
tspDynamic(cost);
return 0;
}
Output:
0 7 3 12
7 0 2 9
3 2 0 5
12 9 5 0
Program: (Tree Traversals)
#include <stdio.h>
int main() {
struct Node* root = createNode(1);
root->left = createNode(12);
root->right = createNode(9);
root->left->left = createNode(5);
root->left->right = createNode(6);
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* root = createNode(3);
root->next = createNode(0);
root->next->next = createNode(4);
root->next->next->next = createNode(1);
root->next->next->next->next = createNode(2);
root->next->next->next->next->next = createNode(5);
root->next->next->next->next->next->next = createNode(6);
return 0;
}
Output:
#include <stdio.h>
#include <limits.h>
#define NUM_STAGES 3
#define NUM_NODES 4
// Set the cost for the source node in the first stage
cost[0][source] = 0;
return minCost;
}
int main() {
int graph[NUM_STAGES-1][NUM_NODES][NUM_NODES] = {
{
{0, 2, 3, 4},
{1, 0, 1, 3},
{2, 1, 0, 2},
{3, 2, 1, 0}
},
{
{0, 1, 2, 3},
{2, 0, 1, 2},
{3, 1, 0, 1},
{4, 2, 1, 0}
}
};
int source = 0;
int destination = 3;
findMinCost(graph, source, destination);
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int col;
char board[4][4];
int leftrow[4];
int upperDiagonal[7];
int lowerDiagonal[7];
int n;
} Solution;
void solveNQueens(int n) {
Solution s;
s.n = n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
s.board[i][j] = '.';
}
}
for (int i = 0; i < n; i++) {
s.leftrow[i] = 0;
}
for (int i = 0; i < 2 * n - 1; i++) {
s.upperDiagonal[i] = 0;
s.lowerDiagonal[i] = 0;
}
s.col = 0;
solve(0, &s, n);
}
int main() {
int n = 4; // we are taking 4*4 grid and 4 queens
solveNQueens(n);
return 0;
}
Output:
Arrangement 0:
..Q.
Q...
...Q
.Q..
Arrangement 0:
.Q..
...Q
Q...
..Q.
Program: (Sum of Subsets)
#include <stdio.h>
#include <stdlib.h>
static int total_nodes;
void printValues(int A[], int size){
for (int i = 0; i < size; i++) {
printf("%*d", 5, A[i]);
}
printf("\n");
}
void subset_sum(int s[], int t[], int s_size, int t_size, int sum, int ite, int const target_sum){
total_nodes++;
if (target_sum == sum) {
printValues(t, t_size);
subset_sum(s, t, s_size, t_size - 1, sum - s[ite], ite + 1, target_sum);
return;
}
else {
for (int i = ite; i < s_size; i++) {
t[t_size] = s[i];
subset_sum(s, t, s_size, t_size + 1, sum + s[i], i + 1, target_sum);
}
}
}
void generateSubsets(int s[], int size, int target_sum){
int* tuplet_vector = (int*)malloc(size * sizeof(int));
subset_sum(s, tuplet_vector, size, 0, 0, 0, target_sum);
free(tuplet_vector);
}
int main(){
int set[] = { 5, 6, 12 , 54, 2 , 20 , 15 };
int size = sizeof(set) / sizeof(set[0]);
printf("The set is ");
printValues(set , size);
generateSubsets(set, size, 25);
printf("Total Nodes generated %d\n", total_nodes);
return 0;
}
Output:
The set is 5 6 12 54 2 20 15
5 6 12 2
5 20
Total Nodes generated 127
Graph Coloring Problem Program:
#include <stdio.h>
#include <stdbool.h>
#define V 4
return false;
}
// Main function to solve the m-coloring problem
void graphColoring(bool graph[V][V], int m)
{
int color[V];
for (int i = 0; i < V; i++)
color[i] = 0;
// Sample input
int main()
{
// Graph represented as adjacency matrix
bool graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}};
// Number of colors
int m = 3;
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
if (!found) {
pageFaults++;
frame[index] = pages[i];
index = (index + 1) % frames; // Move to the next frame in a circular manner
}
}
Output:
#include<stdio.h>
if (!found) {
pageFaults++;
if (replaceIndex == -1) {
for (int j = 0; j < frames; j++) {
if (frame[j] == -1) {
replaceIndex = j;
break;
}
}
}
frame[replaceIndex] = pages[i];
}
}
int main() {
int frames = 3;
int pages[] = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
int n = sizeof(pages) / sizeof(pages[0]);
Output:
#include<stdio.h>
if (!found) {
pageFaults++;
int lruIndex = 0;
int minTime = time[0];
int main() {
int frames = 3;
int pages[] = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
int n = sizeof(pages) / sizeof(pages[0]);
Output:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 5
sem_t empty;
sem_t full;
pthread_mutex_t mutex;
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
printf("Produced: %d\n", item);
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
return NULL;
}
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
return NULL;
}
int main() {
pthread_t prod, cons;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_join(prod, NULL);
pthread_join(cons, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
Output:
Produced: 45
Consumed: 45
Produced: 66
Consumed: 66
Produced: 74
Consumed: 74
Produced: 51
Consumed: 51
Produced: 42
Consumed: 42
Produced: 33
Consumed: 33
Produced: 28
Consumed: 28
Produced: 39
Consumed: 39
Produced: 82
Consumed: 82
Produced: 12
Consumed: 12
Reader Writer Program:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
sem_t rw_mutex;
pthread_mutex_t mutex;
int read_count = 0;
pthread_mutex_lock(&mutex);
read_count--;
if (read_count == 0)
sem_post(&rw_mutex);
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t r[5], w[5];
int ids[5] = {1, 2, 3, 4, 5};
sem_init(&rw_mutex, 0, 1);
pthread_mutex_init(&mutex, NULL);
sem_destroy(&rw_mutex);
pthread_mutex_destroy(&mutex);
return 0;
}
Output:
Reader 1 is reading
Reader 2 is reading
Reader 3 is reading
Reader 4 is reading
Reader 5 is reading
Writer 1 is writing
Writer 2 is writing
Writer 3 is writing
Writer 4 is writing
Writer 5 is writing
Dining Philosopher Program:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_PHILOSOPHERS 5
sem_t forks[NUM_PHILOSOPHERS];
sem_wait(&forks[left]);
sem_wait(&forks[right]);
sem_post(&forks[right]);
sem_post(&forks[left]);
}
return NULL;
}
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int ids[NUM_PHILOSOPHERS];
return 0;
}
Output:
Philosopher 0 is thinking
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 0 is eating
Philosopher 1 is eating
Philosopher 2 is eating
Philosopher 3 is eating
Philosopher 4 is eating
Philosopher 0 is thinking
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 0 is eating
Philosopher 1 is eating
Philosopher 2 is eating
Philosopher 3 is eating
Philosopher 4 is eating
Philosopher 0 is thinking
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 0 is eating
Philosopher 1 is eating
Philosopher 2 is eating
Philosopher 3 is eating
Philosopher 4 is eating
Disk Scheduling FCFS Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int requests[] = { 72, 160 , 33, 130, 14, 6, 180};
int n = sizeof(requests) / sizeof(requests[0]);
int head = 50;
FCFS(requests, n, head);
return 0;
}
Output:
Seek Sequence is:
72 160 33 130 14 6 180
Total number of seek operations = 632
Shortest Seek Time First Program:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include<limits.h>
int main() {
int requests[] = { 72, 160 , 33, 130, 14, 6, 180};
int n = sizeof(requests) / sizeof(requests[0]);
int head = 50;
SSTF(requests, n, head);
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
void SCAN(int requests[], int n, int head, int direction, int disk_size) {
int seek_count = 0;
int distance, cur_track;
int size = n + 2;
int seek_sequence[size];
int index = 0;
seek_sequence[index++] = head;
if (direction == 1) {
for (int i = 0; i < n; i++) {
if (requests[i] < head) {
seek_sequence[index++] = requests[i];
}
}
} else {
for (int i = n - 1; i >= 0; i--) {
if (requests[i] > head) {
seek_sequence[index++] = requests[i];
}
}
}
int main() {
int requests[] = { 72, 160 , 33, 130, 14, 6, 180};
int n = sizeof(requests) / sizeof(requests[0]);
int head = 50;
int disk_size = 200;
int direction = 1; // 1 for high, 0 for low
return 0;
}
Output: