0% found this document useful (0 votes)
13 views28 pages

DSa Com Vivek

The document outlines the laboratory assignment for Data Structures (AL303) at Acropolis Institute of Technology and Research, focusing on practical applications of data structures. It includes objectives, syllabus, general instructions, and programming exercises related to various data structures such as arrays, linked lists, stacks, queues, trees, and graphs. Additionally, it specifies hardware and software requirements, course objectives, outcomes, and evaluation criteria for students.

Uploaded by

davidwhite25493
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)
13 views28 pages

DSa Com Vivek

The document outlines the laboratory assignment for Data Structures (AL303) at Acropolis Institute of Technology and Research, focusing on practical applications of data structures. It includes objectives, syllabus, general instructions, and programming exercises related to various data structures such as arrays, linked lists, stacks, queues, trees, and graphs. Additionally, it specifies hardware and software requirements, course objectives, outcomes, and evaluation criteria for students.

Uploaded by

davidwhite25493
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/ 28

2024-25

Acropolis Institute of
Technology and
Research, Indore
Department of CSE
Submitted To: Dr. Mayur Rathi
(Artificial Intelligence & Machine
Learning)

Data Structures (AL303)

Submitted By:
Name of Student:Vivek Devda
Enrollment No. : 0827AL231149
Class/Year/Sem : ALS-1/2nd / 3rd

[LAB ASSIGNMENT DATA STRUCTURES (AL-303)]

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.

ACROPOLIS INSTITUTE OF TECHNOLOGY & RESEARCH,


INDORE

Department of CSE (Artificial Intelligence G Machine Learning)

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. Aditya Jadhav B.TECH II year III semester in the Data Structures

Laboratory of this institute during the academic year 2024- 2025.

Signature of the Faculty


About the Laboratory

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

✔ Without Prior permission do not enter into the Laboratory.

✔ While entering into the LAB students should wear their ID cards.

✔ The Students should come with proper uniform.

✔ Students should sign in the LOGIN REGISTER before entering into the
laboratory.

✔ Students should come with observation and record note book to the laboratory.

✔ Students should maintain silence inside the laboratory.

✔ After completing the laboratory exercise, make sure to shutdown the system
properly.

⮚ DONT’S

✔ Students bringing the bags inside the laboratory.

✔ Students using the computers in an improper way.

✔ Students scribbling on the desk and mishandling the chairs.

✔ Students using mobile phones inside the laboratory.

✔ Students making noise inside the laboratory.


SYLLABUS
Course: AL303 (Data Structures)
Branch/Year/Sem: Artificial Intelligence & Machine Learning / II / III

Module1: Introduction to Data Structure: Concepts of Data and Information,


Classification of Data structures, Abstract Data Types, Implementation aspects: Memory
representation. Data structures operations and its cost estimation. Introduction to linear
data structures- Arrays, Linked List: Representation of linked list in memory, different
implementation of linked list. Circular linked list, doubly linked list, etc. Application of
linked list: polynomial manipulation using linked list, etc..

Module2: Stacks and Queue: Stacks as ADT, Different implementation of stack,


multiple stacks. Application of Stack: Conversion of infix to postfix notation using
stack, evaluation of postfix expression, Recursion. Queues: Queues as ADT, Different
implementation of queue, Circular queue, Concept of Dqueue and Priority Queue, Queue
simulation, Application of queues.

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.

Module4: Graphs: Introduction, Classification of graph: Directed and Undirected


graphs, etc, Representation, Graph Traversal: Depth First Search (DFS), Breadth First
Search (BFS), Graph algorithm: Minimum Spanning Tree (MST)-Kruskal, Prim’s
algorithms. Dijkstra’s shortest path algorithm; Comparison between different graph
algorithms. Application of graphs.

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:

S. Name of Item Specification


No.
1 Computer System Hard Disk min 5 GB
RAM: 4 GB / 8 GB
Processor: Intel i3 or above

S. Name of Item Specification


No.
1 Operating system Window XP or 2000
Editor Python3.7 IDLE or Google Colab or
Spyder(Anaconda) , Turbo C/C++
RATIONALE:
The purpose of this subject is to cover the concepts of Data Structure in terms of Theory
and Implementation .The syllabus provides all the essential concepts of Data Structures.

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 AND OUTCOMES

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

At the end of the course student will be able to:

1. List different types of data structures


2. Illustrate the concepts of stack and queue
3. Describe the concepts of various tree
4. Discuss the concepts of Graph
5. Demonstrate various algorithms of sorting

.
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)

2 Program to perform Insertion and deletion operation


in linked list. (CO 1)

3 Program to perform Push, Pop & top operations in


stack. (CO 2)

4 Program to perform Insertion and deletion operation


in queue. (CO 2)

