0% found this document useful (0 votes)
14 views

Ssn

This document is a record notebook for the Data Structures Lab course (EBCS22L01) for the academic year 2024-2025, detailing the work done by student P. Sai Shanmuk. It includes a bonafide certificate, an index of experiments conducted, and several programming exercises related to data structures such as lists, stacks, and queues implemented using arrays and linked lists. Each experiment includes an aim, algorithm, program code, output, and results.

Uploaded by

saitejagolla66
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)
14 views

Ssn

This document is a record notebook for the Data Structures Lab course (EBCS22L01) for the academic year 2024-2025, detailing the work done by student P. Sai Shanmuk. It includes a bonafide certificate, an index of experiments conducted, and several programming exercises related to data structures such as lists, stacks, and queues implemented using arrays and linked lists. Each experiment includes an aim, algorithm, program code, output, and results.

Uploaded by

saitejagolla66
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/ 124

FORM NO-F/TL/021

REV.00DATE20.03.2020

RECORD NOTEBOOK

EBCS22LO1-DATA STRUCTURES LAB

2024-2025(ODD SEMESTER)

DEPARTMENT

OF

COMPUTER SCIENCE AND ENGINEERING

NAME : P.Sai Shanmuk

REG NO : 231211101517

COURESE : B. Tech-CSE(AI)

YEAR/SEM/SEC : II/III/BF
FORM NO-F/TL/021
REV.00DATE20.03.2020

BONAFIDE CERTIFICATE

REGISTER NO : 231211101517
NAME OF LAB : DATA STRUCTURES LAB (EBCS22L01)
DEPATMENT : COMPUTER SCIENCE AND ENGINEERING

Certified that this is the Bonafide record of work done by


P.Sai Shanmuk(231211101517) II year B.TECH(CSE-AI),SEC-‘BF’ in
DATA STRUCTURES LAB during the year 2024-2025.

Signature of lab-in-charge Signature of Head of Department

Submitted for the Practical Examination held on

Internal Examiner External Examiner


INDEX
Exp.No Date Experiment Name Pg .No Sign
1a 30/9/24 IMPLEMENTATION LIST ADT 01-07
USING ARRAY
1b 30/9/24 IMPLEMENT LIST ADT USING 08-15
LINKED LIST
2a 7/10/24 IMPLEMENTATION OF STACK 16-21
USING ARRAY
2b 7/10/24 IMPLEMENTATION OF STACK USING 22-28
LINKED LIST
3a 14/10/24 IMPLEMENTATION OF QUEUE 29-35
USING ARRAY
3b 14/10/24 IMPLEMENTATION OF QUEUE USING 36-43
LINKED LIST
4a 21/10/24 EVALUATION OF POSTFIX 44-51
EXPRESSION
4b 28/10/24 EVALUATION OF POSTFIX 52-57
EXPRESSION
5 4/11/24 BINARY TREE TRAVERSAL- IN ORDER, 58-64
PREORDER , POSTORDER
USING RECURSION
6 11/11/24 IMPLEMENT THE OPERATION OF 65-74
BINARY SEARCH TREE
7 25/11/24 IMPLEMENTATION OF BINARY HEAPS 75-82

8a 2/12/24 IMPLEMENTATION OF BREADTH 83-89


FIST SEARCH
8b 16/12/24 IMPLEMENTATION OF DEPTH 90-94
FIRST SEARCH
9a 16/12/24 IMPLEMENTATION OF 95-99
LINEAR SEARCH
9b 23/12/24 IMPLEMENTATION OF 100-104
BINARY SEARCH
10a 23/12/24 BUBBLE SORT 105-109

10b 30/12/24 SHELL SORT 110-114

10c 30/12/24 INSERTION SORT 115-119

10d 6/1/25 IMPLEMENTATION OF HEAP SORT 120-125

11 6/1/25 COLLUSION AND DOUBLE HASHING 126-131


EX.NO:1A DATE:30/9/24
IMPLEMENT LIST ADT USING ARRAY

AIM:

ALGORITHM:
PROGRAM:
#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

typedef struct {

int data[MAX_SIZE];

int size;

} List;

