0% found this document useful (0 votes)
2 views22 pages

C Lang

The document contains multiple C programs demonstrating various data structures and algorithms, including linked lists, doubly linked lists, double-ended queues, polynomials, GCD calculation, and binary search trees. Each section includes functions for creation, searching, reversing, concatenating, and displaying the data structures, along with main functions to test their functionality. The document serves as a comprehensive guide for implementing and manipulating these data structures in C.

Uploaded by

foheke6586
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)
2 views22 pages

C Lang

The document contains multiple C programs demonstrating various data structures and algorithms, including linked lists, doubly linked lists, double-ended queues, polynomials, GCD calculation, and binary search trees. Each section includes functions for creation, searching, reversing, concatenating, and displaying the data structures, along with main functions to test their functionality. The document serves as a comprehensive guide for implementing and manipulating these data structures in C.

Uploaded by

foheke6586
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/ 22

// 1. Implement linked list with search, reverse, and concatenate functions.

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

struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to search for a number


int search(struct Node* head, int key) {
struct Node* temp = head;
while (temp) {
if (temp->data == key)
return 1; // Found
temp = temp->next;
}
return 0; // Not found
}

// Function to reverse the list


struct Node* reverse(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}

// Function to concatenate two lists


struct Node* concatenate(struct Node* head1, struct Node* head2) {
if (!head1) return head2;
struct Node* temp = head1;
while (temp->next) {
temp = temp->next;
}
temp->next = head2;
return head1;
}

// Function to print the list


