0% found this document useful (0 votes)
0 views

Hard_C_Programs (1)

The document contains multiple C programming examples demonstrating various algorithms and data structures. Key implementations include a Sudoku solver using backtracking, a Knight's tour problem solver, Huffman coding for data compression, an LRU cache, multithreaded matrix multiplication, expression tree evaluation, a file system simulation, longest common subsequence calculation, counting set bits, and a simple shell command interpreter. Each example illustrates fundamental programming concepts and techniques in C.

Uploaded by

siyamkhan1913
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Hard_C_Programs (1)

The document contains multiple C programming examples demonstrating various algorithms and data structures. Key implementations include a Sudoku solver using backtracking, a Knight's tour problem solver, Huffman coding for data compression, an LRU cache, multithreaded matrix multiplication, expression tree evaluation, a file system simulation, longest common subsequence calculation, counting set bits, and a simple shell command interpreter. Each example illustrates fundamental programming concepts and techniques in C.

Uploaded by

siyamkhan1913
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Sudoku Solver Using Backtracking

#include <stdio.h>
#include <stdbool.h>

#define N 9

bool isSafe(int grid[N][N], int row, int col, int num) {


for (int x = 0; x < N; x++)
if (grid[row][x] == num || grid[x][col] == num)
return false;
int startRow = row - row % 3, startCol = col - col % 3;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (grid[i + startRow][j + startCol] == num)
return false;
return true;
}

bool solveSudoku(int grid[N][N], int row, int col) {


if (row == N - 1 && col == N)
return true;
if (col == N) {
row++;
col = 0;
}
if (grid[row][col] > 0)
return solveSudoku(grid, row, col + 1);
for (int num = 1; num <= N; num++) {
if (isSafe(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid, row, col + 1))
return true;
grid[row][col] = 0;
}
}
return false;
}

void printGrid(int grid[N][N]) {


for (int r = 0; r < N; r++) {
for (int d = 0; d < N; d++)
printf("%2d", grid[r][d]);
printf("\n");
}
}

int main() {
int grid[N][N] = {
{5,3,0,0,7,0,0,0,0},{6,0,0,1,9,5,0,0,0},{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},{4,0,0,8,0,3,0,0,1},{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},{0,0,0,4,1,9,0,0,5},{0,0,0,0,8,0,0,7,9}
};
if (solveSudoku(grid, 0, 0))
printGrid(grid);
else
printf("No solution exists");
return 0;
}
2. Knight's Tour Problem

#include <stdio.h>
#define N 8

int isSafe(int x, int y, int sol[N][N]) {


return (x >= 0 && x < N && y >= 0 && y < N && sol[x][y] == -1);
}

void printSolution(int sol[N][N]) {


for (int x = 0; x < N; x++) {
for (int y = 0; y < N; y++)
printf(" %2d ", sol[x][y]);
printf("\n");
}
}

int solveKTUtil(int x, int y, int movei, int sol[N][N], int xMove[], int yMove[]) {
if (movei == N*N)
return 1;
for (int k = 0; k < 8; k++) {
int next_x = x + xMove[k], next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol)) {
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei+1, sol, xMove, yMove))
return 1;
else
sol[next_x][next_y] = -1;
}
}
return 0;
}

void solveKT() {
int sol[N][N], xMove[8] = {2,1,-1,-2,-2,-1,1,2}, yMove[8] = {1,2,2,1,-1,-2,-2,-1};
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
sol[x][y] = -1;
sol[0][0] = 0;
if (!solveKTUtil(0, 0, 1, sol, xMove, yMove))
printf("Solution does not exist");
else
printSolution(sol);
}

int main() {
solveKT();
return 0;
}
3. Huffman Coding for Data Compression

#include <stdio.h>
#include <stdlib.h>

struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};

struct MinHeap {
unsigned size, capacity;
struct MinHeapNode** array;
};

struct MinHeapNode* newNode(char data, unsigned freq) {


struct MinHeapNode* temp = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data; temp->freq = freq;
return temp;
}

// Skipping utility functions for brevity...

void HuffmanCodes(char data[], int freq[], int size) {


// Logic for Huffman Tree construction and printing codes
// Not fully shown due to space
printf("Huffman Coding is complex and requires heap management.\n");
}

int main() {
char arr[] = {'a','b','c','d','e','f'};
int freq[] = {5,9,12,13,16,45};
HuffmanCodes(arr, freq, 6);
return 0;
}
4. LRU Cache Implementation (Linked List)

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int key;
struct Node* next;
} Node;

