Lab Manual Data Structures Using JAVA-R1UC303B
Lab Manual Data Structures Using JAVA-R1UC303B
Lab Manual Data Structures Using JAVA-R1UC303B
2024-2025
SUBJECT Data Structure using Java PROGRAMME B.Tech CSE and
Specialization III Sem
DURATION OF
CREDITS 1 15 Weeks
SEMESTER
PREREQUISITE SESSION
C/C++/Java 2 Hrs per Week
SUBJECTS DURATION
Vision
"To be recognized globally as a premier School of Computing Science and Engineering for imparting
quality and value-based education within a multi-disciplinary and collaborative research-based
environment."
Mission
The mission of the school is to:
M1: Develop a strong foundation in fundamentals of computing science and engineering with
responsiveness towards emerging technologies.
M2: Establish state-of-the-art facilities and adopt education 4.0 practices to analyze, develop, test and
deploy sustainable ethical IT solutions by involving multiple stakeholders.
M3: Foster multidisciplinary collaborative research in association with academia and industry through
focused research groups, Centre of Excellence, and Industry Oriented R&D Labs.
PROGRAM EDUCATIONAL OBJECTIVES
The Graduates of Computer Science and Engineering shall:
PEO1: Graduates will possess the knowledge and skills to analyze, design, and implement efficient data
structures and algorithms using Java, preparing them for advanced studies or professional careers in
software development.
PEO2: Graduates will be proficient in identifying problems, formulating solutions, and implementing them
using data structures in Java, enabling them to address real-world challenges in diverse domains.
PEO3: Graduates will demonstrate the capability to stay current with technological advancements, adapt to new
tools and programming languages, and continuously improve their skills to remain competitive in the dynamic
field of software engineering.
PSO1: Have the ability to work with emerging technologies in computing requisite to Industry 4.0.
PSO2: Demonstrate Engineering Practice learned through industry internship and research project
to solve live problems in various domains.
PROGRAMME OUTCOME (PO):
PO1 Computing Science knowledge: Apply the knowledge of mathematics, statistics, computing
science and information science fundamentals to the solution of complex computer application
problems.
PO2 Problem analysis: Identify, formulate, review research literature, and analyze complex
computing science problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and computer sciences.
PO3 Design/development of solutions: Design solutions for complex computing problems and design
system components or processes that meet the specified needs with appropriate consideration for the public
health and safety, and the cultural, societal, and environmental considerations.
PO4 Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
PO5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
computing science and IT tools including prediction and modeling to complex computing activities
with an understanding of the limitations.
PO6 IT specialist and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional computing science and information science practice.
PO7 Environment and sustainability: Understand the impact of the professional computing science
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
PO8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the computing science practice.
PO9 Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10 Communication: Communicate effectively on complex engineering activities with the IT
analyst community and with society at large, such as, being able to comprehend and write effective
reports and design documentation, make effective presentations, and give and receive clear
instructions.
PO11 Project management and finance: Demonstrate knowledge and understanding of the
computing science and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO12 Life-long learning: Recognize the need for, and have the preparation and ability to engage in independent
and life-long learning in the broadest context of technological change.
List of Programs
Programs
1 Write a Program to implement addition and multiplication of two 2D arrays
2 Write a Program to implement Singly Linked List
3 Write a Program to implement stack using array.
4 Write a program to implement queue using array.
5 Write a program to implement circular queue using array.
6 Write a program to implement stack using linked list.
7 Write a program to implement queue using linked list.
8 Write a program to implement circular queue using linked list.
9 Write a program to implement binary search tree using linked list.
10 Write a program to implement Linear search and binary Search.
11 Write a program to implement Insertion Sorting.
12 Write a program to implement Bubble Sorting.
13 Write a program to convert Infix to Prefix and postfix representation of an
expression.
14 Write a program to implement BFS using array.
15 Write a program to implement DFS using linked list.
Course Outcomes
CO1 Develop programs for functions like traversal, searching, and sorting over Arrays
and Linked Lists using iterative and recursive techniques.
CO2 Convert iterative programs using Arrays and Linked Lists into equivalent Recursive
versions and vice versa.
CO3 Implement Stack, Queue, and Binary Tree data structures in Java using Array and
Linked Structures.
CO4 Develop Java programs to solve computing problems using multiple data structures
(Stack, Queue, and Binary Tree).
CO-PO-PSO MAPPING:
CO/PO
Mapping
CO1 1
1 1
CO2 1
CO3 1
CO4 2 2 2 2 1 1
1 Class Performance 5
2 Attendance 5
3 File 10
4 Viva 5
Total 25
Experiment 1
Objectives:
To understand and implement the addition and multiplication of two 2D arrays using Java.
Description: Here A is a two–dimensional array with M rows and N columns and B is a two–
dimensional array with X rows and Y columns. This algorithm adds these two arrays.
1. If (M≠ X) or (N≠ Y) Then
2. Print: Addition is not possible.
3. Exit [End of If]
4. Repeat For I = 1 to M
5. Repeat For J = 1 to N
6. Set C[I][J] = A[I][J] + B[I][J] [End of Step 5 For Loop] [End of Step 6 For Loop]
7. Exit
Explanation: First, we have to check whether the rows of array A are equal to the rows of array
B or the columns of array A are equal to the columns of array B. if they are not equal, then
addition is not possible and the algorithm exits. But if they are equal, then first for loop iterates
to the total number of rows i.e. M and the second for loop iterates to the total number of columns
i.e. N. In step 6, the element A[I][J] is added to the element B[I][J] and is stored in C[I][J] by
the statement: C[I][J] = A[I][J] + B[I][J].
Description: Here A is a two– dimensional array with M rows and N columns and B is a
two –dimensional array with X rows and Y columns. This algorithm multiplies these two
arrays.
Explanation: First we check whether the rows of A are equal to columns of B or the columnsof
A are equal to rows of B. If they are not equal, then multiplication is not possible. But, ifthey are
equal, the first for loop iterates to total number of columns of A i.e. N and the secondfor loop
iterates to the total number of rows of B i.e. X. In step 6, all the elements of C are setto zero.
Then the third for loop iterates to total number of columns of B i.e. Y. In step 8, theelement
A[I][K] is multiplied with B[K][J] and added to C[I][J] and the result is assigned toC[I][J] by
the statement: C[I][J] = C[I][J] + A[I][K] * B[K][J].
Post-Experiment Questions:
Q 4. How to modify the program to print Lower triangular and Upper triangular matrix of an
array?
Experiment 2
Objective:
To understand and implement a singly linked list in Java and perform various operations
such as insertion, deletion, and traversal.
Algorithm:
Initialization:
Define Node Class:
• Each node should have two attributes: data (to store the value) and
next (to store the reference to the next node).
Create Linked List Class:
• This class should have a head attribute that points to the first node
of the linked list.
• Insertion Operations:
Deletion Operations:
Delete from the Beginning:
Step 1: Check if the linked list is empty.
Step 2: Update the head to the next node of the current head.
Traversal Operation:
Traverse the Linked List:
Step 1: Start from the head.
Step 2: Print the data of each node.
Step 3: Move to the next node using the next reference.
Step 4: Repeat steps 2 and 3 until the end of the linked list is reached.
Post-Experiment Questions:
1. Insert elements 5, 10, 15, 20, 25 into the linked list in the given order and then
print the linked list.
a. Write a function to search for an element in the linked list. If the element
is found, print its position; otherwise, print "Element not found."
b. Write a function to count the number of nodes in the linked list and print
the count.
2. Write a program to merge two sorted linked lists into a single sorted linked list
and print the merged list.
Experiment 3
Objective:
1. Understand the stack data structure.
2. Learn how to implement a stack using an array in Java.
3. Perform basic stack operations: push, pop, peek, isEmpty, and isFull.
Algorithms:
isfull() function
• Check for the value of top in stack.
• If (top == capacity-1), then the stack is full so return true .
• Otherwise, the stack is not full so return false.
isempty() function
• Check for the value of top in stack.
• If (top == -1) , then the stack is empty so return true .
• Otherwise, the stack is not empty so return false .
Input:
push 10, 20, 30
pop 30
Output:
Top element is : 20
Elements present in stack : 20 10
Post-Experiment Questions:
1. How does the stack handle overflow?
a. What happens if you try to push an element onto a full stack?
2. How does the stack handle underflow?
a. What happens if you try to pop an element from an empty stack?
3. Write a program to reverse a string
a. Example: Input: "hello" Output: "olleh".
4. Solve the Tower of Hanoi problem using stacks.
Experiment 4
Objective:
To understand and implement the basic operations of a queue using an array in Java.
Algorithms:
Procedures to support queue functions –
enqueue():
1. Check the queue is full or not
2. If full, print overflow and exit
3. If queue is not full, increment tail and add the element
dequeue():
1. Check queue is empty or not
2. if empty, print underflow and exit
3. if not empty, print element at the head and increment head
isFullQueue():
1. To check if the queue is full, compare the number of elements in the queue (count) with
MAX_SIZE.
2. If count equals MAX_SIZE, the queue is full.
isEmptyQueue():
1. If number of elements in the queue (count) is equal to 0, then the queue is empty.
2. Otherwise, the queue is not empty.
Input:
enqueue 10, 20, 30, 40
dequeue 10
Output:
Front item is 20
Rear item is 40
Post-Experiment Questions:
Objective:
To understand and implement a circular queue using an array in Java, covering basic operations such
as enqueue, dequeue, checking if the queue is full or empty, and peeking at the front and rear
elements.
Algorithm:
IsFull Implementation
1. Check if the front index is equal to the rear index.
2. If true, the queue is empty; return true.
3. Otherwise, return false.
IsEmpty Implementation
1. Check if the (rear + 1) % MAX_SIZE is equal to the front index.
2. If true, the queue is full; return true.
3. Otherwise, return false.
Enqueue Implementation
1. Check if the queue is full using the isFull function.
2. If the queue is full, print “Queue Overflow” and return.
3. Otherwise, increment the rear index using (rear + 1) % MAX_SIZE.
4. Insert the new element at the rear index.
Dequeue Implementation
1. Check if the queue is empty using the isEmpty function.
2. If the queue is empty, print “Queue Underflow” and return.
3. Otherwise, increment the front index using (front + 1) % MAX_SIZE.
Peek Implementation
1. Check if the queue is empty using the isEmpty function.
2. If the queue is empty, print “Queue is Empty” and return -1.
3. Otherwise, return the element at (front + 1) % MAX_SIZE.
Input:
Insert 10, 20, 30
Delete 30
Insert 50
Output:
Queue elements: 10 20 30
Dequeued element: 10
Queue elements: 20 30 50
Post-Experiment Questions:
1. Design and implement a circular buffer for handling streaming data. Ensure that the buffer
correctly overwrites old data when new data arrives.
2. Simulate a printer queue using a circular queue. Implement functionalities for adding print
jobs, processing print jobs, and handling cases where the queue is full.
Experiment 6
Objective:
To implement a stack data structure using a linked list and perform operations such as
push, pop, and peek. This will help understand the stack's LIFO (Last-In-First-Out)
nature and how it can be managed using linked lists.
Post-Experiment Questions:
1. Extend your stack implementation to handle multiple data types (e.g., integers, floats, strings).
2. Design and implement a calculator that uses a stack to evaluate arithmetic expressions.
3. Modify your stack implementation to maintain and provide the current size of the stack. Add a
size function to return the number of elements in the stack.
Experiment 7
Objective:
To implement a queue using a linked list and understand the operations involved: enqueue,
dequeue, and checking if the queue is empty or not.
Algorithm:
Initialization
Steps:
1. Define a Node class to represent each element in the queue. Each node should
contain the data and a reference to the next node.
2. Create a Queue class that includes references to the front and rear nodes of the
queue.
3. Initialize the front and rear pointers to null when the queue is empty.
Enqueue Operation
Steps:
1. Create a new node with the given data.
2. If the queue is empty (i.e., front is null), set both the front and rear pointers to the
new node.
3. If the queue is not empty, link the new node to the end of the queue by setting the
next pointer of the rear node to the new node.
4. Update the rear pointer to the new node.
Dequeue Operation
Steps:
1. If the queue is empty (i.e., front is null), indicate that the queue is empty and no
element can be dequeued.
2. If the queue is not empty, store the data of the front node to return later.
3. Move the front pointer to the next node in the queue.
4. If the front becomes null (i.e., the queue becomes empty), also set the rear pointer
to null.
5. Return the stored data.
isEmpty Operation
Steps:
1. Return true if both the front and rear pointers are null.
2. Otherwise, return false.
Peek Operation
Steps:
1. If the queue is empty (i.e., front is null), indicate that the queue is empty and there
is no element to peek.
2. If the queue is not empty, return the data of the front node.
Example Input and Output:
Input:
Enqueue 10, 20, 30
Dequeue one element
Output:
Dequeued element: 10
Queue contains: 20 30
Post-Experiment Questions:
1. Implement a Queue using Linked List. A Query Q is of 2 Types
(i) 1 x (a query of this type means pushing 'x' into the queue)
(ii) 2 (a query of this type means to pop an element from the queue and print the poped
element)
Input:
Q=5
Queries = 1 2 1 3 2 1 4 2
Output: 2 3
Explanation: In the first test case 1 2 the queue will be {2}. 1 3 the queue will be {2
3}. 2 popped element will be 2 the queue will be {3}. 1 4 the queue will be {3 4}. 2
popped element will be 3.
Experiment 8
Objectives:
Algorithm:
Initialization
Enqueue Operation
Dequeue Operation
Peek Operation
Input:
Enqueue 10, 20, 30
Dequeue one element
Output:
Dequeued element: 10
Queue contains: 20 30
Post-Experiment Questions:
Problem: Design a circular buffer to handle streaming data. Ensure that when the buffer is
full, new data overwrites the oldest data.
AIM: Write a Program to implement a binary search tree using linked list
Objective:
To understand and implement a Binary Search Tree (BST) using a linked list in Java, and perform various
operations such as insertion, deletion, and traversal.
Algorithm:
Procedure:
1. Start at the root of the BST.
2. Compare the value to be inserted with the value of the current node.
3. If the value is less than the current node's value, move to the left child.
4. If the value is greater than the current node's value, move to the right child.
5. Repeat steps 2-4 until reaching a null reference.
6. Insert the new node at the null position.
Procedure:
1. Start at the root of the BST.
2. Search for the node to be deleted.
3. If the node has no children, simply remove it.
4. If the node has one child, replace the node with its child.
5. If the node has two children:
6. Find the inorder successor (the smallest node in the right subtree).
7. Replace the node's value with the inorder successor's value.
8. Delete the inorder successor.
Procedure:
1. Start at the root node.
2. Recursively traverse the left subtree.
3. Visit the current node.
4. Recursively traverse the right subtree.
Procedure:
1. Start at the root node.
2. Visit the current node.
3. Recursively traverse the left subtree.
4. Recursively traverse the right subtree.
Procedure:
1. Start at the root node.
2. Recursively traverse the left subtree.
3. Recursively traverse the right subtree.
4. Visit the current node.
Input:
1. Insert the values 50, 30, 70, 20, 40, 60, 80 into an empty BST.
2. Delete the value 70 from the BST.
3. Perform inorder, preorder and postorder traversal on the BST.
Output:
1. Inorder Traversal: 20, 30, 40, 50, 60, 80
2. Preorder Traversal: 50, 30, 20, 40, 80, 60
3. Postorder Traversal: 20, 40, 30, 60, 80, 50
Post-Experiment Questions:
1. Write a Java program to search for a given value in a BST.
2. Write a Java program to find the minimum and maximum values in a BST.
3. Write a Java program to check if a given binary tree is a valid BST.
Experiment 10
Objective:
Algorithm:
BFS Traversal
1. While the queue is not empty:
2. Dequeue a node from the front of the queue.
3. Process the dequeued node (e.g., print it or record it).
4. For each unvisited neighbor of the dequeued node:
5. Mark the neighbor as visited.
6. Enqueue the neighbor.
End of BFS
The BFS ends when the queue becomes empty, indicating that all reachable nodes
have been visited.
Input:
Output
The BFS traversal order starting from node 0 is: 0 -> 1 -> 2 -> 3 -> 4
Post-Experiment Questions:
1. Given a graph, implement BFS to find the shortest path from a starting node to all
other nodes.
2. Use BFS to find and print all connected components of an undirected graph.
Experiment 11
Objective:
To implement the Breadth-First Search (DFS) algorithm using linked list in Java.
Algorithm:
Initialize Graph
Output: 2 0 1 3
Post-Experiment Questions:
Objective:
To understand and implement linear search and binary search algorithms in Java.
Algorithm:
1. Linear Search
Output:
1. Index of the target value if found.
2. Indicator if not found (e.g., -1).
2. Binary Search
Initialize Variables:
• Input sorted array of integers.
• Target value to be searched.
• Two pointers: low and high.
Output:
Index of the target value if found.
Indicator if not found (e.g., -1).
Output1:
Index: 3
Input 2:
Array: [5, 15, 25, 35, 45]
Target value: 100
Output 2:
Index: -1 (indicating the target is not found)
Post-Experiment Questions:
1. Implement a linear search to find the index of the value 7 in the array [1, 3, 5, 7, 9,
11].
Input 1:
arr[] = [1, 10, 20, 20, 40]
x = 20
Output 1: 2 3
Objective:
The objective of this lab is to understand and implement the Insertion Sort algorithm in Java. By the end
of this lab, you should be able to:
Algorithm:
1. We have to start with second element of the array as first element in the
array is assumed to be sorted.
2. Compare second element with the first element and check if the second
element is smaller then swap them.
3. Move to the third element and compare it with the second element, then the
first element and swap as necessary to put it in the correct position among the first
three elements.
4. Continue this process, comparing each element with the ones before it and
swapping as needed to place it in the correct position among the sorted elements.
5. Repeat until the entire array is sorted.
Input: [5, 2, 9, 1, 5, 6]
Sorting Steps:
• Initial Array: [5, 2, 9, 1, 5, 6]
• Take 2, compare with 5, and shift 5 to the right: [2, 5, 9, 1, 5, 6]
• Take 9, compare with 5 and 2, no shift needed: [2, 5, 9, 1, 5, 6]
• Take 1, compare with 9, 5, and 2, shift all to the right: [1, 2, 5, 9, 5, 6]
• Take 5, compare with 9, and shift 9 to the right: [1, 2, 5, 5, 9, 6]
• Take 6, compare with 9, and shift 9 to the right: [1, 2, 5, 5, 6, 9]
Output: [1, 2, 5, 5, 6, 9]
Post-Experiment Questions:
Objective:
The objective of this lab is to understand and implement the bubble Sort algorithm in Java. By the end of
this lab, you should be able to:
Algorithm:
• Bubble Sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order. The pass
through the list is repeated until the list is sorted.
• Each iteration of the outer loop moves the next largest element to its correct position.
Outer Loop:
1. Start an outer loop that runs from the first element to the last element of the array.
This loop represents each pass through the array.
2. The loop counter, i, will run from 0 to n-1 where n is the length of the array.
Inner Loop:
1. Within the outer loop, start an inner loop that runs from the first element to n-i-1.
2. The inner loop counter, j, will run from 0 to n-i-2. This ensures that each pass
compares the elements correctly without re-evaluating the already sorted elements.
1. Within the inner loop, compare the current element (array[j]) with the next element
(array[j+1]).
Repeat:
1. Repeat the inner loop until the end of the array is reached.
End of Sorting:
Post-Experiment Questions:
AIM: Write a Program to convert Infix to Prefix and postfix representation of an expression.
Objective:
To write a Java program that converts infix expressions to both prefix and postfix representations.
Algorithm:
Pop all the remaining operators from the stack and append to the output list.
Convert the reversed infix expression to postfix using the above steps.
• Reverse the postfix expression obtained in step 2 to get the prefix
expression.
Post-Experiment Questions:
Problem 1: Convert Infix to Postfix
Input: A * (B + C) / D
Expected Output: A B C + * D /