5 Program to create a tree. (CO 3)

6 Program to implement Selection Sort. (CO 5)

7 Program to implement Insertion Sort. (CO 5)

8 Program to implement Quick Sort. (CO 5)

9 Program to implement Merge Sort. (CO 5)

10 Program to implement Binary Search. (CO 5)


Program Outcome (PO)

The engineering graduate of this institute will demonstrate:


a) Apply knowledge of mathematics, science, computing and engineering fundamentals to computer
science engineering problems.
b) Able to identify, formulate, and demonstrate with excellent programming, and problem solving skills.
c) Design solutions for engineering problems including design of experiment and processes to meet
desired needs within reasonable constraints of manufacturability, sustainability, ecological,
intellectual and health and safety considerations.
d) Propose and develop effective investigational solution of complex problems using research
methodology; including design of experiment, analysis and interpretation of data, and combination of
information to provide suitable conclusion. synthesis
e) Ability to create, select and use the modern techniques and various tools to solve engineering
problems and to evaluate solutions with an understanding of the limitations.
f) Ability to acquire knowledge of contemporary issues to assess societal, health and safety, legal and
cultural issues.
g) Ability to evaluate the impact of engineering solutions on individual as well as organization in a
societal and environmental context, and recognize sustainable development, and will be aware of
emerging technologies and current professional issues.
h) Capability to possess leadership and managerial skills, and understand and commit to professional
ethics and responsibilities.
i) Ability to demonstrate the team work and function effectively as an individual, with an ability to
design, develop, test and debug the project, and will be able to work with a multi-disciplinary team.
j) Ability to communicate effectively on engineering problems with the community, such as being able
to write effective reports and design documentation.
k) Flexibility to feel the recognition of the need for, and have the ability to engage in independent and
life- long learning by professional development and quality enhancement programs in context of
technological change.
l) A practice of engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and entrepreneurship.
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine Learning)
Group / Title:
Lab: Data Structure (AL303)

EVALUATION RECORD Type/ Lab Session:


Name Vivek Devda Enrollment No. 0827AL231149
Performing on First submission Second submission
Extra Regular

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

Grade: Cross the grade.


A B C D F

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 printArray(const std::vector<int>& arr) {


std::cout << "Array: ";
for (int val : arr) std::cout << val << " ";
std::cout << "\n";
}

void arrayOps() {
std::vector<int> arr = {10, 20, 40, 50};
printArray(arr);

// Insert at various positions


auto insert = [&](int pos, int val) {
if (pos < 0 || pos > arr.size()) {
std::cerr << "Invalid insert position.\n";
return;
}
arr.insert(arr.begin() + pos, val);
std::cout << "Inserted " << val << " at " << pos << ": ";
printArray(arr);
};

// Delete at various positions


auto remove = [&](int pos) {
if (pos < 0 || pos >= arr.size()) {
std::cerr << "Invalid delete position.\n";
return;
}
arr.erase(arr.begin() + pos);
std::cout << "Removed at " << pos << ": ";
printArray(arr);
};

insert(2, 30); // Insert in the middle


insert(0, 5); // Insert at the beginning
insert(arr.size(), 60); // Insert at the end

remove(1); // Remove from the middle


remove(0); // Remove from the beginning

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 printList(Node* head) {


std::cout << "List: ";
for (Node* temp = head; temp; temp = temp->next)
std::cout << temp->data << " ";
std::cout << "\n";
}

void listOps() {
Node* head = nullptr;

auto insertAt = [&](int pos, int val) {


if (pos < 0) return std::cerr << "Invalid pos.\n", void();
Node* newNode = new Node(val);
if (pos == 0) {
newNode->next = head;
head = newNode;
return;
}
Node* temp = head;
for (int i = 0; i < pos - 1 && temp; ++i, temp = temp->next);
if (!temp) return std::cerr << "Pos out of range.\n", delete newNode, void();
newNode->next = temp->next;
temp->next = newNode;
};

auto deleteAt = [&](int pos) {


if (!head) return std::cerr << "List empty.\n", void();
if (pos < 0) return std::cerr << "Invalid pos.\n", void();

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);

while (head) { // Memory cleanup


Node* temp = head;
head = head->next;
delete temp;
}
}

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;

auto push = [&](int val) {


stack.push_back(val);
std::cout << "Pushed " << val << "\n";
};

auto pop = [&]() {


if (stack.empty()) {
std::cerr << "Stack is empty. Cannot pop.\n";
return;
}
int topVal = stack.back();
stack.pop_back();
std::cout << "Popped " << topVal << "\n";
};

auto top = [&]() {


if (stack.empty()) {
std::cerr << "Stack is empty. No top element.\n";
return;
}
std::cout << "Top: " << stack.back() << "\n";
};
push(10);
push(20);
top();
pop();
top();
pop();

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;

auto enqueue = [&](int val) {


queue.push_back(val);
std::cout << "Enqueued: " << val << "\n";
};

auto dequeue = [&]() {


if (queue.empty()) {
std::cerr << "Queue is empty. Cannot dequeue.\n";
return;
}
int frontVal = queue.front();
queue.pop_front();
std::cout << "Dequeued: " << frontVal << "\n";
};

auto front = [&]() {


if (queue.empty()) {
std::cerr << "Queue is empty. No front element.\n";
return;
}
std::cout << "Front: " << queue.front() << "\n";
};

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) {}
};

