Ds Adip
Ds Adip
Acropolis Institute of
Technology and
Research, Indore
Department of CSE
Submitted To: Dr. Mayur Rathi (Artificial Intelligence & Machine
Learning)
Submitted By:
Name of Student
Enrollment No. : 0827
Class/Year/Sem : ALS-1/2nd / 3rd
CERTIFICATE
This is to certify that the experimental work entered in this journal as per
the B. TECH. II year syllabus prescribed by the RGPV was done by Mr./
2024- 2025.
In this lab, students will be able to learn and practice basic data structures programming. Students
can expand their skill set by practical learning on various data structures and to understand the
processing of different algorithm for problem-solving. This lab complements the data structures and
computer algorithms courses. Data Structures provides the requisite environment for design and
analysis of algorithms for solving complex problems in the field of computer science. Students gain
practical knowledge by writing and executing programs in C/C++/JAVA/Python using various data
structures and implementing algorithm principles. The latest platforms compilers are provided to the
students to run their programs.
GENERAL INSTRUCTIONS FOR LABORATORY CLASSES
DO’S
While entering into the LAB students should wear their ID cards.
Students should sign in the LOGIN REGISTER before entering into the
laboratory.
Students should come with observation and record note book to the laboratory.
After completing the laboratory exercise, make sure to shutdown the system
properly.
DONT’S
Module3: Tree: Definitions - Height, depth, order, degree etc. Binary Search Tree -
Operations, Traversal, Search. AVL Tree, Heap, Applications and comparison of various
types of tree; Introduction to forest, multi-way Tree, B tree, B+ tree, B* tree and red-
black tree.
Module5: Sorting: Introduction, Sort methods like: Bubble Sort, Quick sort. Selection
sort, Heap sort, Insertion sort, Shell sort, Merge sort and Radix sort; comparison of
various sorting techniques. Searching: Basic Search Techniques: Sequential search,
Binary search, Comparison of search methods. Hashing & Indexing. Case Study:
Application of various data structures in operating system, DBMS etc.
HARDWARE AND SOFTWARE REQUIREMENTS:
PREREQUISITE:-
Experience with a high level language (C/C++, Java, Python) is suggested. Prior
knowledge of a Object-Oriented concepts is helpful but not mandatory.
Course Objectives
1. To write and execute programs in any high level language to solve problems using
data structures such as arrays, linked lists.
2. To write and execute programs in any high level language to solve problems using
data structures such as stacks, queues.
3. To write and execute programs in C++ to solve problems using data structures
such as trees, graphs, hash tables and search trees. To write and execute write
programs in C++ to implement various sorting and searching methods..
Course Outcomes
.
Index
Date of Page Date of Grade & Sign
[Link] Exp. Name of the Experiment No. Submission of the Faculty
Additional remarks
Tutor
1 Title
Program for insertion and deletion in array at different positions.
2 Neatly Drawn and labeled experimental setup
3.1 Algorithm
Insertion at a Specific Position:
1. Shift all elements from the position onwards by one index to the right.
2. Insert the new element at the specified position.
Deletion at a Specific Position:
1. Shift all elements from the position onwards by one index to the left.
2. The element at the specified position is effectively removed, and the size of the array reduces.
3.2 Program
#include <iostream>
using namespace std;
arr[position] = element;
size++;
cout << "Element inserted successfully!" << endl;
}
Page 10
void deleteAtPosition(int arr[], int& size, int position) {
size--;
cout << "Element deleted successfully!" << endl;
}
int main() {
int arr[10];
int size = 3;
arr[0] = 10; arr[1] = 20; arr[2] = 30;
Page 11
return 0;
}
4 Tabulation Sheet
INPUT OUTPUT
5 Results
o Initial array: 10 20 30
o Element inserted successfully!
o 10 15 20 30
o Element inserted successfully!
o 5 10 15 20 30
o Element deleted successfully!
o 5 10 20 30
o Element deleted successfully!
o 10 20 30
Initial array: 10 20 30
Insert 15: 10 15 20 30
Insert 5: 5 10 15 20 30
Delete 15: 5 10 20 30
Delete 5: 10 20 30
Final array: 10 20 30
Page 12
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine Learning)
Group / Title: Implementation of Stack Operations (Push, Pop,
and Top) in C++
Lab: Data Structure (AL303)
Additional remarks
Tutor
Title
Implementation of Stack Operations (Push, Pop, and Top) in C++
Neatly Drawn and labeled experimental setup
Page 13
Theoretical solution of the instant problem
A stack is a linear data structure that follows the LIFO (Last In First Out) principle. It supports the following
operations:
[Link]: Adds an element to the top of the stack.
[Link]: Removes the topmost element from the stack.
[Link]: Retrieves the element at the top of the stack without removing it.
.
Algorithm
[Link] an empty stack.
[Link] Push operation:
•Add the element to the top of the stack.
[Link] Pop operation:
•Remove the element from the top of the stack.
[Link] Top operation:
•Return the element at the top of the stack without removing it.
Program
#include <iostream>
using namespace std;
class Stack {
private:
int arr[100]; // Fixed-size array for simplicity
Page 14
int top;
public:
Stack() {
top = -1; // Initialize stack as empty
}
void pushElement(int x) {
if (top == 99) {
cout << "Stack Overflow!" << endl;
} else {
arr[++top] = x;
}
}
void popElement() {
if (top == -1) {
cout << "Stack is empty!" << endl;
} else {
--top;
}
}
void topElement() {
if (top == -1) {
cout << "Stack is empty!" << endl;
} else {
cout << "Top element: " << arr[top] << endl;
}
}
bool isEmpty() {
return top == -1;
}
};
int main() {
Page 15
Stack stack;
int choice, value;
while (true) {
cout << "1. Push\n2. Pop\n3. Top\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
[Link](value);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
exit(0);
default:
cout << "Invalid choice!" << endl;
}
}
return 0;
}
Tabulation Sheet
Page 16
INPUT OUTPUT
1. Push Top element: 24
2. Pop
3. Top
4. Exit
1. Push
2. Pop
3. Top
4. Exit
1. Push
2. Pop
3. Top
4. Exit
Enter your choice: 3
Results
•Push operation adds elements to the stack successfully.
•Pop operation removes the top element from the stack and displays the new top.
•Top operation correctly displays the top element of the stack without removing it.
•The program handles empty stack situations and invalid inputs.
Page 17
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work
Additional remarks
Tutor
Title
Insertion and Deletion Operations in Queue
Neatly Drawn and labeled experimental setup
Algorithm
•Insertion (Enqueue) Operation:
[Link] if there is space available in the queue.
[Link] space is available, insert the element at the rear end.
Page 18
[Link] the rear pointer.
•Deletion (Dequeue) Operation:
[Link] if the queue is empty.
[Link] not, remove the element from the front of the queue.
[Link] the front pointer.
Program
#include<iostream>
using namespace std;
class Queue {
int front, rear;
int queue[5];
public:
Queue() {
front = -1;
rear = -1;
}
void enqueue(int value) {
if (rear == 4) {
cout << "Queue is full!" << endl;
} else {
if (front == -1) front = 0;
rear++;
queue[rear] = value;
cout << value << " enqueued to queue." << endl;
}
}
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue is empty!" << endl;
} else {
cout << queue[front] << " dequeued from queue." << endl;
front++;
}
}
Page 19
void display() {
if (front == -1 || front > rear) {
cout << "Queue is empty!" << endl;
} else {
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
cout << queue[i] << " ";
}
cout << endl;
}
}
};
int main() {
Queue q;
[Link](100);
[Link](200);
[Link](300);
[Link](400);
[Link](500);
[Link]();
[Link]();
[Link]();
[Link]();
[Link](600);
[Link](700);
[Link]();
return 0;
}
Tabulation Sheet
Page 20
INPUT OUTPUT
100 enqueued to queue.
200 enqueued to queue.
300 enqueued to queue.
400 enqueued to queue.
500 enqueued to queue.
Queue elements: 100 200 300 400
500
100 dequeued from queue.
200 dequeued from queue.
Queue elements: 300 400 500
Queue is full!
Queue is full!
Queue elements: 300 400 500
Results
•The program successfully demonstrates the insertion and deletion operations in a queue.
•Elements are inserted at the rear and removed from the front, following the FIFO principle.
•The queue size is limited to 5 elements, and error handling is done for full and empty conditions.
Additional remarks
Tutor
Title
Tree Creation in C++
Neatly Drawn and labeled experimental setup
Page 22
manner. The insertion method ensures that each value inserted is placed at the correct position in the tree.
Algorithm
[Link] with an empty tree.
[Link] each value into the tree.
[Link] each insertion:
•If the tree is empty, the node becomes the root.
•If the tree is not empty, compare the value with the current node’s value and move left or right
accordingly.
[Link] the tree using different methods:
•Inorder: left, root, right.
•Preorder: root, left, right.
•Postorder: left, right, root.
Program
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int val) {
data = val;
left = right = nullptr;
}
};
class Tree {
public:
Node* root;
Tree() {
root = nullptr;
}
Page 23
Node* insert(Node* node, int data) {
if (node == nullptr) {
return new Node(data);
}
if (data < node->data) {
node->left = insert(node->left, data);
} else {
node->right = insert(node->right, data);
}
return node;
}
void insert(int data) {
root = insert(root, data);
}
void inorder(Node* node) {
if (node == nullptr) {
return;
}
inorder(node->left);
cout << node->data << " ";
inorder(node->right);
}
void inorder() {
inorder(root);
}
void preorder(Node* node) {
if (node == nullptr) {
return;
}
cout << node->data << " ";
preorder(node->left);
preorder(node->right);
}
void preorder() {
preorder(root);
}
void postorder(Node* node) {
Page 24
if (node == nullptr) {
return;
}
postorder(node->left);
postorder(node->right);
cout << node->data << " ";
}
void postorder() {
postorder(root);
}
};
int main() {
Tree tree;
[Link](5);
[Link](3);
[Link](2);
[Link](4);
[Link](7);
[Link](6);
[Link](8);
return 0;
}
Page 25
Tabulation Sheet
INPUT OUTPUT
Inorder traversal: 2 3 4 5 6 7b8
Preorder traversal: 5 3 2 4 7 6 8
Postorder traversal: 2 4 3 6 8 7 5
Results
The tree correctly displays different traversal orders: inorder, preorder, and postorder.
Page 26
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine Learning)
Group / Title: Implementation of Selection Sort in C++
Lab: Data Structure (AL303)
Additional remarks
Tutor
Title
Implementation of Selection Sort in C++
Page 27
Theoretical solution of the instant problem
Selection Sort is a sorting algorithm that divides the array into two parts: sorted and unsorted. It repeatedly selects
the smallest element from the unsorted part and swaps it with the first unsorted element, growing the sorted
portion step by step.
Algorithm
[Link]
[Link] the array size and array elements.
[Link] the outer loop to iterate from the first element to the second last element of the array.
[Link] each iteration, assume the first unsorted element as the minimum.
[Link] the assumed minimum with the rest of the unsorted elements.
[Link] a smaller element is found, update the minimum’s position.
[Link] the minimum element with the first unsorted element.
[Link] steps 4–7 until the array is sorted.
[Link] the sorted array.
[Link]
Program
#include <iostream>
using namespace std;
Page 28
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
}
}
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
selectionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
}Tabulation Sheet
INPUT OUTPUT
Enter number of elements: 4 Sorted array: 14 17 21 45
Enter the elements: 21
45
14
17
Page 29
Results
The program successfully implements the Selection Sort algorithm. It sorts the input array in ascending order as
expected.
Additional remarks
Tutor
Title
Implementation of Insertion Sort in C++
Page 30
Neatly Drawn and labeled experimental setup
Algorithm
[Link] with the second element (index 1) as the key.
[Link] the key with elements to its left and find the correct position.
[Link] all elements greater than the key one position to the right.
[Link] the key at its correct position.
[Link] steps 2–4 for all elements in the array.
Program
#include <iostream>
using namespace std;
Page 31
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
Tabulation Sheet
INPUT OUTPUT
Original Array: 24 71 43 92 57
Sorted Array: 24 43 57 71 92
Results
•The insertion sort algorithm successfully sorts the given input arrays in ascending order.
Page 32
•The algorithm works in O(n²) time complexity in the worst case and O(n) in the best case.
•It is a stable and in-place sorting algorithm
Additional remarks
Page 33
Grade: Cross the grade.
A B C D F
Tutor
Title
Implementation of Quick Sort in C++
Algorithm
[Link] the pivot element.
[Link] elements such that all elements less than the pivot are on the left, and all elements greater
than the pivot are on the right (partition step).
[Link] apply steps 1 and 2 to the left and right subarrays.
[Link] the sorted subarrays with the pivot to produce the sorted array.
Program
Page 34
#include <iostream>
using namespace std;
Page 35
}Tabulation Sheet
INPUT OUTPUT
Sorted array: 11 17 19 45 55 84
Results
The Quick Sort program was successfully implemented and tested. The program sorts an input array of integers in
ascending order using the divide-and-conquer approach.
Page 36
5. Contribution to the team work
Additional remarks
Tutor
Title
Implementation of Merge Sort in C++
Algorithm
[Link] the array has one element, return.
[Link] the array into two halves.
[Link] sort the left half.
[Link] sort the right half.
[Link] the two sorted halves into a single sorted array.
Program
Page 37
#include <iostream>
using namespace std;
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
Page 38
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
Tabulation Sheet
INPUT OUTPUT
Sorted array: 17 18 45 53 76 92
Page 39
Results
The merge sort algorithm successfully sorts an array of integers in ascending order. The program follows the
divide-and-conquer strategy, ensuring efficiency with a time complexity of O(n log n).
Page 40
Grade and Remarks by the Tutor
1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work
Additional remarks
Tutor
Title
Binary Search Implementation in C++
Algorithm
[Link] with a sorted array and a key to find.
[Link] two pointers, low at the first index and high at the last index of the array.
Page 41
[Link] the following steps until low is greater than high:
•Calculate the mid index: mid = (low + high) / 2.
•Compare the key with the middle element of the array.
•If the key is equal to the middle element, the search is successful, and return the index.
•If the key is less than the middle element, adjust high = mid - 1.
•If the key is greater than the middle element, adjust low = mid + 1.
[Link] the key is not found, return -1.
Program
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main() {
int arr[] = {21, 54, 48, 82, 76, 63, 77, 55, 75};
int size = sizeof(arr) / sizeof(arr[0]);
int key;
cout << "Enter the number to search: ";
cin >> key;
int result = binarySearch(arr, size, key);
if (result != -1)
cout << "Element found at index: " << result << endl;
else
cout << "Element not found." << endl;
Page 42
return 0;
}
Tabulation Sheet
INPUT OUTPUT
Enter the number to search: 82 Element found at index: 3
Results
The binary search algorithm is successfully implemented and tested. It efficiently finds the index of a given key in a
sorted array in O(log n) time complexity.
Page 43