C Lang
C Lang
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* list1 = createNode(1);
list1->next = createNode(2);
list1->next->next = createNode(3);
printf("List 1: ");
printList(list1);
printf("List 2: ");
printList(list2);
struct DNode {
int data;
struct DNode* prev;
struct DNode* next;
};
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 DequeNode {
int data;
struct DequeNode* next;
struct DequeNode* prev;
};
struct Deque {
struct DequeNode* front;
struct DequeNode* rear;
};
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;
}
struct PolyNode {
int coeff;
int power;
struct PolyNode* next;
};
int main() {
int n, coeff, power;
printf("Enter number of terms: ");
scanf("%d", &n);
struct PolyNode* poly = NULL;
struct PolyNode* last = NULL;
printf("Polynomial: ");
printPolynomial(poly);
return 0;
}
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;
}
#include <stdio.h>
#include <stdlib.h>
// 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;
return root;
}
// 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);
}
}
int main() {
struct TreeNode* root = NULL;
if (areBSTsEqual(root, root2))
printf("The two BSTs are equal.\n");
else
printf("The two BSTs are not equal.\n");
return 0;
}
#include <stdio.h>
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;
}
struct Stack {
int* arr;
int top;
int capacity;
};
int main() {
struct Stack* stack = createStack(5);
push(stack, 1);
push(stack, 2);
push(stack, 3);
return 0;
}
struct Queue {
int* arr;
int front, rear, size;
int capacity;
};
int main() {
struct Stack* stack = createStack(5);
push(stack, 1);
push(stack, 2);
push(stack, 3);
return 0;
}
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;
}
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);
return 0;
}
// Right rotation
Node* rotateRight(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
// Left rotation
Node* rotateLeft(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
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;
return 0;
}