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

Ravindra Data

This document is a record notebook for the Data Structures Lab (EBCS22L01) for the academic year 2024-2025, detailing the work done by student B. Ravindra. It includes a bonafide certificate, an index of experiments, and various implementations of data structures using arrays and linked lists. The document outlines algorithms, flowcharts, and program codes for different data structures such as lists, stacks, queues, and trees.

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)
11 views

Ravindra Data

This document is a record notebook for the Data Structures Lab (EBCS22L01) for the academic year 2024-2025, detailing the work done by student B. Ravindra. It includes a bonafide certificate, an index of experiments, and various implementations of data structures using arrays and linked lists. The document outlines algorithms, flowcharts, and program codes for different data structures such as lists, stacks, queues, and trees.

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/ 163

FORM NO.

-F/TL/021
REV.00Date 20.03.2020

RECORD NOTEBOOK

DATA STURCTURES LAB–(EBCS22L01)

2024-2025(ODD SEMESTER)

DEPARTMENT
Of
COMPUTER SCIENCE AND ENGINEERING

NAME B.Ravindra
REGISTER NO 231211101200
COURSE B.TECH-CSE(AI)
YEAR/SEM/SEC II/III/BA
FORM NO.-F/TL/021
REV.00Date 20.03.2020

BONAFIDE CERTIFICATE

Register No : 231211101200

Name of Lab : DATA STRUCTURES (EBCS22L01)

Department : COMPUTER SCIENCE AND ENGINEERING (AI)

Certified that this is the bonafide record of work done by B.RAVINDRA,


231211101200 of II Year B.Tech(CSE-AI), Sec-‘BA’ in the DATA
STRUCTURES LAB (EBCS22L01) during the year 2024-2025.

Signature of Lab-in-Charge Signature of Head of Dept

Submitted for the Practical Examination held on -------------------------

Internal Examiner External Examiner


INDEX

EXP. STAFF
DATE TITLE PAGE NO.
NO SIGNATURE

IMPLEMENTATION LIST ADT


1a 01-07
USING ARRAY

IMPLEMENT LIST ADT USING


1b 08-17
LINKED LIST

IMPLEMENTATION OFSTACK
2a 18-25
USING ARRAY

IMPLEMENTATION OF STACK USING


2b 26-33
LINKED LIST

IMPLEMENTATION OF QUEUE
3a 34-41
USING ARRAY

IMPLEMENTATION OF QUEUE USING


3b 42-50
LINKED LIST

4a INFIX TO POSTFIX CONVERSATION 51-57

EVALUATION OFPOSTFIX
4b 58-66
EXPRESSION

BINARY TREE TRAVERSAL- IN ORDER,


5 PREORDER , POSTORDER 67-73
USING RECURSION

IMPLEMENT THE OPERATION OF


6 74-81
BINARY SEARCH TREE
7 IMPLEMENTATION OF BINARY HEAPS 82-90

IMPLEMENTATION OF BREADTH
8a 91-96
FIST SEARCH

IMPLEMENTATION OF DEPTH
8b 97-102
FIRST SEARCH

9a IMPLEMENTATION OF LINEAR SEARCH 103-107

IMPLEMENTATION OF
9b 108-112
BINARY SEARCH

10a BUBBLE SORT 113-117

10b SHELL SORT 118-122

10c INSERTION SORT 123-128

10d IMPLEMENTATION OF HEAP SORT 129-134

11 COLLUSION AND DOUBLE HASHING 135-141


EXPNO: 1a DATE:
IMPLEMENTATION LIST ADT USING ARRAY

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h> // Use iostream.h for Turbo C++

#include <conio.h> // For clrscr(), textbackground(), textcolor(), and getch()

#include <stdlib.h> // For system() if needed