void printList(struct Node* head) {
struct Node* temp = head;
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node* list1 = createNode(1);
list1->next = createNode(2);
list1->next->next = createNode(3);

struct Node* list2 = createNode(4);


list2->next = createNode(5);

printf("List 1: ");
printList(list1);
printf("List 2: ");
printList(list2);

printf("Search 2 in List 1: %s\n", search(list1, 2) ? "Found" : "Not


Found");
list1 = reverse(list1);
printf("Reversed List 1: ");
printList(list1);

struct Node* concatenated = concatenate(list1, list2);


printf("Concatenated List: ");
printList(concatenated);
return 0;
}

// 2. Implement doubly linked list with search, reverse, and concatenate


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

struct DNode {
int data;
struct DNode* prev;
struct DNode* next;
};

// Function to create a new node


struct DNode* createDNode(int data) {
struct DNode* newNode = (struct DNode*)malloc(sizeof(struct DNode));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Function to search for a number


int searchD(struct DNode* head, int key) {
struct DNode* temp = head;
while (temp) {
if (temp->data == key)
return 1; // Found
temp = temp->next;
}
return 0; // Not found
}

// Function to reverse the list


struct DNode* reverseD(struct DNode* head) {
struct DNode* temp = NULL;
struct DNode* current = head;
while (current) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
return temp ? temp->prev : NULL;
}

// Function to concatenate two lists


struct DNode* concatenateD(struct DNode* head1, struct DNode* head2) {
if (!head1) return head2;
struct DNode* temp = head1;
while (temp->next) {
temp = temp->next;
}
temp->next = head2;
if (head2) head2->prev = temp;
return head1;
}
// Function to print the list
void printDList(struct DNode* head) {
struct DNode* temp = head;
while (temp) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct DNode* dlist1 = createDNode(1);
dlist1->next = createDNode(2);
dlist1->next->prev = dlist1;
dlist1->next->next = createDNode(3);
dlist1->next->next->prev = dlist1->next;

struct DNode* dlist2 = createDNode(4);


dlist2->next = createDNode(5);
dlist2->next->prev = dlist2;

printf("Doubly List 1: ");


printDList(dlist1);
printf("Doubly List 2: ");
printDList(dlist2);

printf("Search 2 in Doubly List 1: %s\n", searchD(dlist1, 2) ? "Found" :


"Not Found");
dlist1 = reverseD(dlist1);
printf("Reversed Doubly List 1: ");
printDList(dlist1);

struct DNode* concatenatedD = concatenateD(dlist1, dlist2);


printf("Concatenated Doubly List: ");
printDList(concatenatedD);
return 0;
}

// 3. Double-ended queue implementation using linked list.


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

struct DequeNode {
int data;
struct DequeNode* next;
struct DequeNode* prev;
};

struct Deque {
struct DequeNode* front;
struct DequeNode* rear;
};

struct Deque* createDeque() {


struct Deque* dq = (struct Deque*)malloc(sizeof(struct Deque));
dq->front = dq->rear = NULL;
return dq;
}

// Function to add to front


void addFront(struct Deque* dq, int data) {
struct DequeNode* newNode = (struct DequeNode*)malloc(sizeof(struct
DequeNode));
newNode->data = data;
newNode->next = dq->front;
newNode->prev = NULL;
if (dq->front) dq->front->prev = newNode;
dq->front = newNode;
if (!dq->rear) dq->rear = newNode;
}

// Function to add to rear


void addRear(struct Deque* dq, int data) {
struct DequeNode* newNode = (struct DequeNode*)malloc(sizeof(struct
DequeNode));
newNode->data = data;
newNode->prev = dq->rear;
newNode->next = NULL;
if (dq->rear) dq->rear->next = newNode;
dq->rear = newNode;
if (!dq->front) dq->front = newNode;
}

// Function to remove from front


int removeFront(struct Deque* dq) {
if (!dq->front) return -1; // Empty deque
struct DequeNode* temp = dq->front;
int data = temp->data;
dq->front = dq->front->next;
if (dq->front) dq->front->prev = NULL;
else dq->rear = NULL;
free(temp);
return data;
}
// Function to remove from rear
int removeRear(struct Deque* dq) {
if (!dq->rear) return -1; // Empty deque
struct DequeNode* temp = dq->rear;
int data = temp->data;
dq->rear = dq->rear->prev;
if (dq->rear) dq->rear->next = NULL;
else dq->front = NULL;
free(temp);
return data;
}

// Function to display deque


void displayDeque(struct Deque* dq) {
struct DequeNode* temp = dq->front;
while (temp) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Deque* dq = createDeque();
addFront(dq, 10);
addRear(dq, 20);
addFront(dq, 5);
displayDeque(dq);
printf("Removed Front: %d\n", removeFront(dq));
printf("Removed Rear: %d\n", removeRear(dq));
displayDeque(dq);
return 0;
}

// 4. WAP to scan a polynomial using linked list.


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

struct PolyNode {
int coeff;
int power;
struct PolyNode* next;
};

struct PolyNode* createPolyNode(int coeff, int power) {


struct PolyNode* newNode = (struct PolyNode*)malloc(sizeof(struct
PolyNode));
newNode->coeff = coeff;
newNode->power = power;
newNode->next = NULL;
return newNode;
}

void printPolynomial(struct PolyNode* head) {


struct PolyNode* temp = head;
while (temp) {
printf("%dx^%d", temp->coeff, temp->power);
if (temp->next)
printf(" + ");
temp = temp->next;
}
printf("\n");
}

int main() {
int n, coeff, power;
printf("Enter number of terms: ");
scanf("%d", &n);
struct PolyNode* poly = NULL;
struct PolyNode* last = NULL;

for (int i = 0; i < n; ++i) {


printf("Enter coefficient and power: ");
scanf("%d %d", &coeff, &power);
struct PolyNode* newNode = createPolyNode(coeff, power);
if (!poly)
poly = newNode;
else
last->next = newNode;
last = newNode;
}

printf("Polynomial: ");
printPolynomial(poly);
return 0;
}

// 5. WAP to calculate GCD using iteration.


#include <stdio.h>

int gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));
return 0;
}

// 6. Binary Search Tree with All Operations

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

// Node structure for BST


struct TreeNode {
int data;
struct TreeNode *left, *right;
};

// Create a new node


struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct
TreeNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Recursive Insertion
struct TreeNode* insertRecursive(struct TreeNode* root, int data) {
if (root == NULL) return createNode(data);
if (data < root->data)
root->left = insertRecursive(root->left, data);
else if (data > root->data)
root->right = insertRecursive(root->right, data);
return root;
}

