1. Develop a program to display an array in forward and backward directions.
1. Program to Display Array in Forward and Backward
Directions
#include <stdio.h>
int main() {
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Forward Direction:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nBackward Direction:\n");
for(i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
2. Develop a program to insert an element in an array.
i. Insert at the beginning
ii. Insert at the end
iii. Insert at any position
2. Program to Insert an Element in an Array
#include <stdio.h>
int main() {
int arr[100], n, i, element, pos;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element to insert: ");
scanf("%d", &element);
printf("Enter position (1 to %d): ", n + 1);
scanf("%d", &pos);
for(i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = element;
n++;
printf("Array after insertion:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
3. Develop a program to delete an element in an array.
iv. Delete at the beginning
v. Delete at the end
vi. Delete at any position
3. Program to Delete an Element in an Array
#include <stdio.h>
int main() {
int arr[100], n, i, pos;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter position to delete (1 to %d): ", n);
scanf("%d", &pos);
for(i = pos - 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
printf("Array after deletion:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
4. Program to implement the searching operation on an array using Linear
Search.
4. Linear Search Program
#include <stdio.h>
int main() {
int arr[100], n, i, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
for(i = 0; i < n; i++) {
if(arr[i] == key) {
printf("Element found at position %d", i + 1);
found = 1;
break;
}
}
if(found == 0)
printf("Element not found");
return 0;
}
5. Program to implement the searching operation on an array using Binary
Search.
5. Binary Search Program
#include <stdio.h>
int main() {
int arr[100], n, i, key;
int low = 0, high, mid, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter sorted array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
high = n - 1;
while(low <= high) {
mid = (low + high) / 2;
if(arr[mid] == key) {
found = 1;
break;
}
else if(arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
if(found)
printf("Element found at position %d", mid + 1);
else
printf("Element not found");
return 0;
}
6. Program to implement initialization of arrays and traversal operation with
malloc().
6. Array Initialization and Traversal using malloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr, n, i;
printf("Enter size of array: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Array elements are:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
7. Program to implement initialization of arrays and traversal operation with
calloc().
7. Array Initialization and Traversal using calloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr, n, i;
printf("Enter size of array: ");
scanf("%d", &n);
arr = (int *)calloc(n, sizeof(int));
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Array elements are:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
8. Program to implement initialization of the array and resize the array using
DMA.
8. Resize Array using DMA
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr, n, newSize, i;
printf("Enter initial size: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
printf("Enter elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter new size: ");
scanf("%d", &newSize);
arr = (int *)realloc(arr, newSize * sizeof(int));
printf("Array after resizing:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
9. Program to implement matrix addition with DMA.
9. Matrix Addition using DMA
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
int **a = (int **)malloc(r * sizeof(int *));
int **b = (int **)malloc(r * sizeof(int *));
int **sum = (int **)malloc(r * sizeof(int *));
for(i = 0; i < r; i++) {
a[i] = (int *)malloc(c * sizeof(int));
b[i] = (int *)malloc(c * sizeof(int));
sum[i] = (int *)malloc(c * sizeof(int));
}
printf("Enter first matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &b[i][j]);
printf("Sum matrix:\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
10. Program to implement matrix subtraction with DMA.
10. Matrix Subtraction using DMA
#include <stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10];
int i, j, r, col;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &col);
printf("Enter first matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < col; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < col; j++)
scanf("%d", &b[i][j]);
printf("Subtraction matrix:\n");
for(i = 0; i < r; i++) {
for(j = 0; j < col; j++) {
c[i][j] = a[i][j] - b[i][j];
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
11. Program to implement matrix multiplication with DMA.
11. Matrix Multiplication Program
#include <stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10];
int i, j, k, r1, c1, r2, c2;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
printf("Enter first matrix:\n");
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
c[i][j] = 0;
for(k = 0; k < c1; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Result matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
12. Program to implement stack operations.
12. Stack Operations using Array
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int value) {
if(top == MAX - 1)
printf("Stack Overflow\n");
else {
top++;
stack[top] = value;
}
}
void pop() {
if(top == -1)
printf("Stack Underflow\n");
else
printf("Deleted element: %d\n", stack[top--]);
}
void display() {
int i;
for(i = top; i >= 0; i--)
printf("%d\n", stack[i]);
}
int main() {
push(10);
push(20);
push(30);
display();
pop();
display();
return 0;
}
13. Program to convert an infix expression into postfix.
13. Infix to Postfix Conversion
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char stack[100];
int top = -1;
void push(char x) {
stack[++top] = x;
}
char pop() {
return stack[top--];
}
int priority(char x) {
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main() {
char exp[100], result[100];
int i, j = 0;
printf("Enter infix expression: ");
scanf("%s", exp);
for(i = 0; i < strlen(exp); i++) {
char ch = exp[i];
if(isalnum(ch))
result[j++] = ch;
else if(ch == '(')
push(ch);
else if(ch == ')') {
while(stack[top] != '(')
result[j++] = pop();
pop();
}
else {
while(priority(stack[top]) >= priority(ch))
result[j++] = pop();
push(ch);
}
}
while(top != -1)
result[j++] = pop();
result[j] = '\0';
printf("Postfix Expression: %s", result);
return 0;
}
14. Program to evaluate a given postfix expression.
14. Postfix Expression Evaluation
#include <stdio.h>
#include <ctype.h>
int stack[100], top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() {
return stack[top--];
}
int main() {
char postfix[100];
int i, a, b;
printf("Enter postfix expression: ");
scanf("%s", postfix);
for(i = 0; postfix[i] != '\0'; i++) {
if(isdigit(postfix[i]))
push(postfix[i] - '0');
else {
b = pop();
a = pop();
switch(postfix[i]) {
case '+': push(a + b); break;
case '-': push(a - b); break;
case '*': push(a * b); break;
case '/': push(a / b); break;
}
}
}
printf("Result = %d", pop());
return 0;
}
15. Program to define a recursive function to solve the Tower of Hanoi puzzle.
15. Tower of Hanoi using Recursion
#include <stdio.h>
void tower(int n, char source, char temp, char dest) {
if(n == 1) {
printf("Move disk 1 from %c to %c\n", source, dest);
return;
}
tower(n - 1, source, dest, temp);
printf("Move disk %d from %c to %c\n", n, source, dest);
tower(n - 1, temp, source, dest);
}
int main() {
int n;
printf("Enter number of disks: ");
scanf("%d", &n);
tower(n, 'A', 'B', 'C');
return 0;
}
16. Program to display the Fibonacci series with the help of a recursive function.
16. Fibonacci Series using Recursion
#include <stdio.h>
int fibonacci(int n) {
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
17. Program to implement MultiStack.
17. MultiStack Program
#include <stdio.h>
#define SIZE 5
int stack1[SIZE], stack2[SIZE];
int top1 = -1, top2 = -1;
void push1(int val) {
if(top1 < SIZE - 1)
stack1[++top1] = val;
}
void push2(int val) {
if(top2 < SIZE - 1)
stack2[++top2] = val;
}
void display() {
int i;
printf("Stack1:\n");
for(i = top1; i >= 0; i--)
printf("%d ", stack1[i]);
printf("\nStack2:\n");
for(i = top2; i >= 0; i--)
printf("%d ", stack2[i]);
}
int main() {
push1(10);
push1(20);
push2(30);
push2(40);
display();
return 0;
}
18. Program to implement queue operations using arrays.
18. Queue Operations using Arrays
#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
void enqueue(int value) {
if(rear == MAX - 1)
printf("Queue Overflow\n");
else {
if(front == -1)
front = 0;
queue[++rear] = value;
}
}
void dequeue() {
if(front == -1 || front > rear)
printf("Queue Underflow\n");
else
printf("Deleted element: %d\n", queue[front++]);
}
void display() {
int i;
for(i = front; i <= rear; i++)
printf("%d ", queue[i]);
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}
19. Program to implement circular queue operations using arrays.
19. Circular Queue using Arrays
#include <stdio.h>
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
void enqueue(int value) {
if((rear + 1) % SIZE == front)
printf("Queue is Full\n");
else {
if(front == -1)
front = 0;
rear = (rear + 1) % SIZE;
queue[rear] = value;
}
}
void dequeue() {
if(front == -1)
printf("Queue is Empty\n");
else {
printf("Deleted: %d\n", queue[front]);
if(front == rear)
front = rear = -1;
else
front = (front + 1) % SIZE;
}
}
int main() {
enqueue(10);
enqueue(20);
dequeue();
return 0;
}
20. Program to implement double-ended queue operations using arrays.
20. Double Ended Queue using Arrays
#include <stdio.h>
#define SIZE 5
int deque[SIZE];
int front = -1, rear = -1;
void insertRear(int value) {
if(rear == SIZE - 1)
printf("Overflow\n");
else {
if(front == -1)
front = 0;
deque[++rear] = value;
}
}
void deleteFront() {
if(front == -1)
printf("Underflow\n");
else
printf("Deleted: %d\n", deque[front++]);
}
int main() {
insertRear(10);
insertRear(20);
deleteFront();
return 0;
}
21. Program to create a single-linked list and implement its operations
Linked list Operations:
i) traversing
ii) inserting an element at the beginning.
iii) inserting an element at the end.
iv) inserting an element at any position.
v) inserting an element after a given node.
vi) inserting an element before a given node.
vii) deleting an element at the beginning.
viii) deleting an element at the end.
ix) deleting an element at any position.
x) deletinga particular element.
xi) Searching.
xii) Counting
21. Single Linked List Operations
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
void insertEnd(int value) {
struct node *newNode, *temp;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else {
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
void display() {
struct node *temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
insertEnd(10);
insertEnd(20);
insertEnd(30);
display();
return 0;
}
22. Program to create a Circular Single-linked list and implement its operations
Linked list Operations:
i. Traversing
ii. inserting an element at the beginning.
iii. inserting an element at the end.
iv. inserting an element at any position.
v. inserting an element after a given node.
vi. inserting an element before a given node.
vii. deleting an element at the beginning.
viii. deleting an element at the end.
ix. deleting an element at any position.
x. deleting a particular element.
xi. Searching.
xii. Counting
22. Circular Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *tail = NULL;
void insert(int value) {
struct node *newNode;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
if(tail == NULL) {
tail = newNode;
tail->next = tail;
}
else {
newNode->next = tail->next;
tail->next = newNode;
tail = newNode;
}
}
void display() {
struct node *temp;
if(tail == NULL)
return;
temp = tail->next;
do {
printf("%d ", temp->data);
temp = temp->next;
} while(temp != tail->next);
}
int main() {
insert(10);
insert(20);
insert(30);
display();
return 0;
}
23. Program to create a Double-linked list and implement its operations
Linked list Operations:
i. Traversing
ii. inserting an element at the beginning.
iii. inserting an element at the end.
iv. inserting an element at any position.
v. inserting an element after a given node.
vi. inserting an element before a given node.
vii. deleting an element at the beginning.
viii. deleting an element at the end.
ix. deleting an element at any position.
x. deleting a particular element.
xi. Searching.
xii. Counting
23. Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev, *next;
};
struct node *head = NULL;
void insert(int value) {
struct node *newNode, *temp;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;
if(head == NULL) {
newNode->prev = NULL;
head = newNode;
}
else {
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
}
void display() {
struct node *temp = head;
while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
int main() {
insert(10);
insert(20);
insert(30);
display();
return 0;
}
24. Program to create a Circular Double-linked list and implement its operations
Linked list Operations:
i. Traversing
ii. inserting an element at the beginning.
iii. inserting an element at the end.
iv. inserting an element at any position.
v. inserting an element after a given node.
vi. inserting an element before a given node.
vii. deleting an element at the beginning.
viii. deleting an element at the end.
ix. deleting an element at any position.
x. deleting a particular element.
xi. Searching.
xii. Counting
24. Circular Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next, *prev;
};
struct node *head = NULL;
void insert(int value) {
struct node *newNode, *temp;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
if(head == NULL) {
head = newNode;
head->next = head;
head->prev = head;
}
else {
temp = head->prev;
temp->next = newNode;
newNode->prev = temp;
newNode->next = head;
head->prev = newNode;
}
}
void display() {
struct node *temp = head;
do {
printf("%d ", temp->data);
temp = temp->next;
} while(temp != head);
}
int main() {
insert(10);
insert(20);
insert(30);
display();
return 0;
}
25. Program to implement stack operations using linked list.
25. Stack using Linked List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *top = NULL;
void push(int value) {
struct node *newNode;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = top;
top = newNode;
}
void pop() {
struct node *temp;
if(top == NULL)
printf("Stack Underflow\n");
else {
temp = top;
printf("Deleted: %d\n", top->data);
top = top->next;
free(temp);
}
}
int main() {
push(10);
push(20);
pop();
return 0;
}
26. Program to implement queue operations using linked list.
26. 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 value) {
struct node *newNode;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;
if(rear == NULL) {
front = rear = newNode;
}
else {
rear->next = newNode;
rear = newNode;
}
}
void dequeue() {
struct node *temp;
if(front == NULL)
printf("Queue Underflow\n");
else {
temp = front;
printf("Deleted: %d\n", front->data);
front = front->next;
free(temp);
}
}
int main() {
enqueue(10);
enqueue(20);
dequeue();
return 0;
}
27. Program to implement selection sort.
27. Selection Sort Program
#include <stdio.h>
int main() {
int arr[100], n, i, j, min, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
for(i = 0; i < n - 1; i++) {
min = i;
for(j = i + 1; j < n; j++) {
if(arr[j] < arr[min])
min = j;
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
printf("Sorted array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
28. Program to implement insertion sort.
28. Insertion Sort Program
#include <stdio.h>
int main() {
int arr[100], n, i, j, key;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
for(i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
printf("Sorted array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
29. Program to implement shell sort.
29. Shell Sort Program
#include <stdio.h>
int main() {
int arr[100], n, i, j, gap, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
for(gap = n / 2; gap > 0; gap /= 2) {
for(i = gap; i < n; i++) {
temp = arr[i];
for(j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
printf("Sorted array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
30. Program to implement radix sort
30. Radix Sort Program
#include <stdio.h>
int getMax(int arr[], int n) {
int max = arr[0], i;
for(i = 1; i < n; i++) {
if(arr[i] > max)
max = arr[i];
}
return max;
}
void countSort(int arr[], int n, int exp) {
int output[100], count[10] = {0};
int i;
for(i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for(i = 1; i < 10; i++)
count[i] += count[i - 1];
for(i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for(i = 0; i < n; i++)
arr[i] = output[i];
}
int main() {
int arr[100], n, i, exp, max;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
max = getMax(arr, n);
for(exp = 1; max / exp > 0; exp *= 10)
countSort(arr, n, exp);
printf("Sorted array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}