DS
DS
LABORATORY MANUAL
Roll No.
Section-Batch
Develop a platform for achieving globally acceptable level of intellectual acumen and
technological competence.
Create an inspiring ambience that raises the motivation level for conducting quality research.
To spark the imagination of the Computer Science Engineers with values, skills and creativity to
solve the real world problems.
To inculcate creative thinking and problem solving skills through effective teaching, learning
and research.
To empower professionals with core competency in the field of Computer Science and
Engineering.
To foster independent and lifelong learning with ethical and social responsibilities.
PEO2: To establish as entrepreneurs, and work in interdisciplinary research and development organizations
as an individual or in a team.
PEO3: To inculcate ethical values and leadership qualities in students to have a successful career.
PEO4: To develop analytical thinking that helps them to comprehend and solve real-world problems
and inherit the attitude of lifelong learning for pursuing higher education.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 6
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
Design/development of solutions: Design solutions for complex engineering 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.
The engineer 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 engineering practice.
Environment and sustainability: Understand the impact of the professional engineering solutions
in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 6
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
write effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
Project management and finance: Demonstrate knowledge and understanding of the engineering
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.
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.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 6
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
PSO2: Ability to analyse, design, develop, test and manage complex software system and
applications using advanced tools and techniques.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 6
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
CO-PO MAPPING
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
3 2 2 1 3 - - 1 1 1 - 2
C209.1
2 3 3 2 3 - - 1 1 1 - 2
C209.2
2 3 3 2 3 - - 1 1 1 - 2
C209.3
2 3 3 2 3 - - 1 1 1 - 2
C209.4
2.25 2.75 2.75 1.75 3.00 - - 1.00 1.00 1.00 - 2.00
C209
CO-PSO MAPPING
PSO1 PSO2
2 2
C 209.1
2 2
C 209.2
2 2
C 209.3
2 2
C 209.4
C209 2.00 2.00
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 6
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
LIST OF EXPERIMENTS
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 9
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 9
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
INTRODUCTION
In computer science, data structure is a particular way of organizing and storing data in a computer
so that it can be accessed and modified efficiently. Precisely, it is a collection of data values, the
relationships among them, and the functions or operations that can be applied to the data.
Data structures can implement one or more particular abstract data types (ADT), which specify the
operations that can be performed on a data structure and the computational complexity of those
operations. Usually, efficient data structures are the key to designing efficient algorithms. The
implementation of a data structure usually requires writing a set of procedures that create and
manipulate instances of that structure. The efficiency of a data structure cannot be analyzed separately
from those operations.
There are numerous types of data structures, generally built upon simpler primitive data types:
An array is a number of elements in a specific order, typically all of the same type. Elements are
accessed using an integer index to specify which element is required. Typical implementations
allocate contiguous memory words for the elements of arrays (but this is not always a necessity).
Arrays may be fixed-length or resizable.
A linked list is a linear collection of data elements of any type, called nodes, where each node has
itself a value, and points to the next node in the linked list. The principal advantage of a linked list
over an array is that values can always be efficiently inserted and removed without relocating the rest
of the list. Certain other operations, such as random access to a certain element, are however slower
on lists than on arrays.
Data structures are generally based on the ability of a computer to fetch and store data at any place in
its memory, specified by a pointer—a bit string, representing a memory address, that can be itself
stored in memory and manipulated by the program. Thus, the array data structures are based on
computing the addresses of data items with arithmetic operations; while the linked data structures are
based on storing addresses of data items within the structure itself.
A data structure is said to be linear if its elements form a sequence or a linear list. The linear data
structures like an array, stacks, queues and linked lists organize data in linear order.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 9
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
A data structure is said to be non-linear if its elements form a hierarchical classification where, data
items appear at various levels. Trees and Graphs are widely used non-linear data structures. Tree and
graph structures represent hierarchical relationship between individual data elements. Graphs are
nothing but trees with certain restrictions removed.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 13
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
PREFACE
In order to develop efficient software systems, it is essential that efficient algorithms and appropriate
data structures are used. The purpose of this laboratory manual is to introduce undergraduate students
to techniques for developing efficient data structures and algorithms in a systematic manner. The
manual serves as a guide for learning and implementing the linear and non- linear data structures in
a programming language (C). It basically focuses on memory management and various other
operations on data with algorithm analysis and design. The manual contains procedures, and pre-
experiment questions to help students prepare for experiments.
This practical lab manual will be helpful for students of Computer Science & Engineering for
understanding the course from the point of view of applied aspects. Though all the efforts have been
made to make this manual error free, yet some errors might have crept in inadvertently. Suggestions
from the readers for the improvement of the manual are most welcomed.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 13
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
DO’s
DONT’S
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 13
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
1. Know the location of the fire extinguisher and the first aid box and how to use them in case
of an emergency.
2. Report fires or accidents to your faculty /laboratory technician immediately.
3. Report any broken plugs or exposed electrical wires to your faculty/laboratory
technician immediately.
4. Do not plug in external devices without scanning them for computer viruses.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 13
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
MARKS FACULTY
DATE OF EXPT. TITLE OF THE PAGE
S.No AWARDED SIGNATURE
CONDUCTION No EXPERIMENT No.
(20) WITH REMARK
10
11
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 16
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
12
13
14
15
16
17
18
19
20
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 16
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
While preparing the lab records, the student is required to adhere to the following guidelines:
Contents to be included in Lab Records:
1. Cover page
2. Vision
3. Mission
4. PEOs
5. POs
6. PSOs
7. COs
8. CO-PO-PSO mapping
9. Index
10. Experiments
Aim
Source code
Input-Output
A separate copy needs to be maintained for pre-lab written work
The student is required to make the Lab File as per the format given on the next two pages.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 16
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Name
Roll No.
Section- Batch
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
INDEX
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Students are provided with the details of the experiment (Aim, pre-experimental questions, procedure
etc.) to be conducted in next lab and are expected to come prepared for each lab class.
Faculty ensures that students have completed the required pre-experiment questions and they
complete the in-lab programming assignment(s) before the end of class. Given that the lab programs
are meant to be formative in nature, students can ask faculty for help before and during the lab class.
Student’s performance will be assessed in each lab based on the following Lab Assessment
Components:
AC1: Written Work (Max. marks = 4)
AC2: Fundamental Knowledge to conduct Experiment (Max. marks = 4)
AC3: Experiment Completed Successfully (Max. marks = 4)
AC4: Questions Answered (Max. marks = 4)
AC5: Punctuality (Max. marks = 4)
In each lab class, students will be awarded marks out of 4 under each component head, making it
total out of 20 marks.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 1
Description: Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison based
algorithm in which each pair of adjacent elements is compared and elements are swapped if they are
not in order. This algorithm is not suitable for large data sets as its average and worst case complexity
are of O(n2) where n are no. of items.
This is a in-place comparison based sorting algorithm. Here, a sub-list is maintained which is always
sorted. For example, the lower part of an array is maintained to be sorted. A element which is to be
inserted in this sorted sub-list, has to find its appropriate place and insert it there. Hence the name
insertion sort. The array is searched sequentially and unsorted items are moved and inserted into
sorted sub-list (in the same array). This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο (n2) where n are no. of items. This process goes until all the
unsorted values are covered in sorted sub-list.
We assume list is an array of n elements. We further assume that swap function, swaps the values of
given array elements.
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Input:
157289
Output:
Sorted Elements: 1 2 5 7 8 9
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 2
Description: Selection sort is a simple sorting algorithm. This sorting algorithm is a in-place
comparison based algorithm in which the list is divided into two parts, sorted part at left end and
unsorted part at right end. Initially sorted part is empty and unsorted part is entire list. Smallest
element is selected from the unsorted array and swapped with the leftmost element and that element
becomes part of sorted array. This process continues moving unsorted array boundary by one
element to the right. This algorithm is not suitable for large data sets as its average and worst case
complexity are of O(n2) where n are no. of items.
Input:
136289
Output:
Sorted Elements: 1 2 3 6 8 9
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q1. What is the advantage of selection sort over other sorting techniques?
a) It requires no additional storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sorting technique
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 3
Pre-Experiment Question:
Q1. Which is the correct order of the following algorithms with respect to their time
Complexity in the best case?
a) Merge sort > Quick sort >Insertion sort > selection sort
b) insertion sort < Quick sort < Merge sort < selection sort
c) Merge sort > selection sort > quick sort > insertion sort
d) Merge sort > Quick sort > selection sort > insertion sort
Q2. Consider the array A[]= {6,4,8,1,3} apply the insertion sort to sort the array . Consider the
cost associated with each sort is 25 rupees , what is the total cost of the insertion sort when
element 1 reaches the first position of the array ?
a) 50
b) 25
c) 75
d) 100
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Input:
136259
Output:
Sorted Elements: 1 2 3 5 6 9
PostExperiment Question:
Q1. Consider an array of elements arr[5]= {5,4,3,2,1} , what are the steps of insertions done while
doing insertion sort in the array.
a) 4 5 3 2 1 3 4 5 2 1 2 3 4 5 1 1 2 3 4 5
b) 5 4 3 1 2 5 4 1 2 3 5 1 2 3 4 1 2 3 4 5
c) 4 3 2 1 5 3 2 1 5 4 2 1 5 4 3 1 5 4 3 2
d) 4 5 3 2 1 2 3 4 5 1 3 4 5 2 1 1 2 3 4 5
Q2. Which sorting algorithm will take least time when all elements of input array are
identical? Consider typical implementations of sorting algorithms.
a)Insertion Sort
b)Heap Sort
c) Merge Sort
d) Selection Sort
Q3. If all the elements in an input array is equal for example {1,1,1,1,1,1}, What would be the
running time of the Insertion Algorithm?
Q4. What operation does the Insertion Sort use to move numbers from the unsorted section to the
sorted section of the list?
a) Finding the minimum value
b) Swapping
c) Finding out an pivot value
d) None of the above
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 4
Description: QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and
partitions the given array around the picked pivot. There are many different versions of quickSort
that pick pivot in different ways.
Q1. Suppose we are sorting an array of eight integers using quicksort, and we have just finished the
first partitioning with the array looking like this:
2 5 1 7 9 12 11 10
Which statement is correct?
(A) The pivot could be either the 7 or the 9.
(B) The pivot could be the 7, but it is not the 9
(C) The pivot is not the 7, but it could be the 9
(D) Neither the 7 nor the 9 is the pivot.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
at right place */
pi = partition(arr, low, high);
quickSort(arr, low, pi - 1); // Before pi
quickSort(arr, pi + 1, high); // After pi
}
}
Input:
1 3 6 2 8 9 4 10
Output:
Sorted Elements: 1 2 3 4 6 8 9 10
Q1. Which of the following is not a stable sorting algorithm in its typical implementation.
Insertion Sort
Merge Sort
Quick Sort
Bubble Sort.
Q2. Which sorting algorithms is most efficient to sort string consisting of ASCII characters?
Quick sort
Heap sort
Merge sort
Counting sort
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 5
Description: Radix Sort is a linear sorting algorithm that sorts elements by processing them digit by
digit. It is an efficient sorting algorithm for integers or strings with fixed-size keys. With worst-case
time complexity O(d * (n + b)), where d is the number of digits, n is the number of elements, and b
is the base of the number system being used.
Q1. Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort?
Q2. What is the best time complexity of the Radix sort?
a) Omega(n log(n))
b) Omega(n+k)
c) Omega(n)
d) Omega(nk)
Q3. The maximum number of comparisons needed to sort 9 items using Radix sort is
(each number is a 5 digit octal no.)
a) 45
b) 72
c) 360
d)450
c)Merge Sort
d)Tree Sort
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Step 1 − Find the largest element in the array, which is 802. It has three digits, so we will
iterate three times, once for each significant place.
Step 2 − Sort the elements based on the unit place digits (X=0). We use a stable sorting
technique, such as counting sort, to sort the digits at each significant place.
Step 3 − Sort the elements based on the tens place digits.
Step 4 − Sort the elements based on the hundreds place digits.
Step 5 − The array is now sorted in ascending order.
Input:
170 45 75 90 802 24 2 66
Output:
b) O(d nk)
c) O((d +n) k)
d)O(d (n +k))
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 21
JSS Academy of Technical Education – NOIDA
EXPERIMENT - 6
Pre-experimental Questions:
Q1. In a max-heap, element with the greatest key is always in which node?
Q2.Heap can be used as…
a)Priority queue
b)Stack
c)Adecreasingorderarray
d) None of the mentioned
Q3.What is the space complexity of searching in a heap?
Q4. What is the best case complexity in builading a heap?
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 30
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
}
}
Input:
1 3 6 2 8 9 4 10
Output:
Sorted Elements: 1 2 3 4 6 8 9 10
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 31
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 7
Description: Linear search is a very simple search algorithm. In this type of search, a sequential
search is made over all items one by one. Every item is checked and if a match founds then that
particular item is returned otherwise search continues till the end of the data collection.
Pre-Experiment Questions:
Input:
157289
Elements to be found: 8
Output:
Elements found at position no. 5
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 34
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q3. What is the best case and worst case complexity of ordered linear search?
b) a) O(nlogn), O(logn) O(logn), O(nlogn)
c)O(n), O(1)
d)O(1), O(n)
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 34
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 8
Description: Binary search is a fast search algorithm with run-time complexity of Ο(log n). These
search algorithms works on the principle of divide and conquer. For this algorithm to work properly
the data collection should be in sorted form. Binary search search a particular item by comparing the
middle most item of the collection. If match occurs then index of item is returned. If middle item is
greater than item then item is searched in sub-array to the right of the middle item other wise item
is search in sub-array to the left of the middle item. This process continues on sub-array as well until
the size of subarray reduces to zero.
Pre-Experiment Question:
Set lowerBound = 1
Set upperBound = n
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 34
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
end while
end procedure
Input:
157289
Elements to be found: 8
Output:
Elements found at position no. 5
Post Experiment Questions:
Q1. Binary Search can be categorized into which of the following?
a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming
Q2. What is the time complexity of binary search with iteration?
Q3. Given an array arr = {45,77,89,90,94,99,100} and key = 100; What are the mid
values(corresponding array elements) generated in the first and second iterations?
a) 90 and 99
b) 90 and 100
c) 89 and 94
d) 94 and 99
Q4. Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done until the
element is found?
a) 1
b) 3
c) 4
d) 2
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 35
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 9
As in example given above, BFS algorithm traverses from A to B to E to F first then to C and G
lastly to D. It employs following rules.
Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex found, remove the first vertex from queue.
Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty
Pre Experiment Questions:
Q1. What is the difference between BFS and DFS?
Q2.The Data structure used in standard implementation of Breadth First Search is?
Q3. When the Breadth First Search of a graph is unique?
Q4. Breadth First Search is equivalent to which of the traversal in the Binary Trees?
Algorithm:
Set all nodes to "not visited";
q = new Queue();
q.enqueue(initial node);
while ( q ≠ empty ) do
{
x = q.dequeue();
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 37
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
visited[x] = true;
q.enqueue(y);
}
}
Input:
Above mentioned graph
Output:
SABCDEFG
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 37
JSS Academy of Technical Education – NOIDA
EXPERIMENT - 10
Description: A Hash Function is a function that converts a given numeric or alphanumeric key to a small
practical integer value. The mapped integer value is used as an index in the hash table. In simple terms, a
hash function maps a significant number or string to a small integer that can be used as the index in the
hash table.
The pair is of the form (key, value), where for a given key, one can find a value using some kind of a
“function” that maps keys to values. The key for a given object can be calculated using a function called a
hash function. For example, given an array A, if i is the key, then we can find the value by simply looking
up A[i].
Pre-Experiment Questions:
h(K) = h(k x k)
Q1. A technique for direct search
Here,
k isBinary
a) the keysearch
value.
b) Linear search
c) Tree search
d) Hashing
a) Binary search
b) Linear search
c) Tree search
d) Hashing
(1) Division Method:
h(K) = k mod M
Here,
k is the key value, and
M is the size of the hash table.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 38
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Input:
(1) k = 12345
M = 95
Output:
k = 60
Output:
k x k = 60 x 60
= 3600
h(60) = 60
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 40
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 11
Descriptions: A stack is an abstract data type, commonly used in most programming languages Stack
ADT allows all data operations at one end only. At any given time, we can only access the top
element of a stack. This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. In
stack terminology, insertion operation is called push operation and removal operation is called
popoperation.
Stack Representation:
A stack can be implemented by means of Array (Static Implementation), and Linked list (Dynamic
implementation).
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from
these basic stuffs, a stack is used for the following two primary operations −
To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the
following functionality is added to stacks −
peek() − get the top data element of the stack, without removing it.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 40
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always
represents the top of the stack, hence named top. The top pointer provides top value of the stack
without actually removing it.
Push Operation:
The process of putting a new data element onto stack is known as a Push Operation. Push operation
involves a series of steps −
If the linked list is used to implement the stack, then in step 3, we need to allocate space dynamically.
Pop Operation:
Accessing the content while removing it from the stack, is known as a Pop Operation. In an array
implementation of pop() operation, the data element is not actually removed, instead top is
decremented to a lower position in the stack to point to the next value. But in linked-list
implementation, pop() actually removes data element and deallocates memory space.
Data Structure Using C Lab (KCS 351) Manual (CS, III Sem) Pag e | 41
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Pre-Experiment Questions:
Q1. Explain Stack data structures.
Q3. Define the underflow and overflow conditions for stack operation.
Algorithms:
return stack[top]
end procedure
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 45
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Input:
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 45
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Output:
34
56
21
18
TOS=3
Q1. What is the advantage of dynamic implementation over static implementation of a stack?
Q2. What is the time complexity of push operation and pop operation?
Q3. Which one of the following is an application of Stack Data Structure?
a. Managing function calls
b. The stock span problem
c. Arithmetic expression evaluation
d. All of the above
Give an explanation for your answer.
Q4. Which of the following is true about linked list implementation of stack?
a. In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.
b. In push operation, if new nodes are inserted at the end, then in pop operation, nodes must
be removed from the beginning.
c. Both of the above
d. None of the above
Give an explanation for your answer.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 45
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 12
Descriptions: Queue is an abstract data structure, somewhat similar to Stack. In contrast to Queue,
queue is opened at both ends. One end is always used to insert data (enqueue) and the other is used
to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored
first will be accessed first.
Queue Representation
Same as Queue, queue can also be implemented using Array (Static Implementation), Linked-list
(Dynamic implementation)
Queue operations may involve initializing or defining the queue, utilizing it and then completing
erasing it from memory. There are two basic operations associated with queues −
Few more functions are required to make above mentioned queue operation efficient. These are −
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or
storing) data in queue we take help of rear pointer.
isfull(): As we are using single dimension array to implement queue, we just check for the rear
pointer to reach at MAXSIZE to determine that queue is full. In case we maintain queue in a
circular linked-list, the algorithm will differ
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 45
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Enqueue Operation:
As queue maintains two data pointers, front and rear, its operations are comparatively more
difficult to implement than Queue.
The following steps should be taken to enqueue (insert) data into a queue −
Sometimes, we also check that if queue is initialized or not to handle any unforeseen situations.
Dequeue Operation:
Accessing data from queue is a process of two tasks − access the data wherefront is pointing and
remove the data after access. The following steps are taken to perform dequeue operation −
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 48
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q2. What are the overflow and underflow conditions for operations on a Queue?
Algorithms:
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 48
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
return true
end procedure
Algorithm for dequeue operation :
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
Input:
1 45 78 90
Output:
23 1 45 78 90
Input:
23 1 45 78 90
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 48
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Output:
23 1 45 78
Q1. What is the advantage of dynamic implementation over static implementation of a queue?
Q2. What is the time complexity of enqueue operation and dequeue operation?
Q3. Which one of the following is an application of Queue Data Structure?
a. When a resource is shared among multiple consumers.
b. When data is transferred asynchronously (data not necessarily received at same rate as sent)
between two processes
c. Load Balancing
d. All of the above
Explain your answer.
Q4. Which of the following is true about linked list implementation of queue?
a. In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.
b. In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be
removed from the beginning.
c. Both of the above
d. None of the above
Explain your answer.
Q5. Suppose you are given an implementation of a queue of integers. The operations that can be
performed on the queue are:
i. isEmpty (Q) — returns true if the queue is empty, false otherwise.
ii. delete (Q) — deletes the element at the front of the queue and returns its value.
iii. insert (Q, i) — inserts the integer i at the rear of the queue.
Consider the following function:
void f (queue Q) {
int i ;
if (!isEmpty(Q)) {
i = delete(Q);
f(Q);
insert(Q, i);
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 56
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
}
}
What operation is performed by the above function f ?
a. Leaves the queue Q unchanged
b. Reverses the order of the elements in the queue Q
c. Deletes the element at the front of the queue Q and inserts it at the rear keeping the other
elements in the same order
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 56
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT – 13
Descriptions: Circular queue is a linear data structure. It follows FIFO principle. In circular queue
the last node is connected back to the first node to make a circle.Circular linked list fallow the First
In First Out principle. Elements are added at the rear end and the elements are deleted at front end of
the queue.
Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear
elements of the QUEUE. ITEM is the value to be inserted.
1. If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then
2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches end if QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by 1] [End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 56
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Input:
10.
Output:
7426135
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 56
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q4. A circular queue is implemented using an array of size 10. The array index starts with 0, front
is 6, and rear is 9. The insertion of next element takes place at the array index .
Q5. If the MAX_SIZE is the size of the array used in the implementation of circular queue, array
index start with 0, front point to the first element in the queue, and rear point to the last element in
the queue. Which of the following condition specify that circular queue is EMPTY?
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 56
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT -14
Descriptions: The major problem with the stack implemented using array is, it works only for fixed
number of data values. That means the amount of data must be specified at the beginning of the
implementation itself. Stack implemented using array is not suitable, when we don't know the size of
data which we are going to use. A stack data structure can be implemented by using linked list data
structure. The stack implemented using linked list can work for unlimited number of values. That
means, stack implemented using linked list works for variable size of data. So, there is no need to fix
the size at the beginning of the implementation. The stack implemented using linked list can organize
as many as data values as we want.
In linked list implementation of a stack, every new element is inserted as 'top' element. That means
every newly inserted element is pointed by 'top'. Whenever we want to remove an element from the
stack, simply remove the node which is pointed by 'top' by moving 'top' to its next node in the list.
The next field of the first element must be always NULL.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 56
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Input:
Insert element 7 in the stack
426135
Output:
4261357
TOS=7
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 56
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 15
Descriptions: The major problem with the queue implemented using array is, It will work for only
fixed number of data. That means, the amount of data must be specified in the beginning itself. Queue
using array is not suitable when we don't know the size of data which we are going to use. A queue
data structure can be implemented using linked list data structure. The queue which is implemented
using linked list can work for unlimited number of values. That means, queue using linked list can
work for variable size of data (No need to fix the size at beginning of the implementation). The
Queue implemented using linked list can organize as many data values as wewant.
In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and the
first node is always pointed by 'front'.
In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted node
is 10 and it is pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.
Q1. The essential condition which is checked before insertion in a linked queue is?
Q2. The essential condition which is checked before deletion in a linked queue is?
Q3. Which of the following is true about linked list implementation of queue?
a. In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end
b. In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be
removed from the beginning
c. Both a and b
d. None of the mentioned
Explain your answer.
Algorithm for enQueue(value) - Inserting an element into the Queue
Step 1: Create a newNode with given value and set 'newNode → next' to NULL.
Step 2: Check whether queue is Empty (rear == NULL)
Step 3: If it is Empty then, set front = newNode and rear = newNode.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 56
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Step 4: If it is Not Empty then, set rear → next = newNode and rear = newNode.
Algorithm for deQueue() - Deleting an Element from Queue
Step 1: Check whether queue is Empty (front == NULL).
Step 2: If it is Empty, then display "Queue is Empty, Deletion is not possible!" and terminate
from the function
Step 3: If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
Step 4: Then set 'front = front → next' and delete 'temp' (free(temp)).
Input:
Insert element 7 in the queue
426135
Output:
7426135
Q1. What is the time complexity for insertion in linked list implementation of queue?
Q2. In linked list implementation of queue, if only front pointer is maintained, which of the
following operation take worst case linear time?
a. Insertion
b. Deletion
c. To empty a queue
d. Both a and c
Q3. In linked list implementation of a queue, front and rear pointers are tracked. Which of these
pointers will
change during an insertion into a NONEMPTY queue?
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 57
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q4. In linked list implementation of a queue, front and rear pointers are tracked. Which of these
pointers will change during an insertion into EMPTY queue?
a. Only front pointer
b. Only rear pointer
c. Both front and rear pointer
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 61
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 16
Description: Circular Queue is a linear data structure in which the operations are performed based
on FIFO (First In First Out) principle and the last position is connected back to the first position to
make a circle. It is also called 'Ring Buffer'. In a normal Queue, we can insert elements until
queuebecomes full.
enQueue(value) This function is used to insert an element into the circular queue. In a
circular queue, the new element is always inserted at Rear position.
deQueue() This function is used to delete an element from the circular queue. In a queue,
the element is always deleted from front position.
Q2. What is the major problem with the circular queue implemented using array?
Q3. What differentiates a circular linked list from a normal linked list?
Step1: Create a new node dynamically and insert value into it.
Step 2: Check if front==NULL, if it is true then front = rear = (newly created node)
Step3: If it is false then rare=(newly created node) and rear node always contains the address of the
front node.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 61
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Step 2: If it is empty then display Queue is empty. If queue is not empty then step 3
Step 3:Check if (front==rear) if it is true then set front = rear = NULL else move the front
forward in queue, update address of front in rear node and return the element.
Input:
Insert element 7 in the circular queue
426135
Output:
7426135
Q2. Why it is better to use circular queue while using array implemention?
Q3. How do you count the number of elements in the circular linked list?
Q4. What is the time complexity of searching for an element in a circular linked list?
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 61
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 17
Description: Binary tree is a special data structure used for data storage purposes. A binary tree has
a special condition that each node can have two children at maximum. Binary Search tree exhibits a
special behaviour. A node's left child must have value less than its parent's value and node's right
child must have value greater than it's parent value. A binary tree have benefits of both an ordered
array and a linked list as search is as quick as in sorted array and insertion or deletion operation are
as fast as in linked list.
Root − Node at the top of the tree is called root. There is only one root per tree and one path
from root node to any node.
Parent − Any node except root node has one edge upward to a node called parent.
Child − Node below a given node connected by its edge downward is called its child node.
Leaf − Node which does not have any child node is called leaf node.
Visiting − Visiting refers to checking value of a node when control is on the node.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 61
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Levels − Level of a node represents the generation of a node. If root node is at level
0, then its next child node is at level 1, its grandchild is at level 2 and so on.
keys − Key represents a value of a node based on which a search operation is to be carried
out for a node.
Basic Operations
The basic operations that can be performed on binary search tree data structure, are following −
then remove x
Q4. Write the pre-, post- and in-order traversal for the given tree.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 64
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted. First
locate its proper location. Start search from root node then if data is less than key value, search empty
location in left subtree and insert the data. Otherwise search empty location in right subtree and insert
the data.
If root is NULL
return
else
endwhile
insert data
end If
Input:
426135
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 64
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Output:
4261357
2 then y z
3 else y Tree-Successor[z]
4. if left[y] NIL
5. then x left[y]
6. else x right[y]
7. if x NIL
9. if p[y] = NIL
14. if y z
16. return y
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 64
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q2. What must be the ideal size of array if the height of tree is ‘l’?
Q3. What is the time complexity for finding the height of the binary tree?
Q4. What are the children for node ‘w’ of a complete-binary tree in an array representation ?
Q5. What are the advantages and disadvantages of linked list representation of binary trees over
arrays?
Q6. What is the time complexity of pre-order traversal in the iterative fashion?
Q7. What is the space complexity of the post-order traversal in the recursive fashion? (d is the tree
depth and n is the number of nodes)
Q8. In a full binary tree if number of internal nodes is I, then number of leaves L are?
Q9. In a full binary tree if number of internal nodes is I, then number of nodes N are?
Q10. In a full binary tree if there are L leaves, then total number of nodes N are?
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 65
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 18
Description:Binary search is a fast search algorithm with run-time complexity of Ο(log n). These
search algorithms works on the principle of divide and conquer. For this algorithm to work properly
the data collection should be in sorted form. Binary search search a particular item by comparing the
middle most item of the collection. If match occurs then index of item is returned. If middle item is
greater than item then item is searched in sub-array to the right of the middle item other wise item
is search in sub-array to the left of the middle item. This process continues on sub-array as well until
the size of subarray reduces to zero.
Q1. What is the difference between linear search and binary search?
node->left=insert(node->left,key);
elseif(key>node->key)
node->right=insert(node,right->key);
Input:
157289
Elements to be found: 8
Output:
Elements found at position no. 5
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 66
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q2. What is the worst case time complexity of search and insert operations in a BST?
Q3. What is the speciality about the inorder traversal of a binary search tree?
Q4. How will you find the minimum element in a binary search tree?
Q5. How will you find the maximum element in a binary search tree?
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 70
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 19
Description:Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) traversing always start from the root (head) node.
That is, why random access operation is not possible. There are three ways which we use to traverse
a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally we traverse a tree to search or locate given item or key in the tree or to print all the
values it contains.
Inorder Traversal:
In this traversal method, the left left-subtree is visited first, then root and then the right sub-tree. We
should always remember that every node may represent a subtree itself. If a binary tree is traversed
inorder, the output will produce sorted key values in ascending order.
We start from A, and following in-order traversal, we move to its left subtree B.B is also traversed
in-ordered. And the process goes on until all the nodes are visited. The output of in-order traversal
of this tree will be −
D→B→E→A→F→C→G
Preorder Traversal:
In this traversal method, the root node is visited first, then left subtree and finally right sub-tree.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 70
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-ordered. And the process goes on until all the nodes are visited.
The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Postorder Traversal:
In this traversal method, the root node is visited last, hence the name. First we traverse left subtree,
then right subtree and finally root.
We start from A, and following pre-order traversal, we first visit left subtree B.B is also traversed
post-ordered. And the process goes on until all the nodes are visited. The output of post-order
traversal of this tree will be −
D→E→B→F→G→C→A
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 70
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q3. What is common in three different types of traversals (Inorder, Preorder and Postorder)?Q4.
What are the tasks performed during Postorder traversal?
Input:
4261357
Output:
In-Order traversal: 4 2 6 1 3 5 7
Pre-Order traversal: 4 2 1 3 6 5 7
Post-Order traversal:1 3 2 5 7 6 4
Q1. Which traversal algorithm traverses all the nodes of a binary tree in a sorted manner?
Q2.What is the time complexity of all traversing algorithms?
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 70
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q3. Which of the following pairs of traversals is not sufficient to build a binary tree from the given
traversals?
(A) Preorder and Inorder
(B) Preorder and Postorder
(C) Inorder and Postorder
(D) None of the Above
Q4. What does the following function do for a given binary tree?
int fun(struct node *root)
{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 0;
return 1 + fun(root->left) + fun(root->right);
}
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 72
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 20
Aim: To find Minimum Cost Spanning Tree of a given undirected graph using Kruskal’s
Algorithm.
Algorithm:
Step 2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If
cycle is not formed, include this edge. Else, discard it.
Step 3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Input:
Output:
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 72
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q1. What is the time complexity of Kruskal’s algorithm and Prim’s algorithm.
Q2. What is the difference between Prim’s and Kruskal’s algorithms.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 21
Description: Dijkstra's algorithm solves the single-source shortest path problem when all
edges have nonnegative weights. It is a greedy algorithm and similar to Prim's algorithm.Algorithm
starts at the source vertex, s, it grows a tree, T, that ultimately spans all vertices reachable from S.
Vertices are added to T in order of distance i.e., first S, then The vertex closest to S, then the next cl
osest, and so on. Following implementation assumes that graph G is represented by adjacency lists.
Q1. Dijkstra algorithm can take into account the negative edge weigthts.'Is the statement true?
Q2. What is a negative weight cycle?
Algorithm:
Input:
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Output:
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 22
These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits
over the larger one. There are other variations of the puzzle where the number of disks increase,
but the tower count remains the same.
Rules: The mission is to move all the disks to some another tower without violating the sequence of
arrangement. A few rules to be followed for Tower of Hanoi are −
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
Following is an animated representation of solving a Tower of Hanoi puzzle with three disks.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation
shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps.
Q1. Which data structure can be used suitably to solve the Tower of Hanoi problem?
a) Tree
b) Heap
c) Priority queue
d) Stack
Algorithm:
o write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem with lesser
amount of disks, say → 1 or 2. We mark three towers with name, source, destination and aux (only
to help moving the disks). If we have only one disk, then it can easily be moved from source to
destination peg.
If we have 2 disks −
First, we move the smaller (top) disk to aux peg.
Then, we move the larger (bottom) disk to destination peg.
And finally, we move the smaller disk from aux to destination peg.
So now, we are in a position to design an algorithm for Tower of Hanoi with more than two disks.
We divide the stack of disks in two parts. The largest disk (nth disk) is in one part and all other (n-
1) disks are in the second part.
Our ultimate aim is to move disk n from source to destination and then put all other (n1) disks onto
it. We can imagine to apply the same in a recursive way for all given set of disks.
The steps to follow are −
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
START
IF disk == 1, THEN
ELSE
END IF
END Procedure
STOP
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 23
Aim: Write a Program to convert from Infix to Prefix and Postfix representation of an
expression.
3 a ∗ (b + c) ∗a+bc abc+∗
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Q2. Which data structure is used to convert infix notations to pre and postfix notations?
Algorithm to convert an infix notation to Postfix notation:
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
…..3.1 If the precedence of the scanned operator is greater than the precedence of the
operator in the stack(or the stack is empty), push it.
…..3.2 Else, Pop the operator from the stack until the precedence of the scanned operator is
less-equal to the precedence of the operator residing on the top of the stack. Push the scanned
operator to the stack.
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop and output from the stack until an ‘(‘ is encountered.
6. Repeat steps 2-6 until infix expression is scanned.
7. Pop and output from the stack until it is not empty.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
EXPERIMENT - 24
Description: Prefix and Postfix expressions can be evaluated faster than an infix expression. This is
because we don’t need to process any brackets or follow operator precedence rule. In postfix and
prefix expressions which ever operator comes before will be evaluated first, irrespective of its
priority. Also, there are no brackets in these expressions. As long as we can guarantee that a valid
prefix or postfix expression is used, it can be evaluated with correctness.
Postfix Evaluation Algorithm:
4. Start scanning the string from the right one character at a time.
6. If it is an operator, pop opnd1, opnd2 and perform the operation, specified by the operator.
Push the result in the stack.
Input : -+8/632
Output : 8
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
Input: 231*+9-
Output : -4
Q1. Evaluate the postfix notation 2 3 1 * + 9 – using the implemented algorithm. Show all the steps.
Q2. Evaluate the postfix notation - + 8 / 6 3 2 using the implemented algorithm. Show all the steps.
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
References
1. Aaron M. Tenenbaum, Yedidyah Langsam and Moshe J. Augenstein, “Data Structures Using C
and C++”,PHI Learning Private Limited, Delhi India
2. Horowitz and Sahani, “Fundamentals of Data Structures”, Galgotia Publications Pvt Ltd DelhI
India.
3. Lipschutz, “Data Structures” Schaum’s Outline Series, Tata McGraw-hill Education (India) Pvt.
Ltd.
6. Rajesh K. Shukla, “Data Structure Using C and C++” Wiley Dreamtech Publication.
7. Michael T. Goodrich, Roberto Tamassia, David M. Mount “Data Structures and Algorithms in
C++”,Wiley India.
9. R. Kruse etal, “Data Structures and Program Design in C”, Pearson Education.
10. Berztiss, AT: Data structures, Theory and Practice, Academic Press.
11. Jean Paul Trembley and Paul G. Sorenson, “An Introduction to Data Structures with
applications”,McGraw Hill.
12. Adam Drozdek “Data Structures and Algorithm in Java”, Cengage Learning
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84
JSS Academy of Technical Education – NOIDA
Department of Computer Science and Engineering
APPENDIX
AKTU SYLLABUS
2. Implementing Searching and Hashing Techniques: Linear search, Binary search, Methods for
Hashing: Modulo Division, Digit Extraction, Fold shift, Fold Boundary, Linear Probe for Collision
Resolution. Direct and Subtraction hashing
5. Implementing Linked List: Singly Linked Lists, Circular Linked List, Doubly Linked Lists :
Insert, Display, Delete, Search, Count, Reverse(SLL), Polynomial , Addition , Comparative study of
arrays and linked list
6. Implementing Trees: Binary search tree : Create, Recursive traversal: preorder, post order, in
order, Search Largest , Node, Smallest Node, Count number of nodes, Heap: Min Heap, Max Heap:
reheap Up, reheap Down, Delete , Expression Tree, Heapsort
7. Implementing Graphs: Represent a graph using the Adjacency Matrix, BFS, Find the minimum
spanning tree (using any method Kruskal’s Algorithm or Prim’s Algorithm) Self Learning
Data Structure Lab (BCS 351) Manual (CS, III Sem) Page | 84