DSA Lab Copy
DSA Lab Copy
return 0;
}
Output:
EN
2D Array:
12
34
3D Array First Layer:
K
12
34
IC
2. Merge Two 3D Arrays
H
#include <stdio.h>
int main() {
C
return 0;
}
Output:
Merged 3D Array: 1 2
3. Create 3D Array Using Nested Loops
#include <stdio.h>
int main() {
int arr[2][2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
arr[i][j][k] = i + j + k;
return 0;
}
Output:
3D Array Element: 3
4. Polynomial Addition
#include <stdio.h>
void addPolynomials(int a[], int b[], int sum[], int n) {
for (int i = 0; i < n; i++)
EN
sum[i] = a[i] + b[i];
}
int main() {
K
int a[] = {1, 2, 3}, b[] = {4, 5, 6}, sum[3];
addPolynomials(a, b, sum, 3);
IC
printf("Sum of polynomials: %d %d %d\n", sum[0], sum[1], sum[2]);
H
return 0;
C
}
Output:
G
Sum of polynomials: 5 7 9
5. Stack Push and Pop Operations
N
#include <stdio.h>
#define MAX_SIZE 10
YI
void pop() {
if (top == -1) {
printf("Stack Underflow!\n");
return;
}
top--;
}
int main() {
push(10);
push(20);
pop();
printf("Stack top after operations: %d\n", stack[top]);
return 0;
}
Output:
Stack top after operations: 10
6. Queue Implementation (Array)
#include <stdio.h>
#define MAX_SIZE 10
EN
int queue[MAX_SIZE], front = -1, rear = -1;
K
if (rear == MAX_SIZE - 1) {
printf("Queue Overflow!\n");
IC
return;
}
H
if (front == -1) front = 0;
queue[++rear] = val;
C
}
G
void dequeue() {
if (front == -1 || front > rear) {
N
printf("Queue Underflow!\n");
return;
YI
}
front++;
FL
int main() {
enqueue(10);
enqueue(20);
dequeue();
printf("Front element after operations: %d\n", queue[front]);
return 0;
}
Output:
Front element after operations: 20
7. Queue Using Key Module
// Not applicable in C; use array or linked list implementations.
8. Array Operations (Sum, Max)
#include <stdio.h>
int main() {
int n, sum = 0, max;
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
max = arr[0];
EN
for (int i = 1; i < n; i++)
if (arr[i] > max) max = arr[i];
K
return 0;
IC
}
Output:
H
Enter number of elements: 2
Enter elements: 1 2
C
Sum: 3, Max: 2
9. Infix to Postfix Conversion
G
#include <stdio.h>
#include <ctype.h>
N
#include <string.h>
#define MAX_SIZE 100
YI
char stack[MAX_SIZE];
FL
void push(char c) {
if (top == MAX_SIZE - 1) {
printf("Stack Overflow!\n");
return;
}
stack[++top] = c;
}
char pop() {
if (top == -1) {
printf("Stack Underflow!\n");
return '\0';
}
return stack[top--];
}
int precedence(char c) {
if (c == '^') return 3;
if (c == '*' || c == '/') return 2;
if (c == '+' || c == '-') return 1;
return 0;
}
EN
else if (infix[i] == ')') {
while (stack[top] != '(') postfix[j++] = pop();
pop();
} else {
K
while (top != -1 && precedence(stack[top]) >= precedence(infix[i]))
postfix[j++] = pop();
IC
push(infix[i]);
}
H
i++;
C
}
while (top != -1) postfix[j++] = pop();
postfix[j] = '\0';
G
}
N
int main() {
char infix[MAX_SIZE], postfix[MAX_SIZE];
YI
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
Output:
Enter infix expression: a+b*(c^d-e)^(f+g*h)-i
Postfix expression: abcd^e-fgh*+^*+i-
10. Queue Implementation (Array)
#include <stdio.h>
#define MAX_SIZE 10
void dequeue() {
if (front > rear) {
printf("Queue Underflow!\n");
return;
}
front++;
}
EN
int main() {
enqueue(5);
enqueue(10);
K
dequeue();
IC
printf("Queue Front: %d\n", queue[front]);
return 0;
H
}
Output:
C
Queue Front: 10
11. Circular Queue Implementation
G
#include <stdio.h>
#define MAX_SIZE 5
N
void dequeue() {
if (front == -1) {
printf("Queue Underflow!\n");
return;
}
if (front == rear) front = rear = -1;
else front = (front + 1) % MAX_SIZE;
}
int main() {
enqueue(10);
enqueue(20);
dequeue();
printf("Circular Queue Front: %d\n", cq[front]);
return 0;
}
Output:
Circular Queue Front: 20
12. Singly Linked List
#include <stdio.h>
EN
#include <stdlib.h>
struct Node {
int data;
K
struct Node *next;
};
IC
void insert(struct Node **head, int data) {
H
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
C
newNode->next = *head;
*head = newNode;
G
}
N
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL;
insert(&head, 10);
insert(&head, 20);
printList(head);
return 0;
}
Output:
20 -> 10 -> NULL
13. Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *prev, *next;
};
EN
}
K
printf("%d <-> ", head->data);
head = head->next;
IC
}
printf("NULL\n");
H
}
C
int main() {
struct Node *head = NULL;
G
insert(&head, 10);
insert(&head, 20);
N
printList(head);
YI
return 0;
}
FL
Output:
20 <-> 10 <-> NULL
14. Factorial (Recursion)
#include <stdio.h>
int fact(int n) {
return (n <= 1) ? 1 : n * fact(n - 1);
}
int main() {
printf("Factorial(5): %d\n", fact(5));
return 0;
}
Output:
Factorial(5): 120
15. Singly Circular Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
EN
struct Node *temp = *head;
while (temp->next != *head) temp = temp->next;
temp->next = newNode;
newNode->next = *head;
K
}
}
IC
void printList(struct Node *head) {
H
if (head == NULL) return;
C
temp = temp->next;
} while (temp != head);
N
printf("HEAD\n");
}
YI
int main() {
FL
return 0;
}
Output:
10 -> 20 -> HEAD
16. Fibonacci (Recursion)
#include <stdio.h>
int fib(int n) {
return (n <= 1) ? n : fib(n - 1) + fib(n - 2);
}
int main() {
printf("Fibonacci(5): %d\n", fib(5));
return 0;
}
Output:
Fibonacci(5): 5
17. Binary Search
#include <stdio.h>
int bs(int arr[], int n, int x) {
int l = 0, r = n - 1;
while (l <= r) {
int m = (l + r) / 2;
if (arr[m] == x) return m;
else if (arr[m] < x) l = m + 1;
else r = m - 1;
EN
}
return -1;
}
K
int main() {
int arr[] = {1, 2, 3};
IC
printf("Index: %d\n", bs(arr, 3, 2));
return 0;
H
}
Output:
C
Index: 1
18. Selection Sort
G
#include <stdio.h>
void selectionSort(int arr[], int n) {
N
int main() {
int arr[] = {3, 1, 2};
selectionSort(arr, 3);
printf("Selection Sorted: %d %d %d\n", arr[0], arr[1], arr[2]);
return 0;
}
Output:
Selection Sorted: 1 2 3
9. Insertion Sort
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {3, 1, 2};
insertionSort(arr, 3);
EN
printf("Insertion Sorted: %d %d %d\n", arr[0], arr[1], arr[2]);
return 0;
}
Output:
K
Insertion Sorted: 1 2 3
20. Merge Sort
IC
#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
H
int n1 = m - l + 1, n2 = r - m;
int L[n1], R[n2];
C
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
YI
EN
int partition(int arr[], int low, int high) {
int pivot = arr[high], i = low - 1;
for (int j = low; j < high; j++) {
K
if (arr[j] < pivot) swap(&arr[++i], &arr[j]);
}
IC
swap(&arr[i + 1], &arr[high]);
return i + 1;
H
}
C
quicksort(arr, pi + 1, high);
}
YI
}
FL
int main() {
int arr[] = {3, 1, 2};
quicksort(arr, 0, 2);
printf("Quicksort Result: %d %d %d\n", arr[0], arr[1], arr[2]);
return 0;
}
Outside:
Quicksort Result: 1 2 3
22. BFS Algorithm
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int dequeue() {
if (front == -1 || front > rear) return -1;
return queue[front++];
}
EN
while (front != -1 && front <= rear) {
int current = dequeue();
printf("%d ", current);
K
for (int i = 0; i < n; i++) {
if (adj[current][i] && !visited[i]) {
IC
enqueue(i);
visited[i] = 1;
H
}
C
}
}
}
G
int main() {
N
adj[0][2] = adj[2][0] = 1;
adj[1][3] = adj[3][1] = 1;
FL
return 0;
}
Output:
BFS Traversal: 0 1 2 3