Data Structures Lab
Data Structures Lab
Do
Don’ts
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
2|P a g e
Lab Equipment
Hardware:
1. A computer that can execute C/C++ programs.
Software:
1. Windows / UNIX Operating System.
2. Compilers: Dev C++ / GCC
Programming Languages:
1. C
2. C++
Marks Distribution:
Quiz-I Quiz-II Continuous Assessment End Sem
15 15 30 40
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
3|P a g e
Content
Lab V Stack 10
Lab VI Queue 12
Lab IX Graph 18
Lab XI Hashing 23
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
4|P a g e
Objective:
To revise the useful concept of programming prerequisite which will be useful for future
lesions. Structures, union, pointers, 1D array etc. are covered in this lab.
Brief Theory:
Structures: A structure is a user defined data type in C/C++. A structure creates a data type
that can be used to group items of possibly different types into a single type. key
word is used to create a structure.
Syntax of structure:
Data Structure can be defined as the group of data elements which provides an efficient way
of storing and organizing data in the computer so that it can be used efficiently. Some
examples of Data Structures are arrays, Linked List, Stack, Queue, etc. Data Structures are
widely used in almost every aspect of Computer Science i.e., Operating System, Compiler
Design, Artificial intelligence, Graphics and many more.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
5|P a g e
Data Structures are the main part of many computer science algorithms as they enable the
programmers to handle the data in an efficient way. It plays a vital role in enhancing the
performance of a software or a program as the main function of the software is to store and
retrieve the user's data as fast as possible.
Based on how they are stored in the memory data structures are classified as:
Observations/Outcome:
Problems on:
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
6|P a g e
LABORATORY-II (ARRAY)
Objective:
To understand one of the most basic data structures called array. This will help the students
as a pre-requisite for most of the complex data structures.
Brief Theory:
Arrays are defined as the collection of similar types of data items, data items in array are
stored at contiguous memory locations. It is one of the simplest data structures where each
data element can be randomly accessed by using its index number.
Properties of array:
o Each element in an array is of the same data type and carries the same size.
o Elements in the array are stored at contiguous memory locations from which the first
element is stored at the smallest memory location.
o Elements of the array can be randomly accessed since we can calculate the address of
each element of the array with the given base address and the size of the data element.
Observations/Outcomes:
1. The students will learn the basic idea of array data structure and how it is stored in
computer memory.
2. The students will be able to use arrays for programming problems.
Problems on:
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
7|P a g e
Objectives:
To get familiar with the linked list data structure and gain the ability to utilize linked list data
structure to develop other advanced data structure.
Brief Theory:
Linked List is a linear data structure and it is very common data structure which consists of group of
nodes in a sequence which is divided in two parts. Each node consists of its own data field and the
address field. Data field is used to store the data and the address field is used to store the address of
its adjacent nodes. First node of a linked list is called head, and only the address of the first node is
stored to store a linked list. All other node can be accessed from the first node itself by seque ntially
exploring the adjacent node. Basically, linked list forms a chain like structure. called head Linked Lists
are used to create trees and graphs. Structure is used to create a node in linked list.
Unlike array In Linked Lists, the size of the list need not be fixed in advance.
Linked list is dynamic in nature, which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked lists let you insert elements at the beginning and end of the list.
Disadvantage:
One disadvantage of linked list is random access is not possible in case of linked list, to
access any element we have to search it from the head, which lead to complexity of
searching.
Another disadvantage of linked list is memory wastage, each nodes waist some amount of
memory to store the address of its adjacent nodes.
1. Singly Linked List: Singly linked lists contain nodes which have a data part as well as an
address part i.e., next, which points to the next node in the sequence. In singly linked list
from any particular node there is no way to reach its previous node. The operations we
can perform on singly linked lists are insertion, deletion and traversal.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
8|P a g e
2. Doubly Linked List: In a doubly linked list, each node maintains two pointers namely
and which are used to store the previous and next adjacent element
respectively. The first link points to the previous node and the next link points to the
next node in the sequence.
Observations/Outcomes:
Problems on:
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
9|P a g e
Objectives:
To dive deep into linked list data structures, it types and different algorithms related to it.
Brief Theory:
Linked List is a linear data structure and it is very common data structure which consists of group of
nodes in a sequence which is divided in two parts. Each node consists of its own data and the
address of the next node and forms a chain. Linked Lists are used to create trees and graphs
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented. Stacks and queues can be
easily executed.
Linked List reduces the access time.
Linked lists are used to implement stacks, queues, graphs, etc.
Linked lists let you insert elements at the beginning and end of the list.
In Linked Lists we don’t need to know the size in advance.
Circular Linked List: In the circular linked list the last node of the list contains the address of the first
node and forms a circular chain
Observations/Outcomes:
Problems on:
1. Implement the various types of insertions and deletions possible in Circular Linked
List.
2. Compare the working of Single, Doubly and Circular Linked List.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
10 | P a g e
LABORATORY-V (STACK)
Objective:
The aim is to get the basic idea of the Stack data structure and its implementation, along with its
various operations, as well as its various use cases.
Brief Theory:
A stack is an abstract data type (ADT), commonly used in most programming languages. It is named
stack as it behaves like a real-world stack. A real-world stack allows operations at one end only. For
example, we can place or remove a card or plate from top of the stack only. Likewise, 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. Here, the element which is
placed (inserted or added) last is accessed first. In stack terminology, insertion operation is called
PUSH operation and removal operation is called POP operation.
A stack can be implemented by means of Array, Structure, Pointer and Linked-List. Stack can either
be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to implement stack
using arrays which makes it a fixed size stack implementation.
Basic Operations:
To use a stack efficiently we need to check status of stack as well. For the same purpose, the
following functionality is added to stacks;
: Get the top data element of the stack, without removing it.
: Check if stack is full.
: Check whether the stack is empty, and return true or false.
Below given diagram tries to depict push and pop operation of stack:
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
11 | P a g e
Observations/Outcome:
Problems on:
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
12 | P a g e
LABORATORY-VI (QUEUE)
Objectives:
The aim is to build proper intuition of queue, and be able to implement different types of
Queues.
Brief Theory:
A queue is a linear data structure. It's an ordered list that follows the FIFO principle (First In -
First Out). A queue is a structure that has some insertion and deletion constraints. In the case
of Queue, insertion is done from one end, which is referred to as the rear end. The deletion is
carried out by another end, which is referred to as a front end. The technical terminology for
insertion and deletion in Queue are and respectively. It has two
pointers in its structure: a pointer and a pointer, element will be added using rear
pointer and will be deleted using front pointer.
Operations in Queue:
Queues can be implemented in two ways; 1) using array data structure, 2) Using linked list
data structure.
Circular Queue: There is a limitation in the array implementation of Queue. If the rear
reaches to the end position of the Queue, then there might be possibility that some vacant
spaces are left in the beginning which cannot be utilized. So, to overcome those limitations,
the concept of the circular queue was introduced.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
13 | P a g e
A circular queue is similar to a linear queue as it is also based on the FIFO (First in First Out)
principle except that the last position is connected to the first position in a circular queue that
forms a circle. It is also known as a Ring Buffer. A modular function is used to implement
and operation in circular queue.
Priority Queue:
A priority queue is a collection of elements such that each element has been assigned a
priority. The order in which elements are processed comes from the following rules:
1. An element of higher priority is processed before any element of lower priority.
2. Two elements with the same priority are processed according to the order in
which they were added to the queue.
These priority queue can be implemented using arrays and linked lists.
Observations/Outcome:
Problems on:
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
14 | P a g e
LABORATORY-VII (TREE-I)
Objectives:
The aim is to learn about the abstract data type tree, and make students able to implement
tree to solve several real-life problems.
Brief Theory:
Tree is a non-linear data structure in which data are stored in hierarchical manner. Tree can
be defined as a finite set of one or more nodes such that:
Tree Terminologies:
Edge: In a tree data structure, the connecting link between any two nodes is called as
edge. In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of
edges.
Sibling: Nodes which belong to same Parent are called as siblings. E and F are
siblings in the above tree.
Internal Node: In tree data structure node which has at least one child is called as
internal Node. A, B, C, D, E, F, G, H, I, J are the internal node in the above tree.
Level: The number of edges present in a path from a node to the root is called the
level of that node. Level of root is zero, if a node is at level 𝑙, its children are at level 𝑙
+ 1. For example, level of node H is 2.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
15 | P a g e
Height: In a tree data structure, the total number of edges from leaf node to a
particular node in the longest path is called as height of that particular Node. Height
of H is 1.
Tree Traversal: Visiting every node of the tree exactly once is known as traversal. Any
hierarchical data structure like a tree can be traversed in different ways. Tree can be traversed
in three ways; 1) In order traversal in which for every node first left subtree will be visited
then root and then right subtree will be visited, 2) Pre order traversal in which for every
node first root then left subtree then and then right subtree will be visited, 3) Post Order
traversal in which first left subtree, then right subtree and then root will be visited.
Binary Tree: Binary tree is a special kind of tree in which every node can have at most two
children. Binary tree can be represented either using array or linked list. For array
representation if a root node is stored at array index , then its left child will be stored at
index and the right child will be stored at index . For linked list representation
along with data fields every node will contain two pointers, one will be linked with left child
and another will be linked with right child.
Observations/Outcome:
Problems on:
1. Tree representation.
2. Binary tree implementation.
3. Implementation of different tree traversal algorithms.
4. Inserting new elements, deleting existing elements, and updating elements in tree.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
16 | P a g e
LABORATORY-VIII (TREE-II)
Objective:
To understand the concept of binary search tree (BST), heap and height balancing tree like
AVL and B-tree, and develop the ability of using those data structure to solve real-world
problems.
Brief Theory:
Binary search tree: Binary search tree is a binary tree with some restriction. In binary search
tree value of root node will be larger than any node in the left subtree, and will be smaller
than any node in the right subtree. Because of this restriction searching time of an element
will be reduced to 𝑙 .
Almost Complete Binary Tree: A almost complete binary tree is actually a binary tree with
some restriction in which all the levels are completely filled except possibly the last level and
the last level has all keys as left as possible.
Heap: A Heap is a special Tree-based data structure in which the tree is a complete binary
tree. Heap is of two types, i.e., Max-Heap and Min-Heap.
Max-Heap: In a Max-Heap the key present at the root node must be maximum among the
keys present at all of its children. The same property must be recursively true for all sub-trees
in that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among the
keys present at all of its children. The same property must be recursively true for all sub-trees
in that Binary Tree.
Height Balancing Tree: Height balancing tree means the difference of the height of left
subtree and the height of right subtree of any node is not more than 1. While inserting
element in a height balancing tree for any node if the height difference of left and right
subtree exceed 1 then appropriate action will immediately be taken to balance the height. The
advantage of height balancing tree is that in worst case also searching for an element will take
𝑙 time. AVL tree B-Tree is the example of height balancing tree.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
17 | P a g e
Observations/Outcomes:
1. The students will learn about BST, heap, and B-tree data structure.
2. The students will be able to implement and apply these data structure to solve various
computational problems.
Problems on:
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
18 | P a g e
LABORATORY-IX (GRAPHS)
Objectives:
Brief Theory:
Types of Graphs:
1. Directed graph: In directed graph, edges have a specific direction. Edge and
edge have different meaning in case of directed graph.
3. Weighted Graphs: Weights are associated with the edges connecting two vertices.
For example, if the two vertices are two cities, then edge between them may represent
the distance between those two cities. If the nodes represent the two workstations in a
network, then edge between two nodes may represent the cost to reach a workstation
from other one.
4. Unweighted Graphs: Weights are not associated with the edges connecting two
vertices. If there one undirected edge is present between two nodes that simply means
that these two nodes are related.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
19 | P a g e
Adjacency matrix Representation: The graph G can be represented in the form of matrix
representation and the weights associated with the edge from one node (node i) to the other
(node j) forms the entries (Aij) of the matrix A.
Graph Traversal: The graphs can be traversed either by Breadth first search (BFS) or Depth
first search (DFS). In BFS we traverse from the root node to the other nodes in a breadth first
manner and uses queue to go to its next node. Depth first search (DFS) traverses the whole
graph depth-wise and uses stack to get to the next nodes.
Spanning Tree:
A spanning tree of a graph is the subset if a graph which covers all the vertices with
minimum number of possible edges. Spanning tree is used to find the minimum path
connecting all nodes in a graph.
A spanning tree does not have cycles and it cannot be disconnected.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
20 | P a g e
Observations/Outcomes:
Problems on:
1. Implement the adjacency list representation of a graph with m vertices and n edges.
Also check whether a path of a given length exist between any two pair of vertices or
not.
2. Find the minimum spanning tree of an undirected Graph using greedy approach.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
21 | P a g e
Objective:
Brief Theory:
Searching is the method to retrieve an element or data from any data structure. The searching
techniques are broadly classified into:
Linear Search: In Linear search we try to retrieve an element by checking the array
elements in one-by-one fashion. It takes time to perform the searching operation
as it requires the whole array of size to be traversed for checking the elements.
Binary Search: This searching technique requires the array to be sorted before
performing the search operation. The sorted array is given as input, which is divided
into two subarrays of equal size. The element is first searched with the middle
element of the array, if the element is larger than the middle element, then it will lie in
the right sub array else search in the left subarray. This step is performed repeatedly
until the item is found.
The time complexity for Binary search is 𝑙 , where is the size of the array.
Sorting: Sorting refers to arranging the given data in a specified order. It requires the element
to follow a particular order e.g., Roll numbers of students are arranged in ordered way. There
are two different categories in sorting. They are:
Internal Sorting: When all data is placed in memory, then sorting is called
internal sorting.
External Sorting: When all data that needs to be sorted cannot be placed in
memory at a time, the sorting is called External Sorting. External Sorting is used
for massive amount of data.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
22 | P a g e
Selection Sort: Selection sort is a simple sorting algorithm. This sorting algorithm is
an in-place comparison-based algorithm in which the list is divided into two parts, the
sorted part at the left end and the unsorted part at the right end. Initially, the sorted
part is empty and the unsorted part is the entire list. The smallest element is selected
from the unsorted array and swapped with the leftmost element, and that element
becomes a part of the 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 complexities are of where n is the number of
items.
Merge Sort: It follows the Divide and Conquer approach for sorting an array. In
merge sort, an array is first divided into two halves. The two sub arrays are then
sorted and merged to form the complete sorted array. The sorting of subarrays
continues till the number of elements in the sub array reduces to 1.
Quick sort: It follows Divide and Conquer approach to sort the array. Quick sort is a
highly efficient sorting algorithm and is based on partitioning of array of data into
smaller arrays. A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is made and
another array holds values greater than the pivot value. Quicksort partitions an array
and then calls itself recursively twice to sort the two resulting subarrays. This
algorithm is quite efficient for large-sized data sets as its average and worst-case
complexity are , respectively and the best-case complexity is 𝑙
Heap Sort: A heap is a complete binary tree, and the binary tree is a tree in which the
node can have the utmost two children. Heapsort is a popular and efficient sorting
algorithm. Heap sort processes the elements by creating the min-heap or max-heap
using the elements of the given array. Min-heap or max-heap represents the ordering
of array in which the root element represents the minimum or maximum element of
the array. The concept of heap sort is to eliminate the elements one by one from the
heap part of the list, and then insert them into the sorted part of the list. Heapsort is
the in-place sorting algorithm. The time complexity of heap sort is 𝑙
Observations/Outcome:
Problems on:
1. Write a program to sort the elements of the given array in descending order using the
logic of algorithm. Always select the median element of the array as pivot.
2. Implement Binary search for an array & check whether the given element is present in
the array
3. Modify the merge procedure of merge sort to perform the merging of 3 sorted arrays
into a single array.
4. Write a function which sorts the numbers in array using the heapsort algorithm.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22
23 | P a g e
LABORATORY-XI (HASHING)
Objective:
Brief Theory:
Hashing is done for the faster access of elements. In hashing a given key is converted to a
smaller value by the help of hash function, and then the given key is stored at the index .
Basically, the goal of the hash function is determining the hash value of a particular key, and
then that hash value will be used as index to store that particular key. Generally, the hash
function are simple modular functions, which can determine the hash value at hence
the element can be stored and access in time using hashing. Below picture is depicting a
hashing scheme.
Collision: If the hash value calculated by hah function is same for more than one key then
those keys are indexed at same slot, this is known as collision. That collision needs to be
handled properly; few collision handling techniques are:
1. Linear Probing.
2. Double Hashing.
3. Quadratic Hashing.
In the hashing scheme shown in the above picture if key value 15 is need to inserted now,
then that will lead to collision because 15 will also map to index 0.
Observations/Outcome:
1. Student will learn how to stores data efficiently so that those data can be accessed in
very efficient manner.
Problems on:
2. Hashing Techniques.
3. Linear probing, double hashing, and quadratic hashing to resolve the collision.
Data Structures Lab (CSC 204) Dept. of CSE, IIT (ISM) Dhanbad Monsoon 2021-22