DSa Com Vivek
DSa Com Vivek
Acropolis Institute of
Technology and
Research, Indore
Department of CSE
Submitted To: Dr. Mayur Rathi
(Artificial Intelligence & Machine
Learning)
Submitted By:
Name of Student:Vivek Devda
Enrollment No. : 0827AL231149
Class/Year/Sem : ALS-1/2nd / 3rd
The Objective of this laboratory work is to strengthen the ability of the students to identify and apply the suitable data structure for
the given real world problem. It enables them to gain knowledge in practical applications of data structures.
CERTIFICATE
This is to certify that the experimental work entered in this journal as per
Mr. Aditya Jadhav B.TECH II year III semester in the Data Structures
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 &
S.No Exp. Name of the Experiment No. Submission Sign of the
Faculty
1 Program for insertion and deletion in array at
different positions. (CO 1)
Additional remarks
Tutor
1 Title
2 Neatly Drawn and labeled experimental setup
3 Theoretical solution of the instant problem
3.1 Algorithm
3.2 Program
4 Tabulation Sheet
INPUT OUTPUT
5
Results
1. Program for insertion and deletion in array at different position
#include <iostream>
#include <vector>
void arrayOps() {
std::vector<int> arr = {10, 20, 40, 50};
printArray(arr);
Page 10
remove(arr.size()-1); //Remove from the end
int main() {
arrayOps();
return 0;
}
Output:
Array: 10 20 40 50 Inserted 30 at 2: Array: 10 20 30 40 50 Inserted 5 at 0: Array: 5 10 20
30 40 50 Inserted 60 at 6: Array: 5 10 20 30 40 50 60 Removed at 1: Array: 5 20 30 40 50 60
Removed at 0: Array: 20 30 40 50 60 Removed at 4: Array: 20 30 40 50
Page 11
2. Program to perform Insertion and deletion operation in linked list.
#include <iostream>
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void listOps() {
Node* head = nullptr;
Page 12
if (pos == 0) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* temp = head;
for (int i = 0; i < pos - 1 && temp && temp->next; ++i, temp = temp->next);
if (!temp || !temp->next) return std::cerr << "Pos out of range.\n", void();
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
};
insertAt(0, 10);
insertAt(1, 30);
insertAt(1, 20);
insertAt(0, 5);
insertAt(4, 40);
printList(head);
deleteAt(2);
deleteAt(0);
deleteAt(2);
printList(head);
int main() {
listOps();
return 0;
}
Output:
List: 5 10 20 30 40
List: 10 30
Page 13
3. Program to perform Push, Pop & top operations in stack.
#include <iostream>
#include <vector>
void stackOps() {
std::vector<int> stack;
Page 14
pop();//trying to pop from empty stack
}
int main() {
stackOps();
return 0;
}
Output:
Pushed 10 Pushed 20 Top: 20 Popped 20 Top: 10 Popped 10 Stack is empty. Cannot pop.
Page 15
4. Program to perform Insertion and deletion operation in queue.
#include <iostream>
#include <deque> // Use std::deque for efficient front/back operations
void queueOps() {
std::deque<int> queue;
enqueue(10);
enqueue(20);
enqueue(30);
front();
dequeue();
front();
dequeue();
dequeue();
dequeue();//trying to dequeue from empty queue
Page 16
int main() {
queueOps();
return 0;
}
Output:
Enqueued: 10 Enqueued: 20 Enqueued: 30 Front: 10 Dequeued: 10 Front: 20 Dequeued: 20
Dequeued: 30 Queue is empty. Cannot dequeue.
Page 17
5. Program to create a tree.
#include <iostream>
#include <vector>
#include <queue>
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
Page 18
void deleteTree(Node* root) {
if (!root) return;
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
int main() {
Node* root = createSimpleTree();
std::cout << "Level-order traversal: ";
levelOrder(root);
Page 19
6. Program to implement Selection Sort.
#include <iostream>
#include <vector>
#include <algorithm> // for std::swap (or std::iter_swap)
int main() {
std::vector<int> arr = {64, 25, 12, 22, 11};
selectionSort(arr);
std::cout << "Sorted array: ";
for (int val : arr) {
std::cout << val << " ";
}
std::cout << "\n";
return 0;
}
Output:
Sorted array: 11 12 22 25 64
Page 20
7. Program to implement Insertion Sort. 8.Program to implement Quick Sort
#include <iostream>
#include <vector>
#include <algorithm>
// 7. Insertion Sort
void insertionSort(std::vector<int>& arr) {
for (size_t i = 1; i < arr.size(); ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}
// 8. Quick Sort
int partition(std::vector<int>& arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; ++j) {
if (arr[j] < pivot) {
std::swap(arr[++i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
return i + 1;
}
Page 21
}
int main() {
std::vector<int> arr1 = {12, 11, 13, 5, 6};
insertionSort(arr1);
std::cout << "Insertion Sorted: ";
for (int val : arr1) std::cout << val << " ";
std::cout << "\n";
return 0;
}
Page 22
8. Program to implement Quick Sort
#include <iostream>
#include <vector>
#include <algorithm> // for swap (optional, can use std::swap)
while (true) {
do {
i++;
} while (arr[i] < pivot);
do {
j--;
} while (arr[j] > pivot);
if (i >= j) {
return j;
}
Page 23
}
}
int main() {
vector<int> data = {10, 7, 8, 9, 1, 5};
int n = data.size();
quickSort(data, 0, n - 1);
quickSort(data, 0, n - 1);
Page 24
9. Program to implement Merge Sort.
#include <iostream>
#include <vector>
int main() {
std::vector<int> arr = {38, 27, 43, 3, 9, 82, 10};
Page 25
std::cout << "\n";
return 0;
}
Output:
Unsorted array: 38 27 43 3 9 82 10 Sorted array: 3 9 10 27 38 43 82 Unsorted array: 12 11
13 5 6 7 Sorted array: 5 6 7 11 12 13
Page 26
10. program to implement Binary Search.
#include <iostream>
#include <vector>
int main() {
std::vector<int> arr = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int target1 = 23;
int target2 = 30; // Not in the array
int target3 = 2; // First element
int target4 = 91;//Last element
if (result1 != -1)
std::cout << target1 << " found at index: " << result1 << "\n";
else
std::cout << target1 << " not found.\n";
if (result2 != -1)
std::cout << target2 << " found at index: " << result2 << "\n";
else
std::cout << target2 << " not found.\n";
if (result3 != -1)
std::cout << target3 << " found at index: " << result3 << "\n";
else
std::cout << target3 << " not found.\n";
if (result4 != -1)
std::cout << target4 << " found at index: " << result4 << "\n";
else
Page 27
std::cout << target4 << " not found.\n";
return 0;
}
Output:
23 found at index: 5
30 not found.
2 found at index: 0
91 found at index: 9
Page 28