1. Implementation of two dimensional array.
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
return 0;
}
Output:
10 20 30 40 50
2. Implementation of Stack using array.
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Stack overflow\n");
} else {
stack[++top] = value;
printf("Pushed %d onto the stack\n", value);
}
}
void pop() {
if (top == -1) {
printf("Stack underflow\n");
} else {
printf("Popped %d from the stack\n", stack[top--]);
}
}
int isEmpty() {
return top == -1;
}
int isFull() {
return top == MAX - 1;
}
void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack elements are: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
push(40);
push(50);
push(60);
display();
printf("Top element is %d\n", peek());
pop();
display();
return 0;
}
Pushed 10 onto the stack
Output: Pushed 20 onto the stack
Pushed 30 onto the stack
Pushed 40 onto the stack
Pushed 50 onto the stack
Stack overflow
Stack elements are: 50 40 30 20 10
Top element is 50
Popped 50 from the stack
Stack elements are: 40 30 20 10
3. Implementation of Queue using array.
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1;
int rear = -1;
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue overflow\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("Enqueued %d\n", value);
}
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue underflow\n");
} else {
printf("Dequeued %d\n", queue[front++]);
if (front > rear) {
front = rear = -1;
}
}
}
int peek() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
return -1;
} else {
return queue[front];
}
}
int isEmpty() {
return front == -1;
}
int isFull() {
return rear == MAX - 1;
}
void display() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
enqueue(60);
display();
printf("Front element is %d\n", peek());
dequeue();
display();
return 0;
}
Output: Enqueued 10
Enqueued 20
Enqueued 30
Enqueued 40
Enqueued 50
Queue overflow
Queue elements are: 10 20 30 40 50
Front element is 10
Dequeued 10
Queue elements are: 20 30 40 50
4. Implementation of Circular Queue using array.
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1;
int rear = -1;
void enqueue(int value) {
if ((rear + 1) % MAX == front) {
printf("Queue overflow\n");
} else {
if (front == -1) front = 0;
rear = (rear + 1) % MAX;
queue[rear] = value;
printf("Enqueued %d\n", value);
}
}
void dequeue() {
if (front == -1) {
printf("Queue underflow\n");
} else {
printf("Dequeued %d\n", queue[front]);
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
}
int peek() {
if (front == -1) {
printf("Queue is empty\n");
return -1;
} else {
return queue[front];
}
}
int isEmpty() {
return front == -1;
}
int isFull() {
return (rear + 1) % MAX == front;
}
void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue elements are: ");
int i = front;
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % MAX;
}
printf("%d\n", queue[rear]);
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
enqueue(60);
display();
printf("Front element is %d\n", peek());
dequeue();
display();
return 0;
}
Output:
Enqueued 10
Enqueued 20
Enqueued 30
Enqueued 40
Enqueued 50
Queue overflow
Queue elements are: 10 20 30 40 50
Front element is 10
Dequeued 10
Queue elements are: 20 30 40 50
5. Implementation of Single linked list for insertion and Searching.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
int search(struct Node* head, int value) {
struct Node* current = head;
int position = 1;
while (current != NULL) {
if (current->data == value) {
return position;
}
current = current->next;
position++;
}
return -1; // Value not found
}
void display(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("The list is empty.\n");
return;
}
printf("Linked List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
display(head);
int value = 20;
int position = search(head, value);
if (position != -1) {
printf("Value %d found at position %d.\n", value, position);
} else {
printf("Value %d not found in the list.\n", value);
}
return 0;
}
Output:
Linked List: 10 20 30 40
Value 20 found at position 2.
6. Implementation of Circular linked list for insertion and Searching.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
struct Node* last = *head;
newNode->data = value;
newNode->next = *head; // Point to head to make it circular
if (*head == NULL) {
*head = newNode;
} else {
while (last->next != *head) {
last = last->next;
}
last->next = newNode;
}
}
int search(struct Node* head, int value) {
if (head == NULL) {
return -1; // Empty list
}
struct Node* current = head;
int position = 1;
do {
if (current->data == value) {
return position;
}
current = current->next;
position++;
} while (current != head); // Loop back to head when we reach
the end of the circular list
return -1; // Value not found
}
void display(struct Node* head) {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
struct Node* temp = head;
printf("Circular Linked List: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
display(head);
int value = 30;
int position = search(head, value);
if (position != -1) {
printf("Value %d found at position %d.\n", value, position);
} else {
printf("Value %d not found in the list.\n", value);
}
return 0;
}
Output:
Circular Linked List: 10 20 30 40
Value 30 found at position 3.
7. Implementation of Sequential search using array.
#include <stdio.h>
int sequentialSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int result = sequentialSearch(arr, size, key);
if (result != -1) {
printf("Element %d found at index %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}
Output:
Element 30 found at index 2.
8. Implementation of binary search using array.
#include <stdio.h>
int binarySearch(int arr[], int size, int key) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int result = binarySearch(arr, size, key);
if (result != -1) {
printf("Element %d found at index %d.\n", key, result);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}
Output:
Element 30 found at index 2.
9. Arrange the list of numbers in ascending order using Bubble Sort.
#include <stdio.h>
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, size);
bubbleSort(arr, size);
printf("Sorted array in ascending order: \n");
printArray(arr, size);
return 0;
}
Output:
Unsorted array:
64 34 25 12 22 11 90
Sorted array in ascending order:
11 12 22 25 34 64 90
10. Arrange the list of numbers in ascending order using Selection Sort.
#include <stdio.h>
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, size);
selectionSort(arr, size);
printf("Sorted array in ascending order: \n");
printArray(arr, size);
return 0;
}
Output:
Unsorted array:
64 25 12 22 11
Sorted array in ascending order:
11 12 22 25 64
11. Arrange the list of numbers in ascending order
using Insertion sort.
#include <stdio.h>
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, size);
insertionSort(arr, size);
printf("Sorted array in ascending order: \n");
printArray(arr, size);
return 0;
}
Output:
Unsorted array:
64 34 25 12 22 11
Sorted array in ascending order:
11 12 22 25 34 64
12. Implementation of Merge sort.
#include <stdio.h>
void merge(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
merge(arr, left, mid);
merge(arr, mid + 1, right);
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1], rightArr[n2];
for (int i = 0; i < n1; i++)
leftArr[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, size);
merge(arr, 0, size - 1);
printf("Sorted array in ascending order: \n");
printArray(arr, size);
return 0;
}
Output:
Unsorted array:
12 11 13 5 6 7
Sorted array in ascending order:
5 6 7 11 12 13
13. Implementation of Quick sort.
#include <stdio.h>
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, size);
quickSort(arr, 0, size - 1);
printf("Sorted array in ascending order: \n");
printArray(arr, size);
return 0;
}
Output:
Unsorted array:
12 11 13 5 6 7
Sorted array in ascending order:
5 6 7 11 12 13
14. Implementation of Binary Tree.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
void preorderTraversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
void postorderTraversal(struct Node* root) {
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}
int main() {
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n");
return 0;
}
1
/ \
2 3
/\ /\
4 56 7
Output:
Inorder Traversal: 4 2 5 1 6 3 7
Preorder Traversal: 1 2 4 5 3 6 7
Postorder Traversal: 4 5 2 6 7 3 1