void initList(List *list) {

list->size = 0;

void addElement(List *list, int element) {

if (list->size < MAX_SIZE) {

list->data[list->size] = element;

list->size++;

} else {

printf("List is full. Cannot add element %d\n", element);

void removeElement(List *list, int element) {

int found = 0;

for (int i = 0; i < list->size; i++) {

if (list->data[i] == element) {
found = 1;

for (int j = i; j < list->size - 1; j++) {

list->data[j] = list->data[j + 1];

list->size--;

break;

if (!found) {

printf("Element %d not found in the list.\n", element);

void displayList(const List *list) {

if (list->size == 0) {

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

return;

printf("List elements: ");

for (int i = 0; i < list->size; i++) {

printf("%d ", list->data[i]);

printf("\n");

int getSize(const List *list) {


return list->size;

int main() {

List myList;

initList(&myList);

printf(" & \n");

int choice, element;

do {

printf("\nMenu:\n");

printf("1. Add Element\n");

printf("2. Remove Element\n");

printf("3. Display List\n");

printf("4. Get Size of List\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter element to add: ");

scanf("%d", &element);

addElement(&myList, element);

break;

case 2:

printf("Enter element to remove: ");


scanf("%d", &element);

removeElement(&myList, element);

break;

case 3:

displayList(&myList);

break;

case 4:

printf("Size of the list: %d\n", getSize(&myList));

break;

case 5:

printf("Exiting...\n");

break;

default:

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

} while (choice != 5);

return 0;

}
OUTPUT:

Figure:1a.2

Figure:1a.3

RESULT:
EX.NO:1B DATE:30/9/24
IMPLEMENT THE LIST ATD USING THE LINKED LIST

AIM:

ALGORITHM :
PROGRAM:

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

typedef struct List {

Node* head;

} List;

void initList(List* list) {

list->head = NULL;

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

if (!newNode) {

printf("Memory allocation failed\n");

exit(1);

newNode->data = data;

newNode->next = NULL;

return newNode;

void insertAtBeginning(List* list, int data) {


Node* newNode = createNode(data);

newNode->next = list->head;

list->head = newNode;

void insertAtEnd(List* list, int data) {

Node* newNode = createNode(data);

if (list->head == NULL) {

list->head = newNode;

return;

Node* temp = list->head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

void deleteNode(List* list, int value) {

Node* temp = list->head;

Node* prev = NULL;

if (temp != NULL && temp->data == value) {

list->head = temp->next;

free(temp);

printf("Node with value %d deleted\n", value);

return;
}

while (temp != NULL && temp->data != value) {

prev = temp;

temp = temp->next;

if (temp == NULL) {

printf("Node with value %d not found\n", value);

return;

prev->next = temp->next;

free(temp);

printf("Node with value %d deleted\n", value);

int search(List* list, int value) {

Node* temp = list->head;

while (temp != NULL) {

if (temp->data == value) {

return 1;

temp = temp->next;

return 0;

}
void displayList(List* list) {

Node* temp = list->head;

if (temp == NULL) {

printf("List is empty\n");

return;

printf("List: ");

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

printf(“ & ./n”);

List list;

initList(&list);

insertAtBeginning(&list, 10);

insertAtBeginning(&list, 20);

insertAtEnd(&list, 30);

insertAtEnd(&list, 40);

printf("After insertions:\n");

displayList(&list);
deleteNode(&list, 20);

printf("After deletion:\n");

displayList(&list);

int value = 30;

if (search(&list, value)) {

printf("Value %d found in the list\n", value);

} else {

printf("Value %d not found in the list\n", value);

return 0;

}
OUTPUT:

Figure:1b.2

RESULT:
EX.NO.2A: DATE:7/10/24

IMPLEMENTATION OF STACK USING ARRAY

AIM:

ALGORITHM:
PROGRAM:

#include <stdio.h>

#define MAX 100

// Structure to represent a stack

typedef struct {

int arr[MAX];

int top;

} Stack;

/ Function to initialize the stack

void initialize(Stack *stack) {

stack->top = -1;

// Function to check if the stack is emptisEmpty(Stack *stack) {

return stack->top == -1;

// Function to check if the stack is full

int isFull(Stack *stack) {

return stack->top == MAX - 1;

// Function to push an element onto the stack

void push(Stack *stack, int value) {

if (isFull(stack)) {

printf("Stack overflow! Cannot push %d\n", value);

return;
}

stack->arr[++stack->top] = value;

printf("Pushed %d onto the stack.\n", value);

// Function to pop an element from the stack

int pop(Stack *stack) {

if (isEmpty(stack)) {

printf("Stack underflow! Cannot pop an element.\n");

return -1; // Return -1 to indicate an error

return stack->arr[stack->top--];

// Function to peek at the top element of the stack

int peek(Stack *stack) {

if (isEmpty(stack)) {

printf("Stack is empty!\n");

return -1; // Return -1 to indicate an error

return stack->arr[stack->top];

// Function to display the elements of the stack

void display(Stack *stack) {

if (isEmpty(stack)) {

printf("Stack is empty!\n");
return;

printf("Stack elements: ");

for (int i = 0; i <= stack->top; i++) {

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

printf("\n");

int main() {

Stack stack;

initialize(&stack);

printf(" & \n ");

push(&stack, 10);

push(&stack, 20);

push(&stack, 30);

printf("Top element is: %d\n", peek(&stack));

printf("Popped element: %d\n", pop(&stack));

display(&stack);

return 0;

}
OUTPUT:

Figure:2a.b

RESULT:
EX.NO:2B DATE:7/10/24

MPLEMENTATION OF QUEUE USING ARRAY

AIM:

ALGORITHM:
PROGRAM:

#include <stdio.h>

#include <stdio.h>

#define MAX 100

// Structure to represent a queue

typedef struct {

int arr[MAX];

int front;

int rear;

} Queue;

// Function to initialize the queue

void initialize(Queue *queue) {

queue->front = -1;

queue->rear = -1;

// Function to check if the queue is empty

int isEmpty(Queue *queue) {

return queue->front == -1;

// Function to check if the queue is full

int isFull(Queue *queue) {

return queue->rear == MAX - 1;

}
// Function to enqueue an element into the queue

void enqueue(Queue *queue, int value) {

if (isFull(queue)) {

printf("Queue overflow! Cannot enqueue %d\n", value);

return;

if (isEmpty(queue)) {

queue->front = 0;

queue->arr[++queue->rear] = value;

printf("Enqueued %d into the queue.\n", value);

// Function to dequeue an element from the queue

int dequeue(Queue *queue) {

if (isEmpty(queue)) {

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

return -1; // Return -1 to indicate an error

int value = queue->arr[queue->front];

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

// Queue becomes empty

queue->front = -1;

queue->rear = -1;

} else {
queue->front++;

return value;

// Function to peek at the front element of the queue

int peek(Queue *queue) {

if (isEmpty(queue)) {

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

return -1; // Return -1 to indicate an error

return queue->arr[queue->front];

// Function to display the elements of the queue

void display(Queue *queue) {

if (isEmpty(queue)) {

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

return;

printf("Queue elements: ");

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

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

printf("\n");
}

int main() {

Queue queue;

initialize(&queue);

printf(“ & \n”);

enqueue(&queue, 10);

enqueue(&queue, 20);

enqueue(&queue, 30);

printf("Front element is: %d\n", peek(&queue));

printf("Dequeued element: %d\n", dequeue(&queue));

display(&queue);

return 0;

}
OUTPUT:

Figure2b.2

RESULT:
EX.NO:3A DATE:14/10/24

IMPLEMENTATION OF STACK USING LINKED LIST

AIM:

ALGORITHM:
PROGRAM:

#include <stdio.h>

#include <stdlib.h>

// Step 1: Define the Node Structure

struct Node {

int data;

struct Node* next;

};

// Step 2: Define the Stack Structure

struct Stack {

struct Node* top;

};

// Function to create a new stack

struct Stack* createStack() {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->top = NULL;

return stack;

// Step 3: Check if the Stack is Empty

int isEmpty(struct Stack* stack) {

return stack->top == NULL;

// Step 4: Push Operation


void push(struct Stack* stack, int item) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = item;

newNode->next = stack->top;

stack->top = newNode;

printf("%d pushed to stack\n", item);

// Step 5: Pop Operation

int pop(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty\n");

return -1; // Return -1 to indicate stack is empty

struct Node* temp = stack->top;

int poppedItem = temp->data;

stack->top = stack->top->next;

free(temp);

return poppedItem;

// Step 6: Peek Operation

int peek(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty\n");

return -1; // Return -1 to indicate stack is empty


}

return stack->top->data;

// Step 7: Display the Stack

void display(struct Stack* stack) {

if (isEmpty(stack)) {

printf("Stack is empty\n");

return;

struct Node* current = stack->top;

printf("Stack elements: ");

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

printf("\n");

// Main function to demonstrate stack operations

int main() {

struct Stack* stack = createStack();

printf("& .\n");

push(stack, 10);

push(stack, 20);

push(stack, 30);
display(stack);

printf("%d popped from stack\n", pop(stack));

printf("Top element is %d\n", peek(stack));

display(stack);

return 0;

}
OUTPUT:

Figure:3a.2

RESULT:
EX NO.3B DATE:14/10/24

IMPLEMENTATION OF QUEUE USING LINKED LIST

AIM:

ALGORITHM:
PROGRAM:

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int data;

struct Node* next;

};

// Queue structure

struct Queue {

struct Node* front;

struct Node* rear;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (!newNode) {

printf("Memory allocation failed.\n");

exit(1);

newNode->data = data;

newNode->next = NULL;

return newNode;

231211101522 38 P.Praveen naidu


// Function to create an empty queue

struct Queue* createQueue() {

struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));

if (!queue) {

printf("Memory allocation failed.\n");

exit(1);

queue->front = queue->rear = NULL;

return queue;

// Enqueue operation

void enqueue(struct Queue* queue, int data) {

struct Node* newNode = createNode(data);

if (queue->rear == NULL) {

queue->front = queue->rear = newNode;

return;

queue->rear->next = newNode;

queue->rear = newNode;

printf("Enqueued: %d\n", data);

// Dequeue operation

int dequeue(struct Queue* queue) {

231211101522 39 P.Praveen naidu


if (queue->front == NULL) {

printf("Queue is empty. Dequeue operation failed.\n");

return -1;

int data = queue->front->data;

struct Node* temp = queue->front;

queue->front = queue->front->next;

if (queue->front == NULL)

queue->rear = NULL;

free(temp);

printf("Dequeued: %d\n", data);

return data;

// Display the queue

void displayQueue(struct Queue* queue) {

if (queue->front == NULL) {

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

return;

struct Node* temp = queue->front;

printf("Queue: ");

while (temp) {

printf("%d ", temp->data);

temp = temp->next;

231211101522 40 P.Praveen naidu


}

printf("\n");

// Main function

int main() {

printf(" & \n");

struct Queue* queue = createQueue();

int choice, value;

do {

printf("\nQueue Operations:\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 value to enqueue: ");

scanf("%d", &value);

enqueue(queue, value);

break;

case 2:

dequeue(queue);

231211101522 41 P.Praveen naidu


break;

case 3:

displayQueue(queue);

break;

case 4:

printf("Exiting program.\n");

break;

default:

printf("Invalid choice. Try again.\n");

} while (choice != 4);

// Free memory

while (queue->front != NULL) {

dequeue(queue);

free(queue);

return 0;

231211101522 42 P.Praveen naidu


OUTPUT :

figure:3b.2

RESULT :

231211101522 43 P.Praveen naidu


EXPNO: 4a DATE:21/10/24
INFIX TO POSTFIX CONVERSATION

AIM:

ALGORITHM:

231211101522 44 P.Praveen naidu


CODE:

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

#define MAX 100

// Stack structure

typedef struct {

char data[MAX];

int top;

} Stack;

// Stack functions

void initStack(Stack *s) {

s->top = -1;

int isEmpty(Stack *s) {

return s->top == -1;

int isFull(Stack *s) {

return s->top == MAX - 1;

void push(Stack *s, char ch) {

if (!isFull(s)) {

s->data[++(s->top)] = ch;
} else {

printf("Stack Overflow\n");

char pop(Stack *s) {

if (!isEmpty(s)) {

return s->data[(s->top)--];

} else {

printf("Stack Underflow\n");

return '\0';

char peek(Stack *s) {

if (!isEmpty(s)) {

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

} else {

return '\0';

// Utility functions

int precedence(char op) {

switch (op) {
case '+':

case '-': return 1;

case '*':

case '/': return 2;

case '^': return 3;

default: return 0;

int isOperator(char ch) {

return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';

// Function to convert infix to postfix

void infixToPostfix(char *infix, char *postfix) {

Stack stack;

initStack(&stack);

int i = 0, j = 0;

while (infix[i] != '\0') {

if (isdigit(infix[i]) || isalpha(infix[i])) { // Operand

postfix[j++] = infix[i];

} else if (infix[i] == '(') { // Left parenthesis

push(&stack, infix[i]);

} else if (infix[i] == ')') { // Right parenthesis

while (!isEmpty(&stack) && peek(&stack) != '(') {

postfix[j++] = pop(&stack);
}

pop(&stack); // Remove '(' from the stack

} else if (isOperator(infix[i])) { // Operator

while (!isEmpty(&stack) && precedence(peek(&stack)) >= precedence(infix[i])) {

postfix[j++] = pop(&stack);

push(&stack, infix[i]);

i++;

// Pop remaining operators

while (!isEmpty(&stack)) {

postfix[j++] = pop(&stack);

postfix[j] = '\0'; // Null-terminate the postfix expression

int main() {

char infix[MAX], postfix[MAX];

printf(" & \n");

printf("Enter an infix expression: ");

fgets(infix, MAX, stdin);

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

infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);

return 0;

}
OUTPUT:

Figure:4a.2

Figure:4a.3

RESULT:
EXPNO: 4b DATE:28/10/24

EVALUATION OF POSTFIX EXPRESSION

AIM:

ALGORITHM:
CODE:

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <math.h>

#define MAX 100

// Stack structure

typedef struct {

int data[MAX];

int top;

} Stack;

// Stack functions

void initStack(Stack *s) {

s->top = -1;

int isEmpty(Stack *s) {

return s->top == -1;

void push(Stack *s, int value) {

s->data[++(s->top)] = value;

int pop(Stack *s) {

if (!isEmpty(s)) {

return s->data[(s->top)--];
} else {

printf("Stack Underflow\n");

exit(EXIT_FAILURE);

// Function to evaluate postfix expression

int evaluatePostfix(char *postfix) {

Stack stack;

initStack(&stack);

int i = 0;

while (postfix[i] != '\0') {

if (isdigit(postfix[i])) { // Operand

push(&stack, postfix[i] - '0'); // Convert char to int

} else { // Operator

int val2 = pop(&stack);

int val1 = pop(&stack);

switch (postfix[i]) {

case '+': push(&stack, val1 + val2); break;

case '-': push(&stack, val1 - val2); break;

case '*': push(&stack, val1 * val2); break;

case '/': push(&stack, val1 / val2); break;

case '^': push(&stack, pow(val1, val2)); break;

}
i++;

return pop(&stack);

int main() {

printf(" & .\n");

char postfix[MAX];

printf("Enter a postfix expression: ");

scanf("%s", postfix);

int result = evaluatePostfix(postfix);

printf("Result of postfix evaluation: %d\n", result);

return 0; }
OUTPUT:

Figure4b.2

RESULT:
EXPNO: 05 DATE:04/11/24
BINARY TREE TRAVERSAL IN ORDER ,PREORDER,
POSTORDER USING RECURSION

AIM:

ALGORITHM:
// Pre-order traversal (Root, Left, Right)

void preOrder(struct Node* root) {

if (root != NULL) {

printf("%d ", root->data); // Visit root

preOrder(root->left); // Traverse left subtree

preOrder(root->right); // Traverse right subtree

// Post-order traversal (Left, Right, Root)

void postOrder(struct Node* root) {

if (root != NULL) {

postOrder(root->left); // Traverse left subtree

postOrder(root->right); // Traverse right subtree

printf("%d ", root->data); // Visit root

int main() {

printf(" & .\n");

int n, data;

struct Node* root = NULL;

struct Node* temp;

struct Node* node;


printf("Enter number of nodes: ");

scanf("%d", &n);

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

printf("Enter value for node %d: ", i + 1);

scanf("%d", &data);

// Creating a new node and adding it to the tree

node = newNode(data);

if (root == NULL) {

root = node; // Root node initialization

} else {

temp = root;

while (1) {

if (data < temp->data) {

if (temp->left == NULL) {

temp->left = node;

break;

} else {

temp = temp->left;

} else {

if (temp->right == NULL) {

temp->right = node;

break;
} else {

temp = temp->right;

printf("\nIn-order traversal: ");

inOrder(root);

printf("\nPre-order traversal: ");

preOrder(root);

printf("\nPost-order traversal: ");

postOrder(root);

return 0;

}
OUTPUT:

Figure:5b.2

RESULT:
EXPNO:06 DATE:11/11/24

IMPLEMENT THE OPERATION OF BINARY SEARCH TREE

AIM:

ALGORITHM:
CODE:

#include <stdio.h>

#include <stdlib.h>

// Definition of a tree node

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new node

struct Node* newNode(int data) {

struct Node* node = (struct Node*)malloc(sizeof(struct Node));

node->data = data;

node->left = node->right = NULL;

return node;

// Insert a node in the BST

struct Node* insert(struct Node* root, int data) {

if (root == NULL) {

return newNode(data);

if (data < root->data) {

root->left = insert(root->left, data);

} else if (data > root->data) {


root->right = insert(root->right, data);

return root;

// Search for a value in the BST

struct Node* search(struct Node* root, int key) {

if (root == NULL || root->data == key) {

return root;

if (key < root->data) {

return search(root->left, key);

return search(root->right, key);

// Find the minimum value node in the BST

struct Node* minValueNode(struct Node* node) {

struct Node* current = node;

while (current && current->left != NULL) {

current = current->left;

return current;

// Delete a node from the BST


struct Node* deleteNode(struct Node* root, int key) {

if (root == NULL) {

return root;

if (key < root->data) {

root->left = deleteNode(root->left, key);

} else if (key > root->data) {

root->right = deleteNode(root->right, key);

} else {

if (root->left == NULL) {

struct Node* temp = root->right;

free(root);

return temp;

} else if (root->right == NULL) {

struct Node* temp = root->left;

free(root);

return temp;

struct Node* temp = minValueNode(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

return root;

}
// In-order traversal of the BST

void inOrder(struct Node* root) {

if (root != NULL) {

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

// Pre-order traversal of the BST

void preOrder(struct Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

// Post-order traversal of the BST

void postOrder(struct Node* root) {

if (root != NULL) {

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

}
int main() {

printf(“ & .\n”);

struct Node* root = NULL;

int n, data, key, choice;

printf("Enter number of nodes: ");

scanf("%d", &n);

// Inserting nodes into the BST

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

printf("Enter value for node %d: ", i + 1);

scanf("%d", &data);

root = insert(root, data);

printf("\nChoose operation:\n");

printf("1. In-order Traversal\n");

printf("2. Pre-order Traversal\n");

printf("3. Post-order Traversal\n");

printf("4. Search a node\n");

printf("5. Delete a node\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("In-order Traversal: ");

inOrder(root);
break;

case 2:

printf("Pre-order Traversal: ");

preOrder(root);

break;

case 3:

printf("Post-order Traversal: ");

postOrder(root);

break;

case 4:

printf("Enter value to search: ");

scanf("%d", &key);

struct Node* found = search(root, key);

if (found != NULL) {

printf("Node %d found in the tree.\n", found->data);

} else {

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

break;

case 5:

printf("Enter value to delete: ");

scanf("%d", &key);

root = deleteNode(root, key);

printf("Node %d deleted.\n", key);


break;

default:

printf("Invalid choice.\n");

return 0;

}
OUTPUT:

Figure:6.2

RESULT:
EXPNO: 07 DATA:25/11/24
IMPLEMENTATION OF BINARY HEAPS

AIM:

ALGORITHM:
CODE:

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int heap[MAX];

int heapSize = 0;

// Function to swap two elements

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

// Heapify operation for a max-heap

void heapifyDown(int index) {

int largest = index;

int left = 2 * index + 1;

int right = 2 * index + 2;

if (left < heapSize && heap[left] > heap[largest]) {

largest = left;

if (right < heapSize && heap[right] > heap[largest]) {

largest = right;

if (largest != index) {
swap(&heap[index], &heap[largest]);

heapifyDown(largest);

// Heapify-up operation for a max-heap

void heapifyUp(int index) {

int parent = (index - 1) / 2;

if (index > 0 && heap[index] > heap[parent]) {

swap(&heap[index], &heap[parent]);

heapifyUp(parent);

// Insert a new element into the heap

void insert(int value) {

if (heapSize == MAX) {

printf("Heap overflow!\n");

return;

heap[heapSize] = value;

heapifyUp(heapSize);

heapSize++;

}
// Delete the root (maximum element) from the heap

void deleteRoot() {

if (heapSize == 0) {

printf("Heap underflow!\n");

return;

printf("Deleted root: %d\n", heap[0]);

heap[0] = heap[heapSize - 1];

heapSize--;

heapifyDown(0);

// Print the heap elements

void printHeap() {

if (heapSize == 0) {

printf("Heap is empty!\n");

return;

printf("Heap elements: ");

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

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

printf("\n");

}
int main() {

int choice, value;

while (1) {

printf(" & .\n");

printf("\nBinary Heap Operations:\n");

printf("1. Insert\n");

printf("2. Delete Root\n");

printf("3. Print Heap\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insert(value);

break;

case 2:

deleteRoot();

break;

case 3:

printHeap();

break;
case 4:

exit(0);

default:

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

return 0;

}
OUTPUT:

Figure:7.2

RESULT:
EXPNO:8a DATE:2/12/24
IMPLEMENTATION OF BREADTH FIRST SEARCH

AIM:

ALGORITHM:
CODE:

#include <stdio.h>

#include <stdlib.h>

#define MAX 100 // Maximum number of nodes in the graph

int graph[MAX][MAX]; // Adjacency matrix

int visited[MAX]; // Array to track visited nodes

int queue[MAX]; // Queue for BFS

int front = -1, rear = -1;

// Function to enqueue an element

void enqueue(int node) {

if (rear == MAX - 1) {

printf("Queue Overflow\n");

return;

if (front == -1) {

front = 0;

queue[++rear] = node;

// Function to dequeue an element

int dequeue() {

if (front == -1 || front > rear) {

printf("Queue Underflow\n");

return -1;
}

return queue[front++];

// Check if the queue is empty

int isQueueEmpty() {

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

// BFS function

void bfs(int startNode, int n) {

enqueue(startNode);

visited[startNode] = 1;

printf("BFS Traversal: ");

while (!isQueueEmpty()) {

int currentNode = dequeue();

printf("%d ", currentNode);

// Visit all adjacent unvisited nodes

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

if (graph[currentNode][i] == 1 && !visited[i]) {

enqueue(i);

visited[i] = 1;

printf("\n");
}

int main() {

int n, edges, u, v, startNode;

printf(" & .\n");

printf("Enter the number of nodes: ");

scanf("%d", &n);

printf("Enter the number of edges: ");

scanf("%d", &edges);

// Initialize graph and visited array

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

visited[i] = 0;

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

graph[i][j] = 0;

printf("Enter the edges (u v):\n");

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

scanf("%d %d", &u, &v);

graph[u][v] = 1;

graph[v][u] = 1; // For undirected graph

printf("Enter the starting node: ");

scanf("%d", &startNode);
bfs(startNode, n);

return 0;

}
OUTPUT:

Figure:8a.2

RESULT:
EXPENO:8b DATE:16/12/24

IMPLEMENTATION OF DEPTH FIRST SEARCH

AIM:

ALGORITHM:
CODE:

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

int graph[MAX][MAX]; // Adjacency matrix

int visited[MAX]; // Array to track visited nodes

// Recursive DFS function

void dfs(int node, int n) {

printf("%d ", node); // Process the current node

visited[node] = 1; // Mark the node as visited

// Visit all unvisited neighbors

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

if (graph[node][i] == 1 && !visited[i]) {

dfs(i, n); // Recursively call DFS for the neighbor

int main() {

int n, edges, u, v, startNode;

printf(" & .\n");

printf("Enter the number of nodes: ");

scanf("%d", &n);

printf("Enter the number of edges: ");

scanf("%d", &edges);
// Initialize graph and visited array

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

visited[i] = 0;

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

graph[i][j] = 0;

printf("Enter the edges (u v):\n");

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

scanf("%d %d", &u, &v);

graph[u][v] = 1;

graph[v][u] = 1; // For undirected graph

printf("Enter the starting node: ");

scanf("%d", &startNode);

printf("DFS Traversal: ");

dfs(startNode, n); // Start DFS from the given node

printf("\n");

return 0;

}
OUTPUT:

Figure:8b.2

RESULT:
EXPNO: 9a DATE:16/12/24

IMPLEMENTATION OF BINARY SEARCH

AIM:

ALGORITHM:
CODE:

#include <stdio.h>

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

int left = 0, right = n - 1;

while (left <= right) {

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

if (arr[mid] == target) {

return mid; // Target found

} else if (arr[mid] < target) {

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

} else {

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

return -1; // Target not found

int main() {

int n, target;

printf(" & .\n");

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

scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array (sorted):\n");

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


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

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

scanf("%d", &target);

int result = binarySearch(arr, n, target);

if (result != -1) {

printf("Element %d found at index %d.\n", target, result);

} else {

printf("Element %d not found in the array.\n", target);

return 0;

}
OUTPUT:

figure:9a.2

RESULT:
EXPNO:9B DATE:23/12/24

IMPLEMENTATION OF LINEAR SEARCH

AIM:

ALGORITHUM:
CODE:

include <stdio.h>

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

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

if (arr[i] == target) {

return i; // Target found, return index

return -1; // Target not found

int main() {

printf(“ & \n");

int n, target;

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

scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array:\n");

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

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

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

scanf("%d", &target);
int result = linearSearch(arr, n, target);

if (result != -1) {

printf("Element %d found at index %d.\n", target, result);

} else {

printf("Element %d not found in the array.\n", target);

return 0;

}
OUTPUT:

Figure:9b.2

RESULT:
EXPNO: 10a DATE:23/12/24
BUBBLE SORT

Aim:

Algorithm:
CODE:

#include <stdio.h>

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

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

int swapped = 0; // Optimization to track if any swaps occurred

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;

swapped = 1; // Mark that a swap occurred

if (!swapped) {

break; // Exit early if no swaps occurred

int main() {

int n;

printf("& .\n");

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


scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array:\n");

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

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

bubbleSort(arr, n);

printf("Sorted array: ");

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

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

printf("\n");

return 0;

}
OUTPUT:

Figure:10b

RESULT:
EXPNO: 10b DATE:30/12/24
SHELL SORT

AIM:

ALGORITHM:
CODE:

#include <stdio.h>

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

for (int gap = n / 2; gap > 0; gap /= 2) {

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

int temp = arr[i];

int j;

// Perform gapped insertion sort

for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {

arr[j] = arr[j - gap];

arr[j] = temp;

int main() {

int n;

printf("& .\n");

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

scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array:\n");

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


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

shellSort(arr, n);

printf("Sorted array: ");

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

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

printf("\n");

return 0;

}
OUTPUT:

Figure:10b.2

RESULT:
EXPNO: 10c DATE:30/12/24

INSERTION SORT

AIM:

ALGORITHM:
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, one position ahead

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

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

j--;

arr[j + 1] = key;

int main() {

int n;

printf(" & .\n");

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

scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array:\n");

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

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

}
insertionSort(arr, n);

printf("Sorted array: ");

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

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

printf("\n");

return 0;

}
OUTPUT:

Figure:10c.1

RESULT:
EXPNO: 10d DATE:6/1/25

IMPLEMENTATION OF HEAP SORT

AIM:

ALGORITHM:
CODE:

#include <stdio.h>

// Function to heapify a subtree rooted at index i

void heapify(int arr[], int n, int i) {

int largest = i; // Initialize largest as root

int left = 2 * i + 1; // Left child

int right = 2 * i + 2; // Right child

// Check if left child is larger than root

if (left < n && arr[left] > arr[largest]) {

largest = left;

// Check if right child is larger than the largest so far

if (right < n && arr[right] > arr[largest]) {

largest = right;

// If largest is not root, swap and heapify the affected subtree

if (largest != i) {

int temp = arr[i];

arr[i] = arr[largest];

arr[largest] = temp;

heapify(arr, n, largest);

}
// Main function to perform heap sort

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

// Build a max heap

for (int i = n / 2 - 1; i >= 0; i--) {

heapify(arr, n, i);

// Extract elements from heap one by one

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

// Move current root to end

int temp = arr[0];

arr[0] = arr[i];

arr[i] = temp;

// Call heapify on the reduced heap

heapify(arr, i, 0);

int main() {

int n;

printf(" & \n");

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

scanf("%d", &n);

int arr[n];

printf("Enter the elements of the array:\n");

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


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

heapSort(arr, n);

printf("Sorted array: ");

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

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

printf("\n");

return 0;

}
OUTPUT:

Figure10d.1

RESULT:
EXPE:11 DATE:6/1/25

COLLISION AND DOUBLE HASHING

Aim:

Algorithm:
CODE:
#include <stdio.h>
#define TABLE_SIZE 10
int hashTable[TABLE_SIZE];
// Initialize hash table
void initialize() {
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable[i] = -1;
}
}
// Primary hash function
int h1(int key) {
return key % TABLE_SIZE;
}
// Secondary hash function
int h2(int key) {
return 1 + (key % (TABLE_SIZE - 1));
}
// Insert key using double hashing
void insert(int key) {
int index = h1(key);
int step = h2(key);
int i = 0;

while (hashTable[(index + i * step) % TABLE_SIZE] != -1) {


i++;
if (i == TABLE_SIZE) {
printf("Hash table is full, cannot insert key %d\n", key);
return;
}
}
hashTable[(index + i * step) % TABLE_SIZE] = key;
}
// Search for a key
int search(int key) {
int index = h1(key);
int step = h2(key);
int i = 0;
while (hashTable[(index + i * step) % TABLE_SIZE] != -1) {
if (hashTable[(index + i * step) % TABLE_SIZE] == key) {
return (index + i * step) % TABLE_SIZE;
}
i++;
if (i == TABLE_SIZE) break;
}
return -1; // Key not found
}

// Display the hash table


void display() {
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable[i] != -1)
printf("Index %d: %d\n", i, hashTable[i]);
else
printf("Index %d: Empty\n", i);
}
}
int main() {
initialize();
int choice, key;
while (1) {
printf("\n 231211101259 & .\nMenu:\n1. Insert\n2. Search\n3.
Display\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
int result = search(key);
if (result != -1)
printf("Key found at index %d\n", result);
else
printf("Key not found\n");
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}
OUTPUT:

Figure:11.2

RESULT:.
CODE:

#include <stdio.h>

#define TABLE_SIZE 10

int hashTable[TABLE_SIZE];

// Initialize hash table

void initialize() {

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

hashTable[i] = -1;

// Primary hash function

int h1(int key) {

return key % TABLE_SIZE;

// Secondary hash function

int h2(int key) {

return 1 + (key % (TABLE_SIZE - 1));

// Insert key using double hashing

void insert(int key) {

int index = h1(key);


int step = h2(key);

int i = 0;

while (hashTable[(index + i * step) % TABLE_SIZE] != -1) {

i++;

if (i == TABLE_SIZE) {

printf("Hash table is full, cannot insert key %d\n", key);

return;

hashTable[(index + i * step) % TABLE_SIZE] = key;

// Search for a key

int search(int key) {

int index = h1(key);

int step = h2(key);

int i = 0;

while (hashTable[(index + i * step) % TABLE_SIZE] != -1) {

if (hashTable[(index + i * step) % TABLE_SIZE] == key) {

return (index + i * step) % TABLE_SIZE;

i++;

if (i == TABLE_SIZE) break;

}
return -1; // Key not found

// Display the hash table

void display() {

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

if (hashTable[i] != -1)

printf("Index %d: %d\n", i, hashTable[i]);

else

printf("Index %d: Empty\n", i);

int main() {

initialize();

int choice, key;

while (1) {

printf("\n 231211101259 & .\nMenu:\n1. Insert\n2. Search\n3. Display\n4.


Exit\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter key to insert: ");

scanf("%d", &key);

insert(key);
break;

case 2:

printf("Enter key to search: ");

scanf("%d", &key);

int result = search(key);

if (result != -1)

printf("Key found at index %d\n", result);

else

printf("Key not found\n");

break;

case 3:

display();

break;

case 4:

return 0;

default:

printf("Invalid choice\n");

return 0;

}
OUTPUT:

Figure:11.2

RESULT:.

You might also like