Data Structures and Algorithms A2
Data Structures and Algorithms A2
Lab Assessment 2:
Mark: 10
Deadline: 21-08-2024
1. Write a program to evaluate the postfix and prefix expression using stack. Test cases: Input:
Enter the postfix expression: 3 4 * 2 5 * + Output: The value is: 22
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack structure
int top;
int items[MAX];
} Stack;
s->top = -1;
if (s->top == MAX - 1) {
printf("Stack overflow\n");
exit(1);
23BKT0031
GAUTAM KRISHNA KUMAR
s->items[++s->top] = value;
if (s->top == -1) {
printf("Stack underflow\n");
exit(1);
return s->items[s->top--];
Stack stack;
initStack(&stack);
} else { // Operator
switch (token) {
default:
exit(1);
return pop(&stack);
Stack stack;
initStack(&stack);
} else { // Operator
switch (token) {
default:
exit(1);
return pop(&stack);
int main() {
char postfixExpression[MAX];
char prefixExpression[MAX];
int choice;
while (1) {
printf("\nMenu:\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
23BKT0031
GAUTAM KRISHNA KUMAR
break;
case 2:
break;
case 3:
exit(0);
default:
return 0;
}
23BKT0031
GAUTAM KRISHNA KUMAR
2. Write a program to implement the following operations of queue using array 1. Enqueue 2.
Dequeue 3. Display
CODE:
#include <stdio.h>
#include <stdlib.h>
int items[MAX];
int front;
int rear;
} Queue;
q->front = -1;
q->rear = -1;
// Enqueue operation
if (isFull(q)) {
return;
if (isEmpty(q)) {
q->rear++;
q->items[q->rear] = value;
// Dequeue operation
if (isEmpty(q)) {
return;
q->front++;
}
23BKT0031
GAUTAM KRISHNA KUMAR
// Display operation
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
printf("\n");
int main() {
Queue q;
initQueue(&q);
while (1) {
printf("\nMenu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
return 0;
}
23BKT0031
GAUTAM KRISHNA KUMAR
3. Write a program to implement the following operations of circular queue using array 1. Enqueue
2. Dequeue 3. Display
CODE:
#include <stdio.h>
#include <stdlib.h>
int items[MAX];
int front;
int rear;
} CircularQueue;
q->front = -1;
q->rear = -1;
// Enqueue operation
if (isFull(q)) {
return;
if (isEmpty(q)) {
q->rear = 0;
q->rear = 0;
} else {
q->rear++;
q->items[q->rear] = value;
// Dequeue operation
if (isEmpty(q)) {
return;
q->front = -1;
q->rear = -1;
q->front = 0;
} else {
q->front++;
// Display operation
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
}
23BKT0031
GAUTAM KRISHNA KUMAR
} else {
printf("\n");
int main() {
CircularQueue q;
initQueue(&q);
while (1) {
printf("\nMenu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
23BKT0031
GAUTAM KRISHNA KUMAR
case 1:
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
return 0;
}
23BKT0031
GAUTAM KRISHNA KUMAR
4. Write a C program to sort the elements using Bubble sort ➢ The first line contains an integer n,
the number of elements to be sorted. ➢ Each of the next n lines contain one integer, the
elements of an array. Input: n=4 3 7 5 8 Output: 3 5 7 8
CODE:
#include <stdio.h>
arr[j + 1] = temp;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printf("Sorted elements:\n");
printf("\n");
return 0;
5. Write a C program to sort the elements using Selection sort ➢ The first line contains an integer
n, the number of elements to be sorted. ➢ Each of the next n lines contain one integer, the
elements of an array. Input: n=5 3 7 5 8 4 Output: 3 4 5 7 8
#include <stdio.h>
int minIndex = i;
minIndex = j;
// Swap the found minimum element with the first element of the unsorted part
arr[i] = arr[minIndex];
arr[minIndex] = temp;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
selectionSort(arr, n);
printf("Sorted elements:\n");
printf("\n");
return 0;
6. Write a C program to sort the elements using Insertion sort ➢ The first line contains an integer
n, the number of elements to be sorted. ➢ Each of the next n lines contain one integer, the
elements of an array. Input: n=5 3 7 5 8 4 Output: 3 4 5 7 8
CODE:
#include <stdio.h>
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
insertionSort(arr, n);
printf("Sorted elements:\n");
}
23BKT0031
GAUTAM KRISHNA KUMAR
printf("\n");
return 0;
CODE:
#include <stdio.h>
if (arr[i] == target) {
}
23BKT0031
GAUTAM KRISHNA KUMAR
if (arr[mid] == target) {
} else {
arr[j + 1] = temp;
}
23BKT0031
GAUTAM KRISHNA KUMAR
int main() {
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
while (1) {
printf("\nMenu:\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
scanf("%d", &target);
23BKT0031
GAUTAM KRISHNA KUMAR
if (result != -1) {
} else {
break;
bubbleSort(arr, n);
scanf("%d", &target);
if (result != -1) {
} else {
break;
case 3: // Exit
return 0;
default:
return 0;
}
23BKT0031
GAUTAM KRISHNA KUMAR