// Function to create a simple tree (for demonstration)


Node* createSimpleTree() {
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);
return root;
}

// Function for level-order (breadth-first) traversal (to visualize the tree)


void levelOrder(Node* root) {
if (!root) return;
std::queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* curr = q.front();
q.pop();
std::cout << curr->data << " ";
if (curr->left) q.push(curr->left);
if (curr->right) q.push(curr->right);
}
std::cout << "\n";
}

// Function to delete the entire tree (important for memory management)

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);

deleteTree(root); // Crucial: prevent memory leaks


return 0;
}

Page 19
6. Program to implement Selection Sort.

#include <iostream>
#include <vector>
#include <algorithm> // for std::swap (or std::iter_swap)

void selectionSort(std::vector<int>& arr) {


int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
int min_idx = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
std::swap(arr[min_idx], arr[i]); // More efficient than manual 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;
}

void quickSort(std::vector<int>& arr, int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}

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";

std::vector<int> arr2 = {10, 7, 8, 9, 1, 5};


quickSort(arr2, 0, arr2.size() - 1);
std::cout << "Quick Sorted: ";
for (int val : arr2) 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)

using namespace std;

// Partition function (Hoare's partition scheme - often more efficient)


int partition(vector<int>& arr, int low, int high) {
int pivot = arr[low]; // Choosing the first element as pivot
int i = low - 1;
int j = high + 1;

while (true) {
do {
i++;
} while (arr[i] < pivot);

do {
j--;
} while (arr[j] > pivot);

if (i >= j) {
return j;
}

swap(arr[i], arr[j]); // Or std::swap(arr[i], arr[j]);


}
}

// Quick Sort function


void quickSort(vector<int>& arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi); // Before pi


quickSort(arr, pi + 1, high); // After pi

Page 23
}
}

int main() {
vector<int> data = {10, 7, 8, 9, 1, 5};
int n = data.size();

cout << "Unsorted array: ";


for (int x : data) cout << x << " ";
cout << endl;

quickSort(data, 0, n - 1);

cout << "Sorted array: ";


for (int x : data) cout << x << " ";
cout << endl;

data = {1, 7, 4, 1, 10, 9, -2};


n = data.size();

cout << "Unsorted array: ";


for (int x : data) cout << x << " ";
cout << endl;

quickSort(data, 0, n - 1);

cout << "Sorted array: ";


for (int x : data) cout << x << " ";
cout << endl;
return 0;
}
Output:
Unsorted array: 10 7 8 9 1 5 Sorted array: 1 5 7 8 9 10 Unsorted array: 1 7 4 1 10 9 -2
Sorted array: -2 1 1 4 7 9 10

Page 24
9. Program to implement Merge Sort.
#include <iostream>
#include <vector>

void merge(std::vector<int>& arr, int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;
std::vector<int> L(n1), R(n2);
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

void mergeSort(std::vector<int>& arr, int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

int main() {
std::vector<int> arr = {38, 27, 43, 3, 9, 82, 10};

std::cout << "Unsorted array: ";


for (int x : arr) std::cout << x << " ";
std::cout << "\n";

mergeSort(arr, 0, arr.size() - 1);

std::cout << "Sorted array: ";


for (int x : arr) std::cout << x << " ";

Page 25
std::cout << "\n";

arr = {12, 11, 13, 5, 6, 7}; // Another test case


std::cout << "Unsorted array: ";
for (int x : arr) std::cout << x << " ";
std::cout << "\n";
mergeSort(arr, 0, arr.size() - 1);
std::cout << "Sorted array: ";
for (int x : arr) std::cout << x << " ";
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 binarySearch(const std::vector<int>& arr, int target) {


int low = 0, high = arr.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2; // Avoid potential overflow
if (arr[mid] == target) return mid;
(arr[mid] < target) ? low = mid + 1 : high = mid - 1; // Concise conditional
}
return -1; // Not found
}

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

int result1 = binarySearch(arr, target1);


int result2 = binarySearch(arr, target2);
int result3 = binarySearch(arr, target3);
int result4 = binarySearch(arr, target4);

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy