Student Copy Updated
Student Copy Updated
for
Semester: II
Academic Year: 2023-2024
LABORATORY RECORD
Course Code: ………………….
…………………………………………………………………………………………………………………… of
KCG College of Technology aspires to become a globally recognized Centre of excellence for science,
technology & engineering education, committed to quality teaching, learning, and research while
ensuring for every student a unique educational experience which will promote leadership, job creation,
social commitment and service to nation building.
• Nurture the virtue of service and an ethical value system in the young minds
● Inculcate innovative and entrepreneurial abilities as well as ethical values among the students
PEO 2 Devise, Implement and Deploy software solutions for computational problems.
PEO 3 Build software solutions for the challenging problems in industry and research.
PEO4 Manifest the ethical values and exhibit social responsibility.
COURSE OUTCOMES
Upon completion of this course, the student will be able to:
CO No Blooms
Description of the Course Outcome
level
Remembering the concept of data structures through ADT including List,
CO 1 K2
Stack and Queues
CO 2 Understand basic concepts about stacks, queues, lists, trees and graphs. K3
Able to apply and implement various tree traversal algorithms and ensure
CO 3 K3
their correctness.
Ability to analyze algorithms and develop algorithms through step-by step
CO 4 K3
approach in solving problems with the help of fundamental data structures.
Design applications and justify use of specific linear and binary data structures
CO 5 K3
for various Applications.
1
INDEX
Page
S. No. Topics Marks Signature
No
1a Array Implementation of Stack
4b Infix to Postfix
4c Expression Evaluation
8a Representation Of Graph
9a Linear Search
9b Binary Search
2
Ex. No. 1a ARRAY IMPLEMENTATION OF STACK
Date:
Algorithm
1. Start
2. Define a array stack of size max = 5
3. Initialize top = -1
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
7. If top < max -1
8. Increment top
9. Store element at current position of top
10. Else
11. Print Stack overflow
12. If choice = 2 then
13. If top < 0 then
14. Print Stack underflow
15. Else
16. Display current top element
17. Decrement top
18. If choice = 3 then
19. Display stack elements starting from top
20. Stop
Program
3
4
Output
5
Result: Thus push and pop operations of a stack was demonstrated using arrays.
MARKS MAXIMUM MARKS
VIVA QUESTIONS SPLIT UP MARKS OBTAINED
Algorithm 5
1. Define global declaration?
Program 10
Output 5
Total 20
2. Define data types?
6
Ex. No. 1b ARRAY IMPLEMENTATION OF QUEUE
Date:
Algorithm
1. Start
2. Define a array queue of size max = 5
3. Initialize front = rear = –1
4. Display a menu listing queue operations
5. Accept choice
6. If choice = 1 then
7. If rear < max -1
8. Increment rear
9. Store element at current position of rear
10. Else
11. Print Queue Full
12. If choice = 2 then
13. If front = –1 then
14. Print Queue empty
15. Else
16. Display current front element
17. Increment front
18. If choice = 3 then
19. Display queue elements starting from front to rear.
20. Stop
Program
7
8
Output
Algorithm 5
Program 10
Output 5
Total 20
Result: Thus insert and delete operations of a queue was demonstrated using arrays.
9
VIVA QUESTIONS
10
EX. 2 ARRAY IMPLEMENTATION OF LIST
Date:
Aim: To create a list using array and perform operations such as display, insertions and
deletions.
Algorithm
1. Start
2. Define list using an array of size n.
3. First item with subscript or position = 0
4. Display menu on list operation
5. Accept user choice
6. If choice = 1 then
7. Locate node after which insertion is to be done
8. Create an empty location in array after the node by moving the existing elements one
position ahead.
9. Insert the new element at appropriate position
10. Else if choice = 2
11. Get element to be deleted.
12. Locate the position of the element replace the element with the element in the
following position.
13. Repeat the step for successive elements until end of the array.
14. Else
15. Traverse the list from position or subscript 0 to n.
16. Stop
PROGRAM
{
11
12
Output
Algorithm 5
Total 20
13
VIVA QUESTIONS
1. What is an array?
14
EX. 3a LINKED LIST IMPLEMENTATION OF LIST [SINGLY LINKED LIST]
Date:
Aim: To define a singly linked list node and perform operations such as insertions and deletions
dynamically.
Algorithm
1. Start
2. Define single linked list node as self-referential structure
3. Create Head node with label = -1 and next = NULL using
4. Display menu on list operation
5. Accept user choice
6. If choice = 1 then
7. Locate node after which insertion is to be done
8. Create a new node and get data part
9. Insert the new node at appropriate position by manipulating address
10. Else if choice = 2
11. Get node's data to be deleted.
12. Locate the node and delink the node
13. Rearrange the links
14. Else
15. Traverse the list from Head node to node which points to null
16. Stop
Program
15
16
Output:
Program 10
Output 5
Total 20
17
VIVA QUESTIONS
1. How to search an element in list.
2. Define Dqueue?
18
Ex. No. 3b LINKED LIST IMPLEMENTATION OF STACK
Date:
Algorithm
1. Start
2. Define a singly linked list node for stack
3. Create Head node
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
7. Create a new node with data
8. Make new node point to first node
9. Make head node point to new node
10. If choice = 2 then
11. Make temp node point to first node
12. Make head node point to next of temp node
13. Release memory
14. If choice = 3 then
15. Display stack elements starting from head node till null
16. Stop
Program
19
20
21
Output
Result: Thus push and pop operations of a stack was demonstrated using linked list.
22
VIVA QUESTIONS
1. What is a Stack?
Algorithm 5
Program 10
Output 5
Total 20
23
Ex. No. 3c LINKED LIST IMPLEMENTATION OF QUEUE
Date:
Algorithm
1. Start
2. Define a singly linked list node for stack
3. Create Head node
4. Display a menu listing stack operations
5. Accept choice
6. If choice = 1 then
7. Create a new node with data
8. Make new node point to first node
9. Make head node point to new node
10. If choice = 2 then
11. Make temp node point to first node
12. Make head node point to next of temp node
13. Release memory
14. If choice = 3 then
15. Display stack elements starting from head node till null
16. Stop
Program
24
25
26
Output
Result: Thus push and pop operations of a stack was demonstrated using linked list.
27
VIVA QUESTIONS
1. Write down the operations that can be done with queue data structure?
Algorithm 5
Program 10
Output 5
Total 20
28
Ex. No. 4a APPLICATIONS OF LIST
POLYNOMIAL ADDITION AND SUBTRACTION
Date:
Aim: To store a polynomial using linked list. Also, perform addition and subtraction on two
polynomials.
Algorithm
Let p and q be the two polynomials represented by linked lists
1. while p and q are not null, repeat step 2.
2. If powers of the two terms are equal
then If the terms do not cancel then
Insert the sum of the terms into the sum Polynomial
Advance p Advance q
Else if the power of the first polynomial> power of second Then
Insert the term from first polynomial into sum polynomial
Advance p
Else
insert the term from second polynomial into sum polynomial Advance q
3. Copy the remaining terms from the non empty polynomial into the sum polynomial.
The third step of the algorithm is to be processed till the end of the polynomials has not been
reached.
Program
29
30
;
31
32
Output
33
VIVA QUESTIONS
1. Define double circularly linked list?
Algorithm 5
Program 10
Output 5
Total 20
34
Ex. No. 4b INFIX TO POSTFIX
Date:
Aim: To convert infix expression to its postfix form using stack operations.
Algorithm
1. Start
2. Define a array stack of size max = 20
3. Initialize top = -1
4. Read the infix expression character-by-character
5. If character is an operand print it
6. If character is an operator
7. Compare the operator‟s priority with the stack[top] operator.
8. If the stack [top] operator has higher or equal priority than the inputoperator,
Pop it from the stack and print it.
9. Else
10. Push the input operator onto the stack
11. If character is a left parenthesis, then push it onto the stack.
12. If the character is a right parenthesis, pop all the operators from the stack andprint it
until a left parenthesis is encountered. Do not print the parenthesis.
Program
35
36
37
Output
Result: Thus the given infix expression was converted into postfix form using stack.
38
Viva Questions
4. Write down the operations that can be done with queue data structure?
Algorithm 5
Program 10
Output 5
Total 20
39
Ex. No. 4c EXPRESSION EVALUATION
Date:
Algorithm
1. Start
2. Define a array stack of size max = 20
3. Initialize top = -1
4. Read the postfix expression character-by-character
5. If character is an operand push it onto the stack
6. If character is an operator
7. Pop topmost two elements from stack.
8. Apply operator on the elements and push the result onto the stack,
Eventually only result will be in the stack at end of the
expression.
9. Pop the result and print it.
Program
40
41
Output
Result: Thus the given postfix expression was evaluated using stack.
42
VIVA QUESTIONS
5. Write down the operations that can be done with queue data structure?
Algorithm 5
Program 10
Output 5
Total 20
43
Ex No. 5 IMPLEMENTATION BINARY SEARCH TREE
Date:
Algorithm
1. Start
2. Call insert to insert an element into binary search tree.
3. Call delete to delete an element from binary search tree.
4. Call findmax to find the element with maximum value in binary search tree
5. Call findmin to find the element with minimum value in binary search tree
6. Call find to check the presence of the element in the binary search tree
7. Call display to display the elements of the binary search tree
8. Call makeempty to delete the entire tree.
9. Stop
Insert function
1. Get the element to be inserted.
2. Find the position in the tree where the element to be inserted by
checking the elements in the tree by traversing from the root.
3. If the element to be inserted is less than the element in the current node in
the tree then traverse left subtree
4. If the element to be inserted is greater than the element in the current node
in the tree then traverse right subtree
5. Insert the element if there is no further move
Delete function
1. Get the element to be deleted.
2. Find the node in the tree that contain the element.
3. Delete the node an rearrange the left and right siblings if any present for the
deleted node
Findmax function
1. Traverse the tree from the root.
2. Find the rightmost leaf node of the entire tree and return it
3. If the tree is empty return null.
Findmin function
1. Traverse the tree from the root.
2. Find the leftmost leaf node of the entire tree and return it
3. If the tree is empty return null.
Find function
1. Traverse the tree from the root.
2. Check whether the element to searched matches with element of the current
node. If match occurs return it.
3. Otherwise if the element is less than that of the element of the current node
then search the leaf subtree
4. Else search right subtree.
Makeempty function
Make the root of the tree to point to null.
44
Program
45
46
47
48
49
Output:
Result:
The program for binary search tree is executed and verified.
50
VIVA QUESTIONS
2. What is a Stack ?
4. What is a Queue ?
Algorithm 5
Program 10
Output 5
Total 20
51
Ex. No.6 IMPLEMENTATION OF AVL TREES
Date:
2. [Link] and [Link] are both AVL trees. = the height balance of every
node must be -1, 0, or 1
Insert operation may cause balance factor to become 2 or –2 for some node
› only nodes on the path from insertion point to root node have possibly changed in height
› So after the Insert, go back up to the root node by node, updating heights
› If a new balance factor (the difference hleft – hright ) is 2 or –2, adjust tree by rotation
around the node
Let the node that needs rebalancing be α.
There are 4 cases:
Outside Cases (require single rotation) :
1. Insertion into left subtree of left child of α.
2. Insertion into right subtree of right child of
α. Inside Cases (require double rotation) :
3. Insertion into right subtree of left child of α.
4. Insertion into left subtree of right child of α.
The rebalancing is performed through four separate rotation algorithms.
You can either keep the height or just the difference in height, i.e. the balance factor; this
has to be modified on the path of insertion even if you don‟t perform rotations
Once you have performed a rotation (single or double) you won‟t need to go back up
the tree
SINGLE ROTATION
RotateFromRight(n : reference node pointer)
{
p : node pointer;
p := [Link];
[Link] := [Link];
[Link] := n;
n := p
}
You also need to modify the heights or balance factors of n and p
52
DOUBLE ROTATION
DoubleRotateFromRight(n : reference node pointer)
{
RotateFromLeft([Link]);
RotateFromRight(n);
}
DELETION
Similar but more complex than insertion
› Rotations and double rotations needed to rebalance
› Imbalance may propagate upward so that many rotations may be needed.
Program
53
54
55
OUTPUT
56
VIVA QUESTIONS
3. Is it possible to insert multiple nodes into an AVL tree at once? If yes, then how?
5. Is it possible to retrieve the smallest element in an AVL tree? If yes, then how?
Algorithm 5
Program 10
Output 5
Total 20
57
Ex. No.7 IMPLEMENTATION OF HEAP USING PRIORITY QUEUES
Date:
Algorithm
Priority queue is a type of queue in which every element has a key associated to it
and the queue returns the element according to these keys, unlike the traditional
queue which works on first come first serve basis.
Thus, a max-priority queue returns the element with maximum key first whereas, a
min-priority queue returns the element with the smallest key first.
Priority queues are used in many algorithms like Huffman Codes, Prim's algorithm,
etc. It is also used in scheduling processes for a computer, etc.
Heaps are great for implementing a priority queue because of the largest and
smallest element at the root of the tree for a max-heap and a min- heap respectively.
We use a max-heap for a max-priority queue and a min-heap for a min-priority
queue.
2. Maximum/Minimum → To get the maximum and the minimum element from the
max-priority queue and min-priority queue respectively.
This is like the pop of a queue, we return the element as well as delete it from the
heap. So, we have to return and delete the root of a heap. Firstly, we store the value
of the root in a variable to return it later from the function and then we just make the
root equal to the last element of the heap. Now the root is equal to the last element of
the heap, we delete the last element easily by reducing the size of the heap by 1.
58
Doing this, we have disturbed the heap property of the root but we have not
touched any of its children, so they are still heaps. So, we can
call Heapify on the root to make the tree a heap again.
EXTRACT-MAXIMUM(A)
Program
59
60
61
OUTPUT
RESULT:
Thus the implementation heap using priority queue was demonstrated.
62
VIVA QUESTIONS
1. Write down the operations that can be done with queue data structure?
Algorithm 5
Program 10
Output 5
Total 20
63
Ex. No. 8a REPRESENTATION OF GRAPH
Date:
Algorithm
For a graph with |V| vertices, an adjacency matrix is a |V|×∣V∣ matrix of 0s and
1s, where the entry in row i and column j is 1 if and only if the edge (i,j) in the
graph.
1. Consider the graph to be represented
2. Start from an edge
3. If the edge connects the vertices i,j the mark the ith row and jth column of the
adjacency matrix as 1 otherwise store 0
4. Finally print the adjacency matrix
Program
64
65
OUTPUT
66
VIVA QUESTIONS
5. What is a Graph?
Algorithm 5
Program 10
Output 5
Total 20
67
Ex. No. 8b GRAPH TRAVERSAL-BREADTH FIRST TRAVERSAL
Date:
Algorithm
BFS (G, s) //Where G is the graph and s is the source
node let Q be queue.
[Link]( s ) //Inserting s in queue until all its neighbour vertices are marked.
mark s as visited.
while ( Q is not
empty)
//Removing that vertex from queue,whose neighbour will be visited now
v = [Link]( )
Program
68
69
70
Output
Total 20
3. Find shortest path from source to destination in a matrix that satisfies given constraints
71
Ex. No. 8c GRAPH TRAVERSAL-DEPTH FIRST TRAVERSAL
Date:
Algorithm
Program
72
73
OUTPUT:
Result : Thus the graph using depth first search traversal was demonstrated.
Algorithm 5
Program 10
2. Transitive closure of a graph using DFS
Output 5
Total 20
3. Application of DFS
74
Ex. No. 9a LINEAR SEARCH
Date:
Algorithm
1. Start
2. Read number of array elements n
3. Read array elements Ai, i = 0,1,2,…n–1
4. Read search value
5. Assign 0 to found
6. Check each array element against search
7. If Ai = search
then found = 1
Print "Element found"
Print position i
Stop
8. If found = 0 then
print "Element not found"
Stop
Program
75
76
Output
Result
Thus an array was linearly searched for an element's existence.
MARKS MAXIMUM MARKS
VIVA Questions:
SPLIT UP MARKS OBTAINED
1. What is meant by Sorting and searching? Algorithm 5
Program 10
Total 20
77
Ex. No. 9b BINARY SEARCH
Date:
Aim
To locate an element in a sorted array using Binary search method
Algorithm
1. Start
2. Read number of array elements, say n
3. Create an array arr consisting n sorted elements
4. Get element, say key to be located
5. Assign 0 to lower and n to upper
6. While (lower < upper)
a. Determine middle element mid = (upper+lower)/2
b. If key = arr[mid] then
i. Print mid
ii. Stop
c. Else if key > arr[mid] then
i. lower = mid + 1
d. else
i. upper = mid – 1
7. Print "Element not found"
8. Stop
Program
78
79
Output
Result
Thus an element is located quickly using binary search method.
VIVA Question
Algorithm 5
Program 10
Output 5
Total 20
80
Ex. No. 10a INSERTION SORT
Date:
Algorithm
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Outer index i varies from second element to last element
5. Inner index j is used to compare elements to left of outer index
6. Insert the element into the appropriate position.
7. Display the array elements after each pass
8. Display the sorted array elements.
9. Stop
Program
81
82
Output
Program 10
Total 20
83
Ex. No. 10b BUBBLE SORT
Date:
Algorithm
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Outer Index i varies from last element to first element
a. Index j varies from first element to i-1
i. Compare elements Aj and Aj+1
ii. If out-of-order then swap the elements
iii. Display array elements after each pass
iv. Display the final sorted list
5. Stop
Program
84
Output
Result
Thus an array was sorted using bubble sort.
85
VIVA Questions
1. Define selection sort?
Algorithm 5
Program 10
Output 5
Total 20
86