// Iterative Insertion
struct TreeNode* insertIterative(struct TreeNode* root, int data) {
struct TreeNode* newNode = createNode(data);
if (root == NULL) return newNode;

struct TreeNode* parent = NULL, *current = root;


while (current) {
parent = current;
if (data < current->data)
current = current->left;
else
current = current->right;
}

if (data < parent->data) parent->left = newNode;


else parent->right = newNode;

return root;
}

struct TreeNode* findMax(struct TreeNode* root) {


while (root->right != NULL) {
root = root->right;
}
return root;
}

struct TreeNode* deleteByCopying(struct TreeNode* root, int key) {


if (!root) return root;

if (key < root->data)


root->left = deleteByCopying(root->left, key);
else if (key > root->data)
root->right = deleteByCopying(root->right, key);
else {
if (!root->left) {
struct TreeNode* temp = root->right;
free(root);
return temp;
} else if (!root->right) {
struct TreeNode* temp = root->left;
free(root);
return temp;
} else {
struct TreeNode* maxLeft = findMax(root->left);
root->data = maxLeft->data;
root->left = deleteByCopying(root->left, maxLeft->data);
}
}
return root;
}

struct TreeNode* deleteByMerging(struct TreeNode* root, int key) {


if (!root) return root;

if (key < root->data)


root->left = deleteByMerging(root->left, key);
else if (key > root->data)
root->right = deleteByMerging(root->right, key);
else {
struct TreeNode* temp = NULL;
if (root->left == NULL) {
temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
temp = root->left;
free(root);
return temp;
} else {
temp = root->left;
while (temp->right != NULL) {
temp = temp->right;
}
temp->right = root->right;
temp = root->left;
free(root);
return temp;
}
}
return root;
}

// Search for a number in BST


struct TreeNode* search(struct TreeNode* root, int key) {
if (root == NULL || root->data == key) return root;
if (key < root->data) return search(root->left, key);
return search(root->right, key);
}

// Traversals
void inorderTraversal(struct TreeNode* root) {
if (root) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
void preorderTraversal(struct TreeNode* root) {
if (root) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}

void postorderTraversal(struct TreeNode* root) {


if (root) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}

// Count leaf and non-leaf nodes


void countNodes(struct TreeNode* root, int* leafCount, int* nonLeafCount) {
if (root == NULL) return;
if (root->left == NULL && root->right == NULL)
(*leafCount)++;
else
(*nonLeafCount)++;
countNodes(root->left, leafCount, nonLeafCount);
countNodes(root->right, leafCount, nonLeafCount);
}

// Find height of the tree


int findHeight(struct TreeNode* root) {
if (root == NULL) return 0;
int leftHeight = findHeight(root->left);
int rightHeight = findHeight(root->right);
return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}

// Mirror the tree


struct TreeNode* mirrorTree(struct TreeNode* root) {
if (root == NULL) return NULL;
struct TreeNode* temp = root->left;
root->left = mirrorTree(root->right);
root->right = mirrorTree(temp);
return root;
}

void levelOrderTraversal(struct TreeNode* root) {


if (!root) return;
struct TreeNode* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;

while (front < rear) {


struct TreeNode* current = queue[front++];
printf("%d ", current->data);
if (current->left) queue[rear++] = current->left;
if (current->right) queue[rear++] = current->right;
}
printf("\n");
}

int areBSTsEqual(struct TreeNode* root1, struct TreeNode* root2) {


if (!root1 && !root2) return 1; // Both are NULL
if (!root1 || !root2) return 0; // One is NULL and the other isn't

return (root1->data == root2->data) &&


areBSTsEqual(root1->left, root2->left) &&
areBSTsEqual(root1->right, root2->right);
}

int main() {
struct TreeNode* root = NULL;

root = insertRecursive(root, 50);


root = insertRecursive(root, 30);
root = insertRecursive(root, 70);
root = insertRecursive(root, 20);
root = insertRecursive(root, 40);

printf("Inorder Traversal: ");


inorderTraversal(root);
printf("\n");

printf("Level-order Traversal: ");


levelOrderTraversal(root);

printf("Delete 30 (by Copying): ");


root = deleteByCopying(root, 30);
inorderTraversal(root);
printf("\n");

printf("Delete 70 (by Merging): ");


root = deleteByMerging(root, 70);
inorderTraversal(root);
printf("\n");
// Create a second tree
struct TreeNode* root2 = NULL;
root2 = insertRecursive(root2, 50);
root2 = insertRecursive(root2, 20);
root2 = insertRecursive(root2, 40);

if (areBSTsEqual(root, root2))
printf("The two BSTs are equal.\n");
else
printf("The two BSTs are not equal.\n");

return 0;
}

// 7. Convert Sparse Matrix to Non-Zero Form and Vice Versa

#include <stdio.h>

#define MAX 100

void sparseToNonZero(int sparse[][3], int size) {


printf("Non-Zero Form:\n");
for (int i = 0; i < size; i++) {
printf("Row: %d, Col: %d, Value: %d\n", sparse[i][0], sparse[i][1],
sparse[i][2]);
}
}

void nonZeroToSparse(int matrix[][MAX], int rows, int cols) {


int sparse[MAX][3];
int k = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {
sparse[k][0] = i;
sparse[k][1] = j;
sparse[k][2] = matrix[i][j];
k++;
}
}
}
sparseToNonZero(sparse, k);
}

