0% found this document useful (0 votes)
8 views25 pages

Data Structures and Algorithms A2

Data structures codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views25 pages

Data Structures and Algorithms A2

Data structures codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

23BKT0031

GAUTAM KRISHNA KUMAR

Data Structures and Algorithms

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>

#define MAX 100

// Stack structure

typedef struct Stack {

int top;

int items[MAX];

} Stack;

// Initialize the stack

void initStack(Stack *s) {

s->top = -1;

// Push an element onto the stack

void push(Stack *s, int value) {

if (s->top == MAX - 1) {

printf("Stack overflow\n");

exit(1);
23BKT0031
GAUTAM KRISHNA KUMAR

s->items[++s->top] = value;

// Pop an element from the stack

int pop(Stack *s) {

if (s->top == -1) {

printf("Stack underflow\n");

exit(1);

return s->items[s->top--];

// Evaluate a postfix expression

int evaluatePostfix(char *expression) {

Stack stack;

initStack(&stack);

for (int i = 0; expression[i] != '\0'; i++) {

char token = expression[i];

if (isdigit(token)) { // If the token is a digit

push(&stack, token - '0'); // Convert char to int and push

} else if (token == ' ') {

continue; // Skip spaces

} else { // Operator

int operand2 = pop(&stack);

int operand1 = pop(&stack);

switch (token) {

case '+': push(&stack, operand1 + operand2); break;


23BKT0031
GAUTAM KRISHNA KUMAR

case '-': push(&stack, operand1 - operand2); break;

case '*': push(&stack, operand1 * operand2); break;

case '/': push(&stack, operand1 / operand2); break;

default:

printf("Invalid operator: %c\n", token);

exit(1);

return pop(&stack);

// Evaluate a prefix expression

int evaluatePrefix(char *expression) {

Stack stack;

initStack(&stack);

int length = strlen(expression);

// Loop through the expression from right to left

for (int i = length - 1; i >= 0; i--) {

char token = expression[i];

if (isdigit(token)) { // If the token is a digit

push(&stack, token - '0'); // Convert char to int and push

} else if (token == ' ') {

continue; // Skip spaces

} else { // Operator

int operand1 = pop(&stack);

int operand2 = pop(&stack);


23BKT0031
GAUTAM KRISHNA KUMAR

switch (token) {

case '+': push(&stack, operand1 + operand2); break;

case '-': push(&stack, operand1 - operand2); break;

case '*': push(&stack, operand1 * operand2); break;

case '/': push(&stack, operand1 / operand2); break;

default:

printf("Invalid operator: %c\n", token);

exit(1);

return pop(&stack);

int main() {

char postfixExpression[MAX];

char prefixExpression[MAX];

int choice;

while (1) {

printf("\nMenu:\n");

printf("1. Evaluate Postfix Expression\n");

printf("2. Evaluate Prefix Expression\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

getchar(); // Clear the buffer

switch (choice) {

case 1:
23BKT0031
GAUTAM KRISHNA KUMAR

printf("Enter the postfix expression: ");

fgets(postfixExpression, MAX, stdin);

postfixExpression[strcspn(postfixExpression, "\n")] = '\0'; // Remove newline character

printf("The value is: %d\n", evaluatePostfix(postfixExpression));

break;

case 2:

printf("Enter the prefix expression: ");

fgets(prefixExpression, MAX, stdin);

prefixExpression[strcspn(prefixExpression, "\n")] = '\0'; // Remove newline character

printf("The value is: %d\n", evaluatePrefix(prefixExpression));

break;

case 3:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

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>

#define MAX 100

typedef struct Queue {

int items[MAX];

int front;

int rear;

} Queue;

// Initialize the queue

void initQueue(Queue *q) {

q->front = -1;

q->rear = -1;

// Check if the queue is full

int isFull(Queue *q) {


23BKT0031
GAUTAM KRISHNA KUMAR

return q->rear == MAX - 1;

// Check if the queue is empty

int isEmpty(Queue *q) {

return q->front == -1 || q->front > q->rear;

// Enqueue operation

void enqueue(Queue *q, int value) {

if (isFull(q)) {

printf("Queue overflow! Cannot enqueue.\n");

return;

if (isEmpty(q)) {

q->front = 0; // Update front when the queue is empty

q->rear++;

q->items[q->rear] = value;

printf("%d enqueued to the queue.\n", value);

// Dequeue operation

void dequeue(Queue *q) {

if (isEmpty(q)) {

printf("Queue underflow! Cannot dequeue.\n");

return;

printf("%d dequeued from the queue.\n", q->items[q->front]);

q->front++;

}
23BKT0031
GAUTAM KRISHNA KUMAR

// Display operation

void display(Queue *q) {

if (isEmpty(q)) {

printf("Queue is empty.\n");

return;

printf("Queue elements are: ");

for (int i = q->front; i <= q->rear; i++) {

printf("%d ", q->items[i]);

printf("\n");

int main() {

Queue q;

initQueue(&q);

int choice, value;

while (1) {

printf("\nMenu:\n");

printf("1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the value to enqueue: ");


23BKT0031
GAUTAM KRISHNA KUMAR

scanf("%d", &value);

enqueue(&q, value);

break;

case 2:

dequeue(&q);

break;

case 3:

display(&q);

break;

case 4:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

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>

#define MAX 5 // Define the maximum size of the queue

typedef struct CircularQueue {


23BKT0031
GAUTAM KRISHNA KUMAR

int items[MAX];

int front;

int rear;

} CircularQueue;

// Initialize the circular queue

void initQueue(CircularQueue *q) {

q->front = -1;

q->rear = -1;

// Check if the queue is full

int isFull(CircularQueue *q) {

return (q->front == 0 && q->rear == MAX - 1) || (q->rear == (q->front - 1) % (MAX - 1));

// Check if the queue is empty

int isEmpty(CircularQueue *q) {

return q->front == -1;

// Enqueue operation

void enqueue(CircularQueue *q, int value) {

if (isFull(q)) {

printf("Queue overflow! Cannot enqueue.\n");

return;

if (isEmpty(q)) {

q->front = 0; // Initialize front when the queue is empty

q->rear = 0;

} else if (q->rear == MAX - 1 && q->front != 0) { // Wrap around


23BKT0031
GAUTAM KRISHNA KUMAR

q->rear = 0;

} else {

q->rear++;

q->items[q->rear] = value;

printf("%d enqueued to the queue.\n", value);

// Dequeue operation

void dequeue(CircularQueue *q) {

if (isEmpty(q)) {

printf("Queue underflow! Cannot dequeue.\n");

return;

printf("%d dequeued from the queue.\n", q->items[q->front]);

if (q->front == q->rear) { // Single element left

q->front = -1;

q->rear = -1;

} else if (q->front == MAX - 1) { // Wrap around

q->front = 0;

} else {

q->front++;

// Display operation

void display(CircularQueue *q) {

if (isEmpty(q)) {

printf("Queue is empty.\n");

return;

}
23BKT0031
GAUTAM KRISHNA KUMAR

printf("Queue elements are: ");

if (q->rear >= q->front) {

for (int i = q->front; i <= q->rear; i++) {

printf("%d ", q->items[i]);

} else {

for (int i = q->front; i < MAX; i++) {

printf("%d ", q->items[i]);

for (int i = 0; i <= q->rear; i++) {

printf("%d ", q->items[i]);

printf("\n");

int main() {

CircularQueue q;

initQueue(&q);

int choice, value;

while (1) {

printf("\nMenu:\n");

printf("1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {
23BKT0031
GAUTAM KRISHNA KUMAR

case 1:

printf("Enter the value to enqueue: ");

scanf("%d", &value);

enqueue(&q, value);

break;

case 2:

dequeue(&q);

break;

case 3:

display(&q);

break;

case 4:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

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>

void bubbleSort(int arr[], int n) {


23BKT0031
GAUTAM KRISHNA KUMAR

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - 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;

int main() {

int n;

// Read the number of elements

printf("Enter the number of elements (n): ");

scanf("%d", &n);

int arr[n];

// Read the elements of the array

printf("Enter %d elements:\n", n);

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Sort the array using Bubble Sort

bubbleSort(arr, n);

// Print the sorted array


23BKT0031
GAUTAM KRISHNA KUMAR

printf("Sorted elements:\n");

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

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>

void selectionSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

// Find the minimum element in the unsorted part of the array

int minIndex = i;

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[minIndex]) {


23BKT0031
GAUTAM KRISHNA KUMAR

minIndex = j;

// Swap the found minimum element with the first element of the unsorted part

int temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

int main() {

int n;

// Read the number of elements

printf("Enter the number of elements (n): ");

scanf("%d", &n);

int arr[n];

// Read the elements of the array

printf("Enter %d elements:\n", n);

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Sort the array using Selection Sort

selectionSort(arr, n);

// Print the sorted array

printf("Sorted elements:\n");

for (int i = 0; i < n; i++) {


23BKT0031
GAUTAM KRISHNA KUMAR

printf("%d ", arr[i]);

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>

void insertionSort(int arr[], int n) {

for (int i = 1; i < n; i++) {

int key = arr[i];

int j = i - 1;

// Move elements of arr[0..i-1] that are greater than key

// to one position ahead of their current position


23BKT0031
GAUTAM KRISHNA KUMAR

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

int main() {

int n;

// Read the number of elements

printf("Enter the number of elements (n): ");

scanf("%d", &n);

int arr[n];

// Read the elements of the array

printf("Enter %d elements:\n", n);

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Sort the array using Insertion Sort

insertionSort(arr, n);

// Print the sorted array

printf("Sorted elements:\n");

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

}
23BKT0031
GAUTAM KRISHNA KUMAR

printf("\n");

return 0;

7. Write a C program to implement linear search and binary search.

CODE:

#include <stdio.h>

// Function for Linear Search

int linearSearch(int arr[], int n, int target) {

for (int i = 0; i < n; i++) {

if (arr[i] == target) {

return i; // Return the index of the target element

}
23BKT0031
GAUTAM KRISHNA KUMAR

return -1; // Element not found

// Function for Binary Search

int binarySearch(int arr[], int left, int right, int target) {

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid; // Return the index of the target element

if (arr[mid] < target) {

left = mid + 1; // Search in the right half

} else {

right = mid - 1; // Search in the left half

return -1; // Element not found

// Function to sort the array (required for Binary Search)

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
23BKT0031
GAUTAM KRISHNA KUMAR

int main() {

int n, choice, target, result;

// Read the number of elements

printf("Enter the number of elements (n): ");

scanf("%d", &n);

int arr[n];

// Read the elements of the array

printf("Enter %d elements:\n", n);

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

while (1) {

printf("\nMenu:\n");

printf("1. Linear Search\n");

printf("2. Binary Search\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: // Linear Search

printf("Enter the target element to search: ");

scanf("%d", &target);
23BKT0031
GAUTAM KRISHNA KUMAR

result = linearSearch(arr, n, target);

if (result != -1) {

printf("Element found at index %d (0-based indexing).\n", result);

} else {

printf("Element not found.\n");

break;

case 2: // Binary Search

// Sort the array first

bubbleSort(arr, n);

printf("Enter the target element to search: ");

scanf("%d", &target);

result = binarySearch(arr, 0, n - 1, target);

if (result != -1) {

printf("Element found at index %d (0-based indexing).\n", result);

} else {

printf("Element not found.\n");

break;

case 3: // Exit

return 0;

default:

printf("Invalid choice! Please try again.\n");

return 0;

}
23BKT0031
GAUTAM KRISHNA KUMAR

You might also like