Node* front = NULL, *rear = NULL;


int capacity = 3, size = 0;

void enqueue(int key) {


Node* temp = (Node*)malloc(sizeof(Node));
temp->key = key;
temp->next = NULL;
if (size == capacity) {
Node* tmp = front;
front = front->next;
free(tmp);
size--;
}
if (!rear) front = rear = temp;
else rear->next = temp, rear = temp;
size++;
}

void display() {
Node* temp = front;
while (temp) {
printf("%d ", temp->key);
temp = temp->next;
}
printf("\n");
}

int main() {
enqueue(1); enqueue(2); enqueue(3);
display();
enqueue(4); // LRU: 1 removed
display();
return 0;
}
5. Multithreaded Matrix Multiplication

#include <pthread.h>
#include <stdio.h>

#define SIZE 2
int A[SIZE][SIZE] = {{1,2},{3,4}}, B[SIZE][SIZE] = {{5,6},{7,8}}, C[SIZE][SIZE];
void* multiply(void* arg) {
int i = *(int*)arg;
for (int j = 0; j < SIZE; j++) {
C[i][j] = 0;
for (int k = 0; k < SIZE; k++)
C[i][j] += A[i][k] * B[k][j];
}
return NULL;
}

int main() {
pthread_t threads[SIZE];
int thread_ids[SIZE];
for (int i = 0; i < SIZE; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, multiply, &thread_ids[i]);
}
for (int i = 0; i < SIZE; i++) pthread_join(threads[i], NULL);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
6. Expression Tree Evaluation

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Node {


char data;
struct Node *left, *right;
} Node;

Node* newNode(char data) {


Node* node = (Node*)malloc(sizeof(Node));
node->data = data; node->left = node->right = NULL;
return node;
}

int eval(Node* root) {


if (!root) return 0;
if (!root->left && !root->right) return root->data - '0';
int l = eval(root->left), r = eval(root->right);
if (root->data == '+') return l + r;
if (root->data == '-') return l - r;
if (root->data == '*') return l * r;
if (root->data == '/') return l / r;
return 0;
}

int main() {
Node* root = newNode('*');
root->left = newNode('+');
root->right = newNode('+');
root->left->left = newNode('3');
root->left->right = newNode('2');
root->right->left = newNode('4');
root->right->right = newNode('5');
printf("Result = %d\n", eval(root));
return 0;
}
7. File System Simulation

#include <stdio.h>
#include <string.h>

struct Dir {
char name[10];
struct Dir* sub[5];
int sub_count;
};

void create(struct Dir* parent, char* name) {


struct Dir* d = malloc(sizeof(struct Dir));
strcpy(d->name, name);
d->sub_count = 0;
parent->sub[parent->sub_count++] = d;
}

void list(struct Dir* d) {


for (int i = 0; i < d->sub_count; i++)
printf("%s\n", d->sub[i]->name);
}

int main() {
struct Dir root;
strcpy(root.name, "root");
root.sub_count = 0;
create(&root, "home");
create(&root, "var");
list(&root);
return 0;
}
8. Longest Common Subsequence (DP)

#include <stdio.h>
#include <string.h>

int max(int a, int b) { return (a > b)? a : b; }

int lcs(char* X, char* Y, int m, int n) {


int L[m+1][n+1];
for (int i=0; i<=m; i++) {
for (int j=0; j<=n; j++) {
if (i==0 || j==0) L[i][j] = 0;
else if (X[i-1]==Y[j-1]) L[i][j] = L[i-1][j-1] + 1;
else L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[m][n];
}

int main() {
char X[] = "AGGTAB", Y[] = "GXTXAYB";
printf("LCS length is %d", lcs(X, Y, strlen(X), strlen(Y)));
return 0;
}
9. Count Set Bits (Kernighan's Algorithm)

#include <stdio.h>

int countSetBits(int n) {
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}

int main() {
int n = 29;
printf("Set bits in %d is %d\n", n, countSetBits(n));
return 0;
}
10. Simple Shell (Command Interpreter)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define MAX 100

int main() {
char input[MAX];
char *args[10];
while (1) {
printf("shell> ");
fgets(input, MAX, stdin);
input[strcspn(input, "\n")] = 0;
if (strcmp(input, "exit") == 0) break;
int i = 0;
args[i] = strtok(input, " ");
while (args[i] != NULL) args[++i] = strtok(NULL, " ");
if (fork() == 0) execvp(args[0], args);
else wait(NULL);
}
return 0;
}

You might also like