int main() {
int matrix[4][4] = {
{0, 0, 3, 0},
{0, 5, 0, 0},
{0, 0, 0, 9},
{0, 0, 0, 0}};

nonZeroToSparse(matrix, 4, 4);
return 0;
}

// 8. Reverse Stack Using Additional Stack


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

struct Stack {
int* arr;
int top;
int capacity;
};

struct Stack* createStack(int capacity) {


struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->arr = (int*)malloc(capacity * sizeof(int));
return stack;
}

int isEmpty(struct Stack* stack) {


return stack->top == -1;
}

void push(struct Stack* stack, int data) {


stack->arr[++stack->top] = data;
}

int pop(struct Stack* stack) {


return stack->arr[stack->top--];
}

void reverseStack(struct Stack* stack) {


struct Stack* tempStack = createStack(stack->capacity);
while (!isEmpty(stack)) {
push(tempStack, pop(stack));
}
while (!isEmpty(tempStack)) {
push(stack, pop(tempStack));
}
}
void printStack(struct Stack* stack) {
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}

int main() {
struct Stack* stack = createStack(5);
push(stack, 1);
push(stack, 2);
push(stack, 3);

printf("Original Stack: ");


printStack(stack);
reverseStack(stack);
printf("Reversed Stack: ");
printStack(stack);

return 0;
}

// 9. Reverse Stack Using Additional Queue


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

struct Queue {
int* arr;
int front, rear, size;
int capacity;
};

struct Queue* createQueue(int capacity) {


struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = 0;
queue->size = 0;
queue->rear = capacity - 1;
queue->arr = (int*)malloc(capacity * sizeof(int));
return queue;
}

int isEmptyQueue(struct Queue* queue) {


return queue->size == 0;
}
void enqueue(struct Queue* queue, int item) {
queue->rear = (queue->rear + 1) % queue->capacity;
queue->arr[queue->rear] = item;
queue->size++;
}

int dequeue(struct Queue* queue) {


int item = queue->arr[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size--;
return item;
}

void reverseStackUsingQueue(struct Stack* stack) {


struct Queue* queue = createQueue(stack->capacity);
while (!isEmpty(stack)) {
enqueue(queue, pop(stack));
}
while (!isEmptyQueue(queue)) {
push(stack, dequeue(queue));
}
}

int main() {
struct Stack* stack = createStack(5);
push(stack, 1);
push(stack, 2);
push(stack, 3);

printf("Original Stack: ");


printStack(stack);
reverseStackUsingQueue(stack);
printf("Reversed Stack using Queue: ");
printStack(stack);

return 0;
}

// 10. Diagonal Matrix Using One-Dimensional Array


#include <stdio.h>

#define MAX 100

void setDiagonal(int diag[], int size, int row, int col, int value) {
if (row == col)
diag[row] = value;
}
int getDiagonal(int diag[], int size, int row, int col) {
if (row == col)
return diag[row];
return 0;
}

int main() {
int n = 5;
int diagonal[MAX] = {0};

setDiagonal(diagonal, n, 0, 0, 1);
setDiagonal(diagonal, n, 1, 1, 2);
setDiagonal(diagonal, n, 2, 2, 3);
setDiagonal(diagonal, n, 3, 3, 4);
setDiagonal(diagonal, n, 4, 4, 5);

printf("Diagonal Matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", getDiagonal(diagonal, n, i, j));
}
printf("\n");
}

