1.
Array Insertion (Insert 25 at index 2)
#include <stdio.h>
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int n = 5, pos = 2, val = 25;
for (int i = n; i > pos; i--)
arr[i] = arr[i - 1];
arr[pos] = val;
n++;
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
10 20 25 30 40 50
2.Array Deletion (Delete element at index 2)
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5, pos = 2;
for (int i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
n--;
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
10 20 40 50
3.Linear Search (Find 9)
#include <stdio.h>
int main() {
int arr[] = {5, 8, 3, 9, 2}, n = 5, key = 9;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Found at index %d\n", i);
return 0;
}
}
printf("Not Found\n");
return 0;
}
Output:
Found at index 3
4.Binary Search (Find 30)
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int low = 0, high = 4, mid, key = 30;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
printf("Found at index %d\n", mid);
return 0;
} else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
printf("Not Found\n");
return 0;
}
Output:
Found at index 2
5. Bubble Sort
#include <stdio.h>
int main() {
int arr[] = {5, 1, 4, 2, 8}, n = 5;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
12458
6.Selection Sort
#include <stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11}, n = 5;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min])
min = j;
int t = arr[min];
arr[min] = arr[i];
arr[i] = t;
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
11 12 22 25 64
7.Insertion Sort
#include <stdio.h>
int main() {
int arr[] = {12, 11, 13, 5, 6}, n = 5;
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--];
arr[j + 1] = key;
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
5 6 11 12 13
8. Stack Using Array (Push/Pop/Display)
#include <stdio.h>
#define SIZE 5
int stack[SIZE], top = -1;
void push(int val) {
if (top == SIZE - 1)
printf("Stack Overflow\n");
else
stack[++top] = val;
}
void pop() {
if (top == -1)
printf("Stack Underflow\n");
else
printf("Popped: %d\n", stack[top--]);
}
void display() {
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}
int main() {
push(10); push(20); push(30);
display();
pop();
display();
return 0;
}
Output:
30 20 10
Popped: 30
20 10
9. Queue Using Array
#include <stdio.h>
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
void enqueue(int val) {
if (rear == SIZE - 1)
printf("Queue Full\n");
else {
if (front == -1) front = 0;
queue[++rear] = val;
}
}
void dequeue() {
if (front == -1 || front > rear)
printf("Queue Empty\n");
else
printf("Dequeued: %d\n", queue[front+
+]);
}
void display() {
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
int main() {
enqueue(10); enqueue(20); enqueue(30);
display();
dequeue();
display();
return 0;
}
Output:
10 20 30
Dequeued: 10
20 30
10. Singly Linked List – Insertion at Beginning
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
struct Node* newNode;
newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = 10;
newNode->next = head;
head = newNode;
newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = 20;
newNode->next = head;
head = newNode;
printList(head);
return 0;
}
Output:
20 -> 10 -> NULL
🔹 11. Singly Linked List – Deletion from
Beginning
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = (struct
Node*)malloc(sizeof(struct Node));
struct Node* second = (struct
Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = second;
second->data = 20;
second->next = NULL;
printf("Before deletion: ");
printList(head);
struct Node* temp = head;
head = head->next;
free(temp);
printf("After deletion: ");
printList(head);
return 0;
}
Output:
Before deletion: 10 -> 20 -> NULL
After deletion: 20 -> NULL
🔹 12. Singly Linked List Traversal
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
struct Node* newNode;
newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = 10;
newNode->next = head;
head = newNode;
newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = 20;
newNode->next = head;
head = newNode;
printList(head);
return 0;
}
Output:
20 -> 10 -> NULL
🔹 13. Circular Queue using Array
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
void enqueue(int val) {
if ((front == 0 && rear == SIZE - 1) || (rear +
1 == front)) {
printf("Queue is Full\n");
return;
}
if (front == -1) front = rear = 0;
else if (rear == SIZE - 1) rear = 0;
else rear++;
queue[rear] = val;
}
void dequeue() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}
printf("Dequeued: %d\n", queue[front]);
if (front == rear) front = rear = -1;
else if (front == SIZE - 1) front = 0;
else front++;
}
void display() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) break;
i = (i + 1) % SIZE;
}
printf("\n");
}
int main() {
enqueue(10); enqueue(20); enqueue(30);
enqueue(40);
display();
dequeue();
display();
return 0;
}
Output:
10 20 30 40
Dequeued: 10
20 30 40
🔹 14. Stack using Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;
void push(int val) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = top;
top = newNode;
}
void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return;
}
printf("Popped: %d\n", top->data);
struct Node* temp = top;
top = top->next;
free(temp);
}
void display() {
struct Node* temp = top;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
push(10); push(20); push(30);
display();
pop();
display();
return 0;
}
Output:
30 20 10
Popped: 30
20 10
🔹 15. Queue using Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node *front = NULL, *rear = NULL;
void enqueue(int val) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (rear == NULL)
front = rear = newNode;
else {
rear->next = newNode;
rear = newNode;
}
}
void dequeue() {
if (front == NULL) {
printf("Queue is Empty\n");
return;
}
printf("Dequeued: %d\n", front->data);
struct Node* temp = front;
front = front->next;
free(temp);
}
void display() {
struct Node* temp = front;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
enqueue(10); enqueue(20); enqueue(30);
display();
dequeue();
display();
return 0;
}
Output:
10 20 30
Dequeued: 10
20 30
🔹 16. Binary Tree Creation
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
struct Node* createNode(int val) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->left = newNode->right = NULL;
return newNode;
}
int main() {
struct Node* root = createNode(10);
root->left = createNode(20);
root->right = createNode(30);
printf("Tree created with root: %d\n", root-
>data);
return 0;
}
Output:
Tree created with root: 10
🔹 17. Inorder Traversal (Left-Root-Right)
void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
Output (for tree in 17):
20 10 30
🔹 18. Preorder Traversal (Root-Left-Right)
void preorder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
Output:
10 20 30
🔹 19. Postorder Traversal (Left-Right-Root)
void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
Output:
20 30 10
20.Reverse an Array
#include <stdio.h>
int main() {
int arr[100], n, i;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nReversed array:\n");
for(i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter size of array: 5
Enter 5 elements:
12345
Original array:
12345
Reversed array:
54321