DS Lab Manual
DS Lab Manual
COURSE DESCRIPTION: This course provides an overview of data structure concepts, arrays,
stack, queues, trees, and graphs. Discussion of various implementations of these data objects,
programming styles, and run-time representations.
COURSE OUTCOMES: After successful completion of the course, students will be able to:
CO1. Understand the concept of Dynamic memory management, data types, algorithms, Big
O notation.
CO2. Understand basic data structures such as arrays, linked lists, stacks and queues.
CO3. Solve problem involving graphs, trees and heaps.
CO4. Apply Algorithm for solving problems like sorting, searching, insertion and deletion of
data.
Program Specific
Course Program Outcomes
Outcomes
Outcome P
s O PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3 PSO4
1
CO1 3 2 - - - - - - - - - - 3 - - -
CO2 3 2 - - - - - - - - - - 3 - - -
CO3 3 3 3 - - - - - - - - - 3 - - -
CO4 3 3 3 2 - - - - - - - - 3 - - -
Course
Correla
tion 3 3 3 2 - - - - - - - - 3 - - -
Mappin
g
Correlation Levels: 3: High; 2: Medium; 1: Low
COURSE CONTENT
MODULE 1 INTRODUCTION TO DATA STRUCTURES & STACK, QUEUE (10 Periods)
Introduction: Introduction to Data Structures, Basic Concepts and Notations, Abstract Data Types,
Analysis and Efficiency of Algorithms, Time and Space Complexity.
Stack, Queue: Stack, Stack operations, Implementation using arrays, applications of stack, Queue,
Queue operations, Implementation using arrays, various Queue Structures, applications of queue.
Text Books:
1. Mark Allen Weiss, Data Structures and Algorithm Analysis in C, Pearson, Second Edition,
2002.
2. Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein Introduction to
Algorithms, PHI, Third Edition,2010.
3. Narasimha Karumanchi, Data Structures and Algorithms Made Easy, Career Monk
Publications,2020.
Reference Books:
Video Resources:
1. https://www.youtube.com/watch?v=zWg7U0OEAoE&list=PLBF3763AF2E1C572F
2. https://www.youtube.com/watch?v=S47aSEqm_0I&list=PLD9781AC5EBC9FA16
3. https://www.youtube.com/watch?v=9MmC_uGjBsM&list=PLyqSpQzTE6M_Fu6l8irVwXkUyC9Gwqr6
WEB RESOURCES:
1. https://www.tutorialspoint.com/what-is-advanced-java
2. https://www.udemy.com/course/advanced-java-programming/
3. https://www.geeksforgeeks.org/what-is-advanced-java/
4. https://cloudacademy.com/learning-paths/advanced-java-programming-518/
Experien al Learning
DATA STRUCTURES (22CS102003)
WEEK 1
AIM: To demonstrate recursive algorithms with examples.
SOURCE CODE:
a. Factorial:
public class Factorial {
public sta c int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
Node(int data) {
this.data = data;
this.next = null;
}
}
class SingleLinkedList {
Node head;
void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
DoubleLinkedList.java:
class DoubleNode {
int data;
DoubleNode next;
DoubleNode prev;
DoubleNode(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoubleLinkedList {
DoubleNode head;
void printList() {
DoubleNode current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
CircularLinkedList.java:
class CircularNode {
int data;
CircularNode next;
CircularNode(int data) {
this.data = data;
this.next = null;
}
}
class CircularLinkedList {
CircularNode head;
void printList() {
if (head == null) return;
CircularNode current = head;
do {
System.out.print(current.data + " ");
current = current.next;
} while (current != head);
System.out.println();
}
}
LinkedListTest.java:
public class LinkedListTest {
public sta c void main(String[] args) {
// Test Single Linked List
System.out.println("Single Linked List:");
SingleLinkedList sll = new SingleLinkedList();
sll.append(1);
sll.append(2);
sll.append(3);
sll.printList(); // Output: 1 2 3
sll.insertAtHead(0);
sll.printList(); // Output: 0 1 2 3
sll.delete(1);
sll.printList(); // Output: 0 1 2 3
dll.insertAtHead(0);
dll.printList(); // Output: 0 1 2 3
dll.delete(1);
dll.printList(); // Output: 0 1 2 3
cll.insertAtHead(0);
cll.printList(); // Output: 0 1 2 3
cll.delete(1);
cll.printList(); // Output: 0 1 2 3
}
}
Compile the Java files:
javac Node.java SingleLinkedList.java DoubleNode.java DoubleLinkedList.java
CircularNode.java CircularLinkedList.java LinkedListTest.java
Run the LinkedListTest class:
java LinkedListTest
OUTPUT:
Single Linked List:
123
0123
01123
0123
Node(int data) {
this.data = data;
this.next = null;
}
}
// Stack class using Linked List
class Stack {
private Node top;
public Stack() {
top = null;
}
public Queue() {
front = rear = null;
}
return stack.pop();
}
editor.type("Hello");
System.out.println("Current text: " + editor.getText());
editor.type(" World");
System.out.println("Current text: " + editor.getText());
editor.undo();
System.out.println("A er undo: " + editor.getText());
editor.undo();
System.out.println("A er undo: " + editor.getText());
}
}
e. Func on Call Management in Recursion
import java.u l.Stack;
cq.display();
cq.display();
cq.enqueue(60);
cq.enqueue(70);
cq.display();
if (arr[mid] == target) {
return mid; // Target found
}
// Move elements of arr[0..i-1], that are greater than key, to one posi on
ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
private sta c void merge(int[] arr, int le , int mid, int right) {
int n1 = mid - le + 1;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[n2];
System.arraycopy(arr, le , L, 0, n1);
System.arraycopy(arr, mid + 1, R, 0, n2);
int i = 0, j = 0;
int k = le ;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
private sta c int par on(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
return i + 1;
}
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
Arrays.fill(count, 0);
// Copy the output array to arr[], so that arr[] contains sorted numbers
according to current digit
System.arraycopy(output, 0, arr, 0, n);
}
// Do coun ng sort for every digit. The exp is 10^i where i is the current
digit number
for (int exp = 1; max / exp > 0; exp *= 10) {
coun ngSort(arr, exp);
}
}
Sorted array:
11 12 22 25 64
WEEK 9
AIM: To develop a program to represent a Tree data structure.
SOURCE CODE:
// Class represen ng a node in the binary tree
class TreeNode {
int value;
TreeNode le , right;
// Constructor
public TreeNode(int item) {
value = item;
le = right = null;
}
}
// Constructor
public BinaryTree() {
root = null;
}
// Constructor
public TreeNode(int item) {
value = item;
le = right = null;
}
}
// Constructor
public BinarySearchTree() {
root = null;
}
return node;
}
// Node with two children: Get the inorder successor (smallest in the right
subtree)
TreeNode temp = findMin(root.right);
return root;
}
// Inser ng nodes
bst.insert(50);
bst.insert(30);
bst.insert(20);
bst.insert(40);
bst.insert(70);
bst.insert(60);
bst.insert(80);
// Dele ng a node
int deleteValue = 20;
System.out.println("Dele ng value " + deleteValue);
bst.delete(deleteValue);
Dele ng value 20
In-order traversal a er dele on:
30 40 50 60 70 80
WEEK 11
AIM: To demonstrate Graph Traversal Techniques.
SOURCE CODE:
a. Graph Representa on Using Adjacency List
import java.u l.*;
// Constructor
public Graph() {
adjacencyList = new HashMap<>();
}
// BFS traversal
public void bfs(int start) {
Set<Integer> visited = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
visited.add(start);
queue.add(start);
// DFS traversal
public void dfs(int start) {
Set<Integer> visited = new HashSet<>();
System.out.println("DFS traversal star ng from node " + start + ":");
dfsU l(start, visited);
System.out.println();
}
// Constructor
public Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
// Constructor
public UnionFind(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
// Union by rank
public void union(int u, int v) {
int rootU = find(u);
int rootV = find(v);
if (rootU != rootV) {
if (rank[rootU] > rank[rootV]) {
parent[rootV] = rootU;
} else if (rank[rootU] < rank[rootV]) {
parent[rootU] = rootV;
} else {
parent[rootV] = rootU;
rank[rootU]++;
}
}
}
}
if (rootU != rootV) {
uf.union(rootU, rootV);
mst.add(edge);
}
}
// Constructor
public PrimGraph() {
adjacencyList = new HashMap<>();
}
if (!visited.contains(node)) {
visited.add(node);
totalWeight += weight;
if (weight > 0) {
System.out.println("Node " + node + " with weight " + weight);
}
for (int[] neighbor : adjacencyList.getOrDefault(node, new
ArrayList<>())) {
if (!visited.contains(neighbor[0])) {
minHeap.add(neighbor);
}
}
}
}
System.out.println("Total weight of MST: " + totalWeight);
}
while (true) {
System.out.println("Hospital Management System");
System.out.println("1. Add Pa ent");
System.out.println("2. View Pa ents");
System.out.println("3. Add Doctor");
System.out.println("4. View Doctors");
System.out.println("5. Schedule Appointment");
System.out.println("6. View Appointments");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
switch (choice) {
case 1:
System.out.print("Enter pa ent name: ");
String pa entName = scanner.nextLine();
System.out.print("Enter pa ent age: ");
int pa entAge = scanner.nextInt();
scanner.nextLine(); // Consume newline
hospital.addPa ent(new Pa ent(pa entName, pa entAge));
break;
case 2:
hospital.viewPa ents();
break;
case 3:
System.out.print("Enter doctor name: ");
String doctorName = scanner.nextLine();
System.out.print("Enter doctor specializa on: ");
String specializa on = scanner.nextLine();
hospital.addDoctor(new Doctor(doctorName, specializa on));
break;
case 4:
hospital.viewDoctors();
break;
case 5:
System.out.print("Enter pa ent name: ");
String pname = scanner.nextLine();
System.out.print("Enter doctor name: ");
String dname = scanner.nextLine();
System.out.print("Enter appointment date (YYYY-MM-DD): ");
String date = scanner.nextLine();
hospital.scheduleAppointment(pname, dname, date);
break;
case 6:
hospital.viewAppointments();
break;
case 7:
System.out.println("Exi ng...");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
Hospital Class
import java.u l.*;
class Hospital {
private final List<Pa ent> pa ents = new ArrayList<>();
private final List<Doctor> doctors = new ArrayList<>();
private final List<Appointment> appointments = new ArrayList<>();
@Override
public String toString() {
return "Pa ent{name='" + name + "', age=" + age + "}";
}
}
Doctor Class
class Doctor {
private final String name;
private final String specializa on;
@Override
public String toString() {
return "Doctor{name='" + name + "', specializa on='" + specializa on + "'}";
}
}
Appointment Class
class Appointment {
private final Pa ent pa ent;
private final Doctor doctor;
private final String date;