Dsa Lab Report Jay.....
Dsa Lab Report Jay.....
Lab Report of
Data Structure and Algorithms (CACS201)
I would like to express my sincere gratitude to my respected teacher Mr. Pratik Chand, for
his continuous guidance, encouragement, and support throughout the course of this lab work
on Data Structures and Algorithms (DSA). His expert teaching and insightful feedback have
helped me develop a clear understanding of various data structures and their practical
applications.
I am also thankful to the college management team BCA Faculty and Program Co-ordinator,
Mr Koshraj Regmi for providing a well- structured academic environment and the
opportunity to gain hands-on experience through this lab. Their efforts in organizing and
facilitating the lab sessions are truly appreciated. Lastly, I would like to extend my heartfelt
thanks to all my classmates, friends, and anyone who contributed, directly or indirectly, in
helping me complete this project successfully and on time.
Yours sincerely,
Jay Budha
TABLE OF CONTENT
lab4 WAP to create double ended queue and perform following operations
a. Enqueue Operation
b. Dequeue Operation
c. Display Operation
lab5 WAP to create singly linked list and perform following operations
a. Insert at first
b. Insert at last
c. Insert before specific position
d. Insert after specific position
e. Insert at specific position
f. Delete at first
g. Delete at last
h. Delete at specific position
i. Delete the node having specific value
j. Display the data of singly linked list
lab6 WAP to create doubly linked list and perform following operations
a. Insert at first
b. Insert at last
c. Insert before specific position
d. Insert after specific position
e. Insert at specific position
f. Delete at first
g. Delete at last
h. Delete at specific position
i. Delete the node having specific value
j. Display the data of singly linked list
lab7 WAP to create circular singly linked list and perform following operations
a. Insert at first
b. Insert at last
c. Insert before specific position
d. Insert after specific position
e. Insert at specific position
f. Delete at first
g. Delete at last
h. Delete at specific position
i. Delete the node having specific value
j. Display the data of singly linked list
lab8 Lab 8: WAP shift the 3 deices from 1st tower to 3rd tower in Tower of Hanoi using
recursion.
lab9 WAP to find GCD of any two numbers using recursion.
lab19 WAP to create binary search tree and perform following operations
a. Insertion Operation
b. Deletion Operation
c. Search data from BST
d. In-order traversal
e. Pre-order traversal
f. Post-order traversal
Output:
Stack is created.
Data 10 is pushed into the stack.
Data 20 is pushed into the stack.
Data 30 is pushed into the stack.
Data 20 is pushed into the stack.
Data 30 is pushed into the stack.
Error: Stack overflow.
Data popped from stack is 30
Data popped from stack is 20
Data popped from stack is 30
Data remaining in stack: 20 10
Lab 2: WAP to create linear queue and perform following operations:
a. Enqueue Operation
b. Dequeue Operation
c. Display Operation
Output:
Queue is empty
Data 10 is inserted in linear queue
Data 20 is inserted in linear queue
Data 30 is inserted in linear queue
Data 10 is inserted in linear queue
Data 20 is inserted in linear queue
Queue is overflow
Data 10 is deleted from queue
Data 20 is deleted from queue
Data 30 is deleted from queue
Data 10 is deleted from queue
Data in queue: 20
Data 50 is inserted in linear queue
Lab 3: WAP to create circular queue and perform following operations
a. Enqueue Operation
b. Dequeue Operation
c. Display Operation
Output:
Data 10 is inserted at index 0
Data 20 is inserted at index 1
Data 30 is inserted at index 2
Data 40 is inserted at index 3
Queue is full
Data in queue:
10 20 30 40
Data 10 is deleted from index 0
Data 20 is deleted from index 1
Data in queue:
30 40
Front is at index 2 (Data: 30)
Rear is at index 3 (Data: 40)
Lab 4: WAP to create double ended queue and perform operations
a. Insert at front
b. Insert at rear
c. Delete from front
d. Delete from rear
e. Display
Output:
After inserting 10,20,30 at front: SLL: 10 20 30
After inserting 40,50 at end: SLL: 10 20 30 40 50
After deleting first: SLL: 20 30 40 50
After deleting last: SLL: 20 30 40
Lab 6: WAP to create doubly linked list and perform operations
a. Insert at first
b. Insert at last
c. Insert before position
d. Insert after position
e. Delete first
f. Delete last
g. Delete at position
h. Delete by value
i. Display (forward & backward)
Output:
Output:
GCD of 48 and 18 is: 6
Lab 10: WAP to implement the bubble sort algorithm.
bubbleSort(data);
System.out.println("Sorted array: " + java.util.Arrays.toString(data));
}
}
Output:
Original array: [64, 34, 25, 12, 22, 11, 90]
Sorted array: [11, 12, 22, 25, 34, 64, 90]
Lab 11: WAP to implement the insertion sort algorithm.
insertionSort(data);
System.out.println("Sorted array: " + Arrays.toString(data));
}
}
Output:
Original array: [12, 11, 13, 5, 6]
Sorted array: [5, 6, 11, 12, 13]
Lab 12: WAP to implement the merge sort algorithm.
Output:
Original array: [38, 27, 43, 3, 9, 82, 10]
Sorted array: [3, 9, 10, 27, 38, 43, 82]
Lab 13: WAP to implement the quick sort algorithm.
Output:
Original array: [10, 7, 8, 9, 1, 5]
Sorted array: [1, 5, 7, 8, 9, 10]
Lab 14: WAP to implement the selection sort algorithm.
Output:
Sorted array:
10 13 14 29 37
Lab 15: WAP to implement the shell sort algorithm.
Output:
Sorted array:
11 12 22 25 34 64 90
Lab 16: WAP to implement the heap sort algorithm.
Output:
Sorted array:
5 6 7 11 12 13
Lab 17: WAP to implement sequential searching algorithm.
Output:
Element found at index: 2
Lab 18: WAP to implement binary searching algorithm.
import java.util.Arrays;
Output:
Element found at index: 3
Lab 19: WAP to create binary search tree and perform following operations
a. Insertion Operation
b. Deletion Operation
c. Search data from BST
d. In-order traversal
e. Pre-order traversal
f. Post-order traversal
class Node {
int key;
Node left, right;
Node(int item) {
key = item;
left = right = null;
}
}
class BST {
Node root;
BST() {
root = null;
}
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {
if (root == null) return new Node(key);
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
return root;
}
void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.key + " ");
inorder(root.right);
}
}
void preorder(Node root) {
if (root != null) {
System.out.print(root.key + " ");
preorder(root.left);
preorder(root.right);
}
}
void postorder(Node root) {
if (root != null) {
postorder(root.left);
postorder(root.right);
System.out.print(root.key + " ");
}
}
Node search(Node root, int key) {
if (root == null || root.key == key)
return root;
if (key < root.key)
return search(root.left, key);
return search(root.right, key);
}
Node delete(Node root, int key) {
if (root == null) return root;
if (key < root.key)
root.left = delete(root.left, key);
else if (key > root.key)
root.right = delete(root.right, key);
else {
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
root.key = minValue(root.right);
root.right = delete(root.right, root.key);
}
return root;
}
int minValue(Node root) {
int minVal = root.key;
while (root.left != null) {
minVal = root.left.key;
root = root.left;
}
return minVal;
}
}
public class BSTOperations {
public static void main(String[] args) {
BST tree = new BST();
int[] keys = {50, 30, 70, 20, 40, 60, 80};
for (int key : keys)
tree.insert(key);
System.out.print("Inorder traversal: ");
tree.inorder(tree.root);
System.out.println("\nPreorder traversal: ");
tree.preorder(tree.root);
System.out.println("\nPostorder traversal: ");
tree.postorder(tree.root);
int searchKey = 40;
System.out.println("\n\nSearching for " + searchKey + ":");
System.out.println(tree.search(tree.root, searchKey) != null ? "Found" : "Not
Found");
System.out.println("\nDeleting 70...");
tree.root = tree.delete(tree.root, 70);
System.out.print("Inorder after deletion: ");
tree.inorder(tree.root);
}
}
Output:
Inorder traversal: 20 30 40 50 60 70 80
Preorder traversal:
50 30 20 40 70 60 80
Postorder traversal:
20 40 30 60 80 70 50
Searching for 40:
Found
Deleting 70...
Inorder after deletion: 20 30 40 50 60 80
Lab 20: WAP to implement Dijkstra’s Shortest Path Algorithm.
return minIndex;
}
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, visited);
visited[u] = true;
for (int v = 0; v < V; v++)
if (!visited[v] && graph[u][v] != 0 &&
dist[u] != Integer.MAX_VALUE &&
dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
Output:
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 16