class List {

private:

int* arr;

int capacity;

int size;

public:

List(int cap) : capacity(cap), size(0) { arr = new int[capacity]; }

~List() { delete[] arr; }

void insert(int element) {

if (size == capacity) { cout << "List is full!" << endl; return; }

arr[size++] = element;

void insertAt(int index, int element) {

if (index < 0 || index > size || size == capacity) { cout << "Invalid index!" << endl;
return; }

for (int i = size; i > index; --i) arr[i] = arr[i - 1];

arr[index] = element; size++;

void removeAt(int index) {

if (index < 0 || index >= size) { cout << "Invalid index!" << endl; return; }
for (int i = index; i < size - 1; ++i) arr[i] = arr[i + 1];

size--;

int get(int index) {

if (index < 0 || index >= size) { cout << "Invalid index!" << endl; return -1; }

return arr[index];

void display() {

if (size == 0) { cout << "List is empty." << endl; return; }

cout << "List elements: ";

for (int i = 0; i < size; i++) cout << arr[i] << " ";

cout << endl;

int getSize() { return size; }

};

int main() {

clrscr(); // Clear the screen (Turbo C++ specific)

// Set background color to white and text color to black

textbackground(15); // White background

textcolor(0); // Black text

clrscr(); // Apply the background color to the screen

// Display name and ID

cout << "Name:B. AMRUTHA VARSHINI"

<< endl; cout << "ID: 23211101213"


<< endl;
int capacity, choice, element, index;

cout << "Enter the capacity of the list: ";

cin >> capacity;

List list(capacity);

do {

cout << "\nMenu:\n1. Insert\n2. Insert at Index\n3. Remove at Index\n4. Get


Element\n5. Display\n6. Size\n7. Exit\nEnter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter element to insert: ";

cin >> element;

list.insert(element);

break;

case 2:

cout << "Enter index and element to insert: ";

cin >> index >> element;

list.insertAt(index, element);

break;

case 3:

cout << "Enter index to remove: ";

cin >> index;

list.removeAt(index);
break;
case 4:

cout << "Enter index to get element: ";

cin >> index;

cout << "Element at index " << index << " is: " << list.get(index) << endl;

break;

case 5:

list.display();

break;

case 6:

cout << "Size of the list: " << list.getSize() << endl;

break;

case 7:

cout << "Exiting program." << endl;

break;

default:

cout << "Invalid choice!" << endl;

} while (choice != 7);

return 0;

8
OUTPUT:

RESULT:

9
EXPNO: 1b DATE:
IMPLEMENT LIST ADT USING LINKED LIST

AIM:

ALGORITHM:

10
.

11
FLOWCHART:

12
PROGRAM CODE:

#include <iostream.h> // Use iostream.h for Turbo C++

#include <conio.h> // For clrscr(), textbackground(), textcolor(), and getch()

class Node {

public:

int data;

Node* next;

Node(int value) : data(value), next(NULL) {} // Use NULL instead of nullptr

};

class List {

Node* head;

public:

List() : head(NULL) {} // Use NULL instead of nullptr

void insert(int value, int pos);

void remove(int pos);

int search(int value);

void update(int pos, int value);

void display();

};

void List::insert(int value, int pos) {

Node* newNode = new Node(value);

if (pos == 0) {

newNode->next = head;

head = newNode;
13
return;

Node* temp = head;

for (int i = 0; temp && i < pos - 1; ++i) temp = temp->next;

if (!temp) { cout << "Position out of range!\n"; return; }

newNode->next = temp->next;

temp->next = newNode;

void List::remove(int pos) {

if (!head) { cout << "List is empty!\n"; return; }

Node* temp = head;

if (pos == 0) { head = head->next; delete temp; return; }

for (int i = 0; temp && i < pos - 1; ++i) temp = temp->next;

if (!temp || !temp->next) { cout << "Position out of range!\n"; return; }

Node* del = temp->next;

temp->next = del->next;

delete del;

int List::search(int value) {

Node* temp = head;

int pos = 0;

while (temp) {

if (temp->data == value) return pos;

temp = temp->next;
14
pos++;

return -1;

void List::update(int pos, int value) {

Node* temp = head;

for (int i = 0; temp && i < pos; ++i) temp = temp->next;

if (!temp) { cout << "Position out of range!\n"; return; }

temp->data = value;

void List::display() {

if (!head) { cout << "List is empty!\n"; return; }

Node* temp = head;

while (temp) {

cout << temp->data << " -> ";

temp = temp->next;

cout << "NULL\n";

int main() {

clrscr(); // Clear the screen (Turbo C++ specific)

// Set background color to white and text color to black

textbackground(15); // White background

textcolor(0); // Black text


15
clrscr(); // Apply the background color to the screen

// Display name and ID

cout << "Name: B.AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;

List list;

int choice, value, pos;

do {

cout << "\nMenu:\n1. Insert\n2. Remove\n3. Search\n4. Update\n5. Display\n6.


Exit\nEnter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter value and position to insert: ";

cin >> value >> pos;

list.insert(value, pos);

break;

case 2:

cout << "Enter position to remove: ";

cin >> pos;

list.remove(pos);

break;

case 3:

cout << "Enter value to search: ";


cin >> value;
pos = list.search(value);
if (pos != -1) cout << "Value found at position: " << pos << endl;

else cout << "Value not found\n";

break;

case 4:

cout << "Enter position and new value to update: ";

cin >> pos >> value;

list.update(pos, value);

break;

case 5:

list.display();

break;

case 6:

cout << "Exiting program." << endl;

break;

default:

cout << "Invalid choice!" << endl;

} while (choice != 6);

return 0;

}
OUTPUT:

RESULT:
EXPNO: 2a DATE:
IMPLEMENTATION OF STACK USING ARRAY

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

#define SIZE 5

class Stack {

private:

int top, a[SIZE];

public:

Stack() {

top = -1;

void push(int num) {

if (top >= SIZE - 1) {

cout << "Stack is Full\n";

return;

a[++top] = num;

int pop() {

if (top < 0) {

cout << "Stack is Empty\n";

return 0;

}
return a[top--];

void display() {

if (top < 0) {

cout << "Stack is Empty\n";

return;

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

cout << a[i] << "\n";

};

void main() {

clrscr();

textbackground(15);

textcolor(0 );

clrscr();

cout << "Name: B.AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;

Stack s;

int val, ch, no;

do {

cout << "\nEnter the Choice: 1.Insert 2.Delete 3.Display 4.Exit: ";
cin >> ch;
switch (ch) {

case 1:

cout << "Enter Value: ";

cin >> no;

s.push(no);

break;

case 2:

val = s.pop();

if (val != 0) {

cout << "The Deleted Value is: " << val << "\n";

break;

case 3:

s.display();

break;

case 4:

cout << "Exiting...\n";

exit(0);

break;

default:

cout << "Invalid Choice!\n";

} while (ch != 4);


getch();

}
OUTPUT:

RESULT:
EXPNO: 2b DATE:
IMPLEMENTATION OF STACK USING LINKED LIST

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

class Stack {

private:

struct Node {

int data;

Node *link;

} *top;

public:

Stack() {

top = NULL;

void push(int num) {

Node *t = new Node;

t->data = num;

t->link =top;

top = t;

void pop() {

if (top == NULL) {

cout << "Stack is Empty\n";

return;
}

Node *temp = top;

cout << "The Deleted Value is: " << top->data << "\n";

top = top->link;

delete temp;

void display() {

if (top == NULL) {

cout << "Stack is Empty\n";

return;

for (Node *temp = top; temp != NULL; temp = temp->link) {

cout << temp->data << "\n";

};

void main() {

clrscr();

textbackground(15);

textcolor(0);

clrscr();

cout << "Name:B. AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;
Stack s;

int ch, no;

do {

cout << "\nEnter the Choice: 1.Insert 2.Delete 3.Display 4.Exit: ";

cin >> ch;

switch (ch) {

case 1:

cout << "Enter Value: ";

cin >> no;

s.push(no);

break;

case 2:

s.pop();

break;

case 3:

s.display();

break;

case 4:

cout << "Exiting...\n";

exit(0);

break;

default:

cout << "Invalid Choice!\n";

}
} while (ch != 4);

getch();

}
OUTPUT:

RESULT:
EXPNO: 3a DATE:
IMPLEMENTATION OF QUEUE USING ARRAY

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

class Stack {

private:

struct Node {

int data;

Node *link;

}*top;

public:

Stack() {

top = NULL;

void push(int num) {

Node *t = new Node;

t->data = num;

t->link = top;

top = t;

void pop() {

if (top == NULL) {

cout << "Stack is Empty\n";

return;
}

Node *temp = top;

cout << "The Deleted Value is: " << top->data << "\n";

top = top->link;

delete temp;

void display() {

if (top == NULL) {

cout << "Stack is Empty\n";

return;

for (Node *temp = top; temp != NULL; temp = temp->link) {

cout << temp->data << "\n";

};

void main() {

clrscr();

textbackground(15);

textcolor(0);

clrscr();

cout << "Name: B.AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;
Stack s;

int ch, no;

do {

cout << "\nEnter the Choice: 1.Insert 2.Delete 3.Display 4.Exit: ";

cin >> ch;

switch (ch) {

case 1:

cout << "Enter Value: ";

cin >> no;

s.push(no);

break;

case 2:

s.pop();

break;

case 3:

s.display();

break;

case 4:

cout << "Exiting...\n";

exit(0);

break;

default:

cout << "Invalid Choice!\n";

}
} while (ch != 4);

getch();

}
OUTPUT:

RESULT:
EXPNO: 3b DATE:
IMPLEMENTATION OF QUEUE USING LINKED LIST

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

class Queue {

private:

struct Node {

int data;

Node* link;

} *front, *rear;

public:

Queue() {

front = rear = NULL;

void enq(int num) {

Node* t = new Node;

t->data = num;

t->link = NULL;

if (front == NULL) {

front = rear = t;

} else {

rear->link = t;

rear = t;

}
}

int deq() {

if (front == NULL) {

textcolor(12); // Red for error

cout << "\nQueue is Empty";

textcolor(15); // Reset to white

return -1;

} else {

Node* temp = front;

int deletedValue = front->data;

front = front->link;

if (front == NULL) {

rear = NULL;

delete temp;

return deletedValue;

void dis() {

if (front == NULL) {

textcolor(12); // Red for error

cout << "\nQueue is Empty";

textcolor(15); // Reset to white

return;
}

Node* t = front;

textcolor(10); // Green for output

cout << "\nQueue Elements: ";

while (t != NULL) {

cout << t->data << " ";

t = t->link;

cout << endl;

textcolor(15); // Reset to white

};

void main() {

clrscr();

textbackground(15); // Blue background

textcolor(0); // Yellow text

clrscr();

cout << "Name: B.AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;

Queue q;

int ch, no;

do {
textcolor(11); // Cyan for prompts
cout << "\nEnter the Choice: 1.Insert 2.Delete 3.Display 4.Exit: ";

textcolor(15); // Reset to white

cin >> ch;

switch (ch) {

case 1:

textcolor(13); // Magenta for input prompts

cout << "\nEnter Value: ";

textcolor(15); // Reset to white

cin >> no;

q.enq(no);

break;

case 2: {

int deletedValue = q.deq();

if (deletedValue != -1) {

textcolor(14); // Yellow for output

cout << "\nThe Deleted Value is: " << deletedValue;

textcolor(15); // Reset to white

break;

case 3:

q.dis();

break;

case 4:
textcolor(12); // Red for exit message

cout << "\nExiting...\n";

textcolor(15); // Reset to white

exit(0);

default:

textcolor(12); // Red for error

cout << "\nInvalid Choice!";

textcolor(15); // Reset to white

} while (ch != 4);

getch();

}
OUTPUT:

RESULT:
EXPNO: 4a DATE:
INFIX TO POSTFIX CONVERSATION

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <conio.h>

#include <malloc.h>

char inf[40], post[40];

int top = -1, st[20];

void postfix();

void push(int);

char pop();

void main() {

clrscr();

textbackground(15); // Blue background

textcolor(0); // Yellow text

clrscr();

cout << "Name: B.AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;

textcolor(11); // Cyan for input prompt

cout << "\nEnter the infix Expression: ";

textcolor(15); // Reset to white

cin >> inf;

postfix();

getch();

}
void postfix() {

int i, j = 0;

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

switch (inf[i]) {

case '+':

case '-':

while (top >= 0 && (st[top] == 1 || st[top] == 2 || st[top] == 3 || st[top] == 4)) {

post[j++] = pop();

push(inf[i] == '+' ? 1 : 2);

break;

case '*':

case '/':

while (top >= 0 && (st[top] == 3 || st[top] == 4)) {

post[j++] = pop();

push(inf[i] == '*' ? 3 : 4);

break;

case '^':

while (top >= 0 && st[top] == 5) {

post[j++] = pop();

push(5);

break;
case '(':

push(0);

break;

case ')':

while (top >= 0 && st[top] != 0) {

post[j++] = pop();

top--;

break;

default:

textcolor(10); // Green for operands

post[j++] = inf[i];

textcolor(15); // Reset to white

while (top >= 0) {

post[j++] = pop();

post[j] = '\0';

textcolor(13); // Magenta for output

cout << "Postfix Expression is: " << post << endl;

textcolor(15); // Reset to white

}
void push(int ele) {

st[++top] = ele;

char pop() {

int el = st[top--];

switch (el) {

case 1: return '+';

case 2: return '-';

case 3: return '*';

case 4: return '/';

case 5: return '^';

default: return '\0';

}
OUTPUT:

RESULT:
EXPNO: 4b DATE:
EVALUATION OF POSTFIX EXPRESSION

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <ctype.h>

#include <stdio.h>

#include <conio.h>

#define MAXSTACK 100

#define POSTFIXSIZE 100

int stack[MAXSTACK];

int top = -1;

void push(int item)

if (top >= MAXSTACK - 1)

cout << "Stack overflow";

return;

else

top = top + 1;

stack[top] = item;

int pop()

{
int item;

if (top < 0)

cout << "Stack underflow";

else

item = stack[top];

top = top - 1;

return item;

return (item);

void EvalPostfix(char postfix[])

int i;

char ch;

int val;

int A, B;

for (i = 0; postfix[i] != ')'; i++)

ch = postfix[i];

if (isdigit(ch))

{
push(ch - '0');

else if (ch == '*' || ch == '/' || ch == '+' || ch == '-')

A = pop();

B = pop();

switch (ch)

case '*':

val = B * A;

break;

case '/':

val = B / A;

break;

case '+':

val = B + A;

break;

case '-':

val = B - A;

break;

push(val);

}
textcolor(13); // Magenta for output

cout << "Result of expression evaluation: " << pop() << endl;

textcolor(15); // Reset to white

void main()

clrscr(); // Clear the screen (Turbo C++ specific)

textbackground(15); // Blue background

textcolor(0); // Yellow text

clrscr();

cout << "Name: B.AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;

textcolor(11); // Cyan for input prompt

cout << "Enter postfix expression, press ')' for end expression: ";

textcolor(15); // Reset to white

int i;

char postfix[POSTFIXSIZE];

for (i = 0; i <= POSTFIXSIZE - 1; i++)

cin >> postfix[i];

if (postfix[i] == ')')

break;
}
}

EvalPostfix(postfix);

getch(); // Wait for user input (Turbo C++ specific)

}
OUTPUT:

RESULT:
EXPNO: 05 DATE:
BINARY TREE TRAVERSAL- IN ORDER, PREORDER ,
POSTORDER USING RECURSION

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <conio.h>

struct Node {

int data;

Node* left;

Node* right;

Node(int data) {

this->data = data;

left = right = NULL;

};

void inOrder(Node* node) {

if (node == NULL) return;

inOrder(node->left);

cout << node->data << " ";

inOrder(node->right);

void preOrder(Node* node) {

if (node == NULL) return;

cout << node->data << " ";

preOrder(node->left);

preOrder(node->right);

}
void postOrder(Node* node) {

if (node == NULL) return;

postOrder(node->left);

postOrder(node->right);

cout << node->data << " ";

int main() {

clrscr();

textbackground(15);

textcolor(0);

clrscr();

cout << "Name: B.AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;

Node* root = new Node(1);

root->left = new Node(2);

root->right = new Node(3);

root->left->left = new Node(4);

root->left->right = new Node(5);

textcolor(11);

cout << "\nPreorder traversal of binary tree is:\n";

textcolor(15);

preOrder(root);
textcolor(11);

cout << "\n\nInorder traversal of binary tree is:\n";

textcolor(15);

inOrder(root);

textcolor(11);

cout << "\n\nPostorder traversal of binary tree is:\n";

textcolor(15);

postOrder(root);

getch();

return 0;

}
OUTPUT:

RESULT:
EXPNO: 06 DATE:
IMPLEMENT THE OPERATION OF BINARY SEARCH TREE

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <conio.h>

struct Node {

int data;

Node *left, *right;

Node(int item) {

data = item;

left = right = NULL;

};

Node* create(int item) {

return new Node(item);

void inorder(Node* root) {

if (!root) return;

inorder(root->left);

cout << root->data << " ";

inorder(root->right);

Node* findMinimum(Node* cur) {

while (cur->left != NULL) cur = cur->left;

return cur;

}
Node* insertion(Node* root, int item) {

if (!root) return create(item);

if (item < root->data)

root->left = insertion(root->left, item);

else

root->right = insertion(root->right, item);

return root;

Node* deletion(Node* root, int item) {

if (!root) return root;

if (item < root->data)

root->left = deletion(root->left, item);

else if (item > root->data)

root->right = deletion(root->right, item);

else {

if (!root->left) {

Node* temp = root->right;

delete root;

return temp;

} else if (!root->right) {

Node* temp = root->left;

delete root;

return temp;
}

Node* succ = findMinimum(root->right);

root->data = succ->data;

root->right = deletion(root->right, succ->data);

return root;

void main() {

clrscr();

textbackground(15); // Blue background

textcolor(0); // Yellow text

clrscr();

cout << "Name:B. AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;

Node* root = NULL;

// Insertion of nodes

root = insertion(root, 45);

root = insertion(root, 30);

root = insertion(root, 50);

root = insertion(root, 25);

root = insertion(root, 35);

root = insertion(root, 60);

root = insertion(root, 4);


textcolor(11); // Cyan for traversal titles

cout << "Inorder Traversal: ";

textcolor(15); // White for traversal result

inorder(root);

cout << endl;

root = deletion(root, 25);

textcolor(11); // Cyan for traversal titles

cout << "After Deletion of 25: ";

textcolor(15); // White for traversal result

inorder(root);

cout << endl;

root = insertion(root, 2);

textcolor(11); // Cyan for traversal titles

cout << "After Insertion of 2: ";

textcolor(15); // White for traversal result

inorder(root);

cout << endl;

getch();

}
OUTPUT:

RESULT:
EXPNO: 07 DATE:
IMPLEMENTATION OF BINARY HEAPS

AIM:

ALGORITHM:

FLOWCHART:
PROGRAM CODE:

#include <iostream.h>

#include <limits.h> // Use this header for INT_MAX

#include <conio.h>

// Node structure for reference (not used in heap implementation here)

struct Node {

int data;

Node *left, *right;

Node(int item) {

data = item;

left = right = NULL;

};

// Utility function to swap two integers

void swap(int *x, int *y) {

int temp = *x;

*x = *y;

*y = temp;

// MinHeap class definition

class MinHeap {

int *harr; // Pointer to the heap array

int capacity; // Maximum size of the heap

int heap_size; // Current size of the heap


public:

MinHeap(int capacity);

void MinHeapify(int i);

int parent(int i) { return (i - 1) / 2; }

int left(int i) { return (2 * i + 1); }

int right(int i) { return (2 * i + 2); }

int extractMin();

void decreaseKey(int i, int new_val);

int getMin() { return harr[0]; }

void deleteKey(int i);

void insertKey(int k);

};

// Constructor: Initialize heap

MinHeap::MinHeap(int cap) {

heap_size = 0;

capacity = cap;

harr = new int[cap]; // Allocate memory dynamically

// Insert a new key into the heap

void MinHeap::insertKey(int k) {

if (heap_size == capacity) {

cout << "\nOverflow: Could not insertKey\n";

return;

}
heap_size++;

int i = heap_size - 1;

harr[i] = k;

while (i != 0 && harr[parent(i)] > harr[i]) {

swap(&harr[i], &harr[parent(i)]);

i = parent(i);

// Decrease the value of a key

void MinHeap::decreaseKey(int i, int new_val) {

harr[i] = new_val;

while (i != 0 && harr[parent(i)] > harr[i]) {

swap(&harr[i], &harr[parent(i)]);

i = parent(i);

// Extract the minimum element

int MinHeap::extractMin() {

if (heap_size <= 0)

return INT_MAX;

if (heap_size == 1) {

heap_size--;

return harr[0];

}
int root = harr[0];

harr[0] = harr[heap_size - 1];

heap_size--;

MinHeapify(0);

return root;

// Delete a key from the heap

void MinHeap::deleteKey(int i) {

decreaseKey(i, INT_MIN);

extractMin();

// Heapify a subtree with root at index i

void MinHeap::MinHeapify(int i) {

int l = left(i);

int r = right(i);

int smallest = i;

if (l < heap_size && harr[l] < harr[i])

smallest = l;

if (r < heap_size && harr[r] < harr[smallest])

smallest = r;

if (smallest != i) {

swap(&harr[i], &harr[smallest]);

MinHeapify(smallest);

}
}

// Main function

void main() {

clrscr(); // Clear screen

// Set background to white and text to black

textbackground(WHITE);

textcolor(BLACK);

clrscr(); // Apply colors

cout << "Name: B.AMRUTHA VARSHINI"

<< endl; cout << "ID: 231211101213"

<< endl;

MinHeap h(11);

// Perform heap operations

h.insertKey(3);

h.insertKey(2);

h.deleteKey(1);

h.insertKey(15);

h.insertKey(5);

h.insertKey(4);

h.insertKey(45);

cout << "Extracted Min: ";

cout << h.extractMin() << endl;

cout << "Current Min: ";

cout << h.getMin() << endl;


h.decreaseKey(2, 1);

cout << "New Min after decreaseKey: ";

cout << h.getMin() << endl;

getch(); // Wait for keypress

}
OUTPUT:

RESULT:
EXPNO: 8a DATE:
IMPLEMENTATION OF BREADTH FIST SEARCH

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int cost[10][10], i, j, k, n, qu[10], front, rare, v, visit[10], visited[10];

int main()

int m;

clrscr(); // Clear the screen for Turbo C++ specific

// Set background to white and text to black

textbackground(WHITE);

textcolor(BLACK);

cout << "Enter number of vertices: ";

cin >> n;

cout << "Enter number of edges: ";

cin >> m;

cout << "\nEDGES\n";

// Initialize the cost matrix to 0

for (i = 0; i < 10; i++) {

for (j = 0; j < 10; j++) {

cost[i][j] = 0;

// Input edges and set cost


for (k = 1; k <= m; k++) {

cin >> i >> j;

cost[i][j] = 1; // Assuming undirected graph, set both directions

cost[j][i] = 1;

// Display name and ID

textcolor(BLACK);

cout << "\nName:

B. AMRUTHA VARSHINI

\n"; cout << "ID:

231211101213\n";

// Initial vertex for BFS traversal

cout << "\nEnter initial vertex to traverse from: ";

cin >> v;

// Set text color for visited vertices

textcolor(GREEN);

cout << "\nVisited vertices: " << v << " ";

visited[v] = 1;

k = 1; // To count visited vertices

// Breadth-First Search (BFS) traversal

while (k < n) {

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

if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1) {

visit[j] = 1;
qu[rare++] = j;
}
}

v = qu[front++];

cout << v << " "; // Print the visited vertex

k++;

visit[v] = 0;

visited[v] = 1;

getch(); // Wait for user input (Turbo C++ specific)

return 0;

}
OUTPUT:

RESULT:
EXPNO: 8b DATE:
IMPLEMENTATION OF DEPTH FIRST SEARCH

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int cost[10][10], i, j, k, n, qu[10], front, rare, v, visit[10], visited[10];

int main()

int m;

clrscr(); // Clear the screen for Turbo C++ specific

// Set background to white and text to black

textbackground(WHITE);

textcolor(BLACK);

cout << "Enter number of vertices: ";

cin >> n;

cout << "Enter number of edges: ";

cin >> m;

cout << "\nEDGES\n";

// Initialize the cost matrix to 0

for (i = 0; i < 10; i++) {

for (j = 0; j < 10; j++) {

cost[i][j] = 0;

// Input edges and set cost


for (k = 1; k <= m; k++) {

cin >> i >> j;

cost[i][j] = 1; // Assuming undirected graph, set both directions

cost[j][i] = 1;

// Display name and ID

textcolor(BLACK);

cout<<"\nName:B.AMRUTH

A VARSHINI\;

cout << "ID: 231211101213\n";

// Initial vertex for BFS traversal

cout << "\nEnter initial vertex to traverse from: ";

cin >> v;

// Set text color for visited vertices

textcolor(GREEN);

cout << "\nVisited vertices: " << v << " ";

visited[v] = 1;

k = 1; // To count visited vertices

// Breadth-First Search (BFS) traversal

while (k < n) {

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

if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1) {

visit[j] = 1;

qu[rare++] = j;
}
}

v = qu[front++];

cout << v << " "; // Print the visited vertex

k++;

visit[v] = 0;

visited[v] = 1;

getch(); // Wait for user input (Turbo C++ specific)

return 0;

}
OUTPUT:

RESULT:
EXPNO: 9a DATE:
MPLEMENTATION OF LINEAR SEARCH

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h>

void main() {

// Set background to blue and text to yellow

textbackground(WHITE);

textcolor(BLACK);

clrscr(); // Clear the screen for Turbo C++ specific

int a[100], i, n, item, s = 0;

// Displaying the name and ID

cout <<

"\nName:B.AMRUTHA

VARSHINI\n"; cout << "ID:

231211101213\n";

cout << "\n--------- LINEAR SEARCH --------- \n\n";

cout << "Enter No. of Elements = ";

cin >> n;

cout << "\nEnter Elements:\n";

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

cin >> a[i];

cout << "\nEnter Element you want to Search: ";

cin >> item;

// Linear Search Logic


for(i = 1; i <= n; i++) {
if(a[i] == item) { // Corrected from '=' to '=='
cout << "\nData is Found at Location: " << i;

s = 1;

break;

if(s == 0) {

cout << "Data is Not Found";

getch(); // Wait for user input before closing

}
OUTPUT:

RESULT:
EXPNO: 9b DATE:
IMPLEMENTATION OF BINARY SEARCH

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h>

void main() {

// Set background to white and text to black

textbackground(WHITE);

textcolor(BLACK);

clrscr(); // Clear the screen for Turbo C++ specific

int a[100], n, i, beg, end, mid, item;

// Display Name and ID

cout<<"\nName:B.AMR

UTHA VARSHINI\n;

cout << "ID:

231211101213\n";

cout << "\n---------- BINARY SEARCH ---------- \n";

cout << "Enter No. of Elements = ";

cin >> n;

cout << "\nEnter Elements in Sorted Order:\n";

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

cin >> a[i];

cout << "\nEnter Item you want to Search: ";

cin >> item;

beg = 1;
end = n;
mid = (beg + end) / 2; // Corrected Mid Calculation
// Binary Search Logic

while(beg <= end && a[mid] != item) {

if(a[mid] < item)

beg = mid + 1; // Corrected from 'beg = mid + l' to 'beg = mid + 1'

else

end = mid - 1;

mid = (beg + end) / 2;

// Output the result

if(a[mid] == item) {

cout << "\nData is Found at Location: " << mid;

} else {

cout << "Data is Not Found";

getch(); // Wait for user input before closing

}
OUTPUT:

RESULT:
EXPNO: 10a DATE:
BUBBLE SORT

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h>

int main()

// Set background to white and text to black

textbackground(WHITE);

textcolor(BLACK);

clrscr(); // Clear the screen for Turbo C++ specific

int i, j, k, temp;

int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23}

// Display name and ID

cout<<"\nName:B.AMR

UTHA VARSHINI\n;

cout << "ID:

231211101213\n";

cout << "\nPrinting sorted elements...\n";

// Insertion sort logic

for(k = 1; k < 10; k++)

temp = a[k];

j = k - 1;

while(j >= 0 && temp <= a[j])

{
a[j + 1] = a[j];
j = j - 1;
}

a[j + 1] = temp;

// Printing sorted array

for(i = 0; i < 10; i++)

cout << a[i] << "\n";

getch(); // Wait for user input before closing

return 0; // Corrected return position

}
OUTPUT:

RESULT:
EXPNO: 10b DATE:
SHELL SORT

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h>

void shellsort(int arr[], int num) {

int i, j, k, tmp;

for(i = num / 2; i > 0; i = i / 2) { // Decrease gap

for(j = i; j < num; j++) {

for(k = j - i; k >= 0; k = k - i) { // Compare with elements before gap

if(arr[k + i] >= arr[k])

break;

else {

tmp = arr[k];

arr[k] = arr[k + i];

arr[k + i] = tmp;

int main() {

// Set background to white and text to black

textbackground(WHITE);

textcolor(BLACK);

clrscr(); // Clear the screen for Turbo C++ specific


int arr[30];

int k, num;

// Display name and ID

cout <<

"Name:B.AMRUTHA

VARSHINI\n"; cout <<

"ID:

231211101213\n";

cout << "Enter total no. of elements: ";

cin >> num;

cout << "\nEnter " << num << " numbers: ";

for(k = 0; k < num; k++) {

cin >> arr[k];

shellsort(arr, num);

cout << "\nSorted array is: ";

for(k = 0; k < num; k++) {

cout << arr[k] << " ";

getch(); // Wait for user input before closing

return 0;

}
OUTPUT:

RESULT:
EXPNO: 10c DATE:
INSERTION SORT

AIM:

ALGORITHM:
FLOW CHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h>

// Template function for Insertion Sort

template <class T>

void isort(T a[], int n) {

int i, j; // Declare i and j only once

T t; // Use template type T for t for

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

t = a[i]; // Current element

j = i - 1;

// Shift elements of the sorted portion to find the correct position for t

while (j >= 0 && t < a[j]) {

a[j + 1] = a[j];

j--;

// Place the element in its correct position

a[j + 1] = t;

void main() {

clrscr(); // Clear the screen for Turbo C++ specific

// Set background color to white and text color to black

textbackground(WHITE);
textcolor(BLACK);

clrscr(); // Apply the color changes to the entire screen

int a[100], n;

int i; // Declare `i` only once for all loops in main

// Display name and ID

cout <<

"Name:B.AMRUTHA

VARSHINI\n"; cout <<

"ID:

231211101213\n";

// Input: Number of elements

cout << "Enter number of elements: ";

cin >> n;

// Input: Array elements

cout << "Enter elements (Use Spacebar as Separator):" << endl;

for (i = 0; i < n; i++) { // Use the declared `i`

cin >> a[i];

// Perform insertion sort

isort(a, n);

// Output: Sorted array

cout << "After sorting, the elements are:" << endl;

for (i = 0; i < n; i++) { // Use the declared `i`


cout << a[i] << " ";

}
cout << endl;
getch(); // Wait for user input before closing

• .
OUTPUT:

RESULT:
EXPNO: 10d DATE:
IMPLEMENTATION OF HEAP SORT

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

// Function to perform Heap Sort

void heapsort(int x[], int n)

int i, elt, s, f, ivalue; // Declare variables only once

// Build the heap (max-heap)

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

elt = x[i];

s = i;

f = (s - 1) / 2;

while (s > 0 && x[f] < elt) { // Fixing comparison here

x[s] = x[f];

s = f;

f = (s - 1) / 2;

x[s] = elt;

// Sorting the heap

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

ivalue = x[i];

x[i] = x[0];
f = 0;

s = 1;

// Choose the larger child

if (i > 2 && x[2] > x[1])

s = 2;

// Heapify the root element

while (s < i && ivalue < x[s]) {

x[f] =x[s];

f = s;

s = 2 * f + 1;

if (s + 1 < i && x[s] < x[s + 1])

s = s + 1;

x[f] = ivalue;

void main()

// Set the background color to white and the text color to black

textbackground(WHITE);

textcolor(BLACK);

clrscr(); // Clear the screen to apply the color changes

int x[10], n;

int i; // Declare `i` only once in this function


// Display name and ID

cout <<

"Name:B.AMRUTHA

VARSHINI\n"; cout <<

"ID:

231211101213\n";

// Input number of elements

cout << "Enter No. Of Elements: ";

cin >> n;

// Input elements

cout << "Enter the elements: ";

for (i = 0; i < n; i++) { // Use the declared `i`

cin >> x[i];

// Perform Heap Sort

heapsort(x, n);

// Output sorted result

cout << "Sorted Result: ";

for (i = 0; i < n; i++) { // Use the declared `i`

cout << x[i] << " ";

getch();

}
OUTPUT:

Result:
EXPNO: 11 DATE:
COLLUSION AND DOUBLE HASHING

AIM:

ALGORITHM:
FLOWCHART:
PROGRAM CODE:

#include<iostream.h>

#include<conio.h

int main() {

int hash[20], pos, n, ch, j = 0;

// Initialize hash table

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

hash[i] = 0;

clrscr(); // Clear the screen

// Set background color to white and text color to black

textbackground(WHITE);

textcolor(BLACK);

clrscr(); // Apply the background color

// Display name and ID

cout<<"Name:B.AMRUT

HA VARSHINI/n;

cout << "ID: 231211101213\n";

// Menu-based loop

while (1) {

cout << "\n1. Insert 2. Delete 3. Display 4. Exit\nEnter your choice: ";

cin >> ch;

switch (ch) {

case 1:
cout << "\nEnter element: ";
cin >> n;

pos = n % 20;

// Linear Probing for insertion

if (hash[pos] == 0) {

hash[pos] = n;

} else {

pos = (pos + (j * (23 - (n % 23)))) % 20;

j++;

while (hash[pos] != 0 && j != 20) {

pos = (pos + (j * (23 - (n % 23)))) % 20;

j++;

if (j == 20) {

cout << "Hash table is full\n";

} else {

hash[pos] = n;

j = 0;

break;

case 2:

cout << "\nEnter element to delete: ";

cin >> n;

pos = n % 20;
// Linear Probing for deletion

if (hash[pos] == n) {

hash[pos] = 0;

} else {

pos = (pos + (j * (23 - (n % 23)))) % 20;

j++;

while (hash[pos] != n && j != 20) {

pos = (pos + (j * (23 - (n % 23)))) % 20;

j++;

if (j == 20) {

cout << "\nElement not found\n";

} else {

hash[pos] = 0;

j = 0;

break;

case 3:

cout << "\nHash Table: ";

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

cout << hash[i] << " ";

}
cout << endl;

break;

case 4:

return 0; // Exit the program

default:

cout << "\nWrong option! Try again...\n";

break;

return 0;

}
OUTPUT:

RESULT:

You might also like