return 0;
}

// 11. Threaded Binary Tree


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

typedef struct Node {


int data;
struct Node *left, *right;
int isThreaded;
} Node;

// Function to create a new node


Node* createNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = node->right = NULL;
node->isThreaded = 0;
return node;
}
// Function to insert a node in a threaded binary tree
Node* insert(Node* root, int key) {
if (root == NULL)
return createNode(key);

Node* parent = NULL, *current = root;

while (current != NULL) {


if (key == current->data) {
printf("Duplicate keys are not allowed.\n");
return root;
}
parent = current;

if (key < current->data) {


if (current->left == NULL)
break;
current = current->left;
} else {
if (current->isThreaded || current->right == NULL)
break;
current = current->right;
}
}

Node* newNode = createNode(key);

if (key < parent->data) {


parent->left = newNode;
} else {
newNode->right = parent->right;
newNode->isThreaded = 1;
parent->right = newNode;
}
return root;
}

// Inorder traversal of a threaded binary tree


void inorderTraversal(Node* root) {
if (root == NULL)
return;

Node* current = root;

while (current->left != NULL)


current = current->left;

while (current != NULL) {


printf("%d ", current->data);

if (current->isThreaded)
current = current->right;
else {
current = current->right;
while (current && current->left != NULL)
current = current->left;
}
}
}

int main() {
Node* root = NULL;
root = insert(root, 20);
root = insert(root, 10);
root = insert(root, 30);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 25);

printf("Inorder Traversal of Threaded Binary Tree:\n");


inorderTraversal(root);

return 0;
}

// 12. AVL Tree Operations


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

typedef struct Node {


int data;
struct Node *left, *right;
int height;
} Node;

// Function to get the height of the tree


int height(Node* node) {
return node ? node->height : 0;
}

// Function to create a new node


Node* createNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = node->right = NULL;
node->height = 1;
return node;
}

// Right rotation
Node* rotateRight(Node* y) {
Node* x = y->left;
Node* T2 = x->right;

x->right = y;
y->left = T2;

y->height = 1 + (height(y->left) > height(y->right) ? height(y->left) :


height(y->right));
x->height = 1 + (height(x->left) > height(x->right) ? height(x->left) :
height(x->right));

return x;
}

// Left rotation
Node* rotateLeft(Node* x) {
Node* y = x->right;
Node* T2 = y->left;

y->left = x;
x->right = T2;

x->height = 1 + (height(x->left) > height(x->right) ? height(x->left) :


height(x->right));
y->height = 1 + (height(y->left) > height(y->right) ? height(y->left) :
height(y->right));

return y;
}

// Get balance factor


int getBalance(Node* node) {
return node ? height(node->left) - height(node->right) : 0;
}

// Function to insert a node in AVL tree


Node* insert(Node* node, int key) {
if (node == NULL)
return createNode(key);

if (key < node->data)


node->left = insert(node->left, key);
else if (key > node->data)
node->right = insert(node->right, key);
else
return node; // Duplicate keys are not allowed

node->height = 1 + (height(node->left) > height(node->right) ?


height(node->left) : height(node->right));

int balance = getBalance(node);

// Left Left Case


if (balance > 1 && key < node->left->data)
return rotateRight(node);

// Right Right Case


if (balance < -1 && key > node->right->data)
return rotateLeft(node);

// Left Right Case


if (balance > 1 && key > node->left->data) {
node->left = rotateLeft(node->left);
return rotateRight(node);
}

// Right Left Case


if (balance < -1 && key < node->right->data) {
node->right = rotateRight(node->right);
return rotateLeft(node);
}

return node;
}

// Inorder traversal
void inorderTraversal(Node* root) {
if (root) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}

int main() {
Node* root = NULL;

root = insert(root, 10);


root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);

printf("Inorder Traversal of AVL Tree:\n");


inorderTraversal(root);

return 0;
}

You might also like