DATA STRUCTURE LAB FILE
DATA STRUCTURE LAB FILE
Submitted by
ADESH SINGH
2300950100003
STCS- A
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING
( DEPARTMENT OF SCIENCE & ENGINEERING)
3 Operation on Stack 20 to 23
Push() and Pop()
4 Implementing queue 24 to 27
Using linked list
Page no: 2
AIM: Implementing Sorting Techniques: Bubble sort, Selection sort, Insertion sort,
Quick sort.
BUBBLE SORT works on the repeatedly swapping of adjacent elements until they
are not in the intended order. It is called bubble sort because the movement of array
elements is just like the movement of air bubbles in the water. Bubbles in water rise up
to the surface; similarly, the array elements in bubble sort move to the end in each
iteration.
4. Adaptive Nature: This can stop early if the array becomes sorted before
finishing all iterations.
Page no: 4
INPUT CODE
OUTPUT CODE
INSERTION SORT is a simple sorting algorithm that works by iteratively inserting
each element of an unsorted list into its correct position in a sorted portion of the list.
It is like sorting playing cards in your hands. You split the cards into two groups: the
sorted cards and the unsorted cards. Then, you pick a card from the unsorted group
and put it in the right place in the sorted group.
Page no: 5
1 . Sorting Technique: Insertion Sort is a comparison-based algorithm that
sorts elements by inserting them into their correct position in the sorted
2 Nearly Sorted Data: Insertion Sort is highly effective when the data is
4 Online Sorting: It can be used when data arrives sequentially and must be
2. Efficient for Small Data: For small datasets or nearly sorted data, Insertion
Sort is fast and has a low overhead compared to more complex algorithms.
Page no: 6
3. In-Place Sorting: It does not require additional memory, making it space-
efficient.
worst and average cases, Insertion Sort becomes slow and inefficient for large
datasets.
requires a large number of comparisons and shifts, making it slower than more
advanced sorting algorithms like Quick Sort and Merge Sort.
Page no: 7
IMPLEMENTATON
Page no: 8
OUTPUT CODE
Page no: 9
SELECTION SORT is a comparison-based sorting algorithm. It sorts an array by
repeatedly selecting the smallest (or largest) element from the unsorted portion and
swapping it with the first unsorted element. This process continues until the entire
array is sorted.
4. Stability: Selection Sort is **not stable**. It may change the relative order
of equal elements.
Page no: 10
2. In-Place Sorting: It doesn’t require extra memory beyond the input array.
3. Works Well for Small Arrays: For small datasets, its simplicity can make it
acceptable in terms of performance.
IMPLEMENTATION
Page no: 11
Page no: 12
OUTPUT CODE
2.Time Complexity:
- Best and Average Case: O(n log n)
- Worst Case: O(n 2 ) , occurs when the pivot is the smallest or largest
element repeatedly (e.g., sorted or reverse-sorted data).
3. Space Complexity:
- In-Place Version: O(log n) for recursion stack.
- Non-in-place versions may require additional memory for subarrays.
4. Stability: Quick Sort is not stable ( relative order of equal elements may
change).
5. Adaptive: Quick Sort does not adapt well to sorted or nearly sorted data,
requiring optimizations like random pivot selection.
IMPLEMENTATION
Page no: 14
Page no: 15
AIM : Implementing Searching Techniques: Binary search, linear search
TYPES OF SEARCHING:
1. LINEAR SEARCH: This is the simplest searching algorithm. In linear
search, the algorithm starts at the beginning of the list and checks each
element one by one until the desired element is found or the entire list is
traversed.
- Time Complexity:
- Worst-case: O(n)
- Best-case: O(1) ( if the element is found at the first position ) -
Average-case: O(n)
- Usage: This is used when the data is unsorted or when the dataset is
small and quick results are not critical.
IMPLEMENTATION
Page no: 16
OUTPUT
Page no: 17
IMPLEMENTATION
Page no: 18
OUTPUT
2 . JUMP SEARCH: This search works on sorted arrays and is similar to binary
search but instead of dividing the list in half, it jumps ahead by a fixed step size
(like the square root of the array size).
- Time Complexity: O(sqrt{n})
Page no: 19
AIM : Operations on Stack: Push() and pop()
STACK is a linear data structure that follows the Last In, First Out (LIFO)
principle. In a stack, the element added most recently is the first one to be removed.
You can think of a stack like a stack of plates, where you add new plates to the top
and also remove them from the top.
Page no: 20
STACK REPRESENTATION:
Stacks can be implemented using two types of data structures:
ADVANTAGES OF STACK:
● Simple : Operations are easy to understand and implement.
● Efficient : Constant time complexity for push, pop, and peek operations.
● Memory-efficient : It doesn’t require extra space compared to some other
data structures like linked lists (in array-based stacks).
DISADVANTAGES OF STACK:
● Limited Access: Only the top element can be accessed. If you need to access
elements further down, you have to pop elements off the stack.
● Fixed Size: In array-based stacks, the size is fixed at the time of creation,
leading to a risk of overflow if the stack grows beyond its capacity.
Page no: 21
IMPLEMENTATION
Page no: 22
OUTPUT
Page no: 23
AIM: Implementing queue using linked list.
QUEUE is a linear data structure that follows the First In, First Out (FIFO)
principle. In a queue, the element added first will be the first one to be removed,
similar to a line of people waiting for a service. Elements are added at the rear (tail)
and removed from the front (head).
OPERATIONS IN A QUEUE:
1 . ENQUEUE :
○ Description : Adds an element to the rear of the queue.
2. DEQUEUE :
○ Description: Removes an element from the front of the queue.
○ Operation: Dequeue(queue)
○ Time Complexity: O(1)
○ Note: If the queue is empty, a dequeue operation results in an underflow
condition.
4. ISEMPTY :
○ Description : Checks whether the queue is empty.
○ Operation : IsEmpty(queue)
○ Time Complexity : O(1)
○ Operation : IsFull(queue)
○ Time Complexity : O(1)
TYPES OF QUEUES:
1. Simple Queue :
○ Elements are added at the rear and removed from the front.
Page no: 24
○ Has a fixed size.
2. Circular Queue :
○ A circular queue connects the rear to the front, allowing efficient use of
space.
○ The queue wraps around once the end of the array is reached.
3. Priority Queue :
○ Elements are dequeued based on their priority rather than their order of
arrival.
4. Double-Ended Queue (Deque) :
○ Elements can be added or removed from both ends (front and rear).
APPLICATIONS OF QUEUES:
ADVANTAGES OF QUEUES:
DISADVANTAGES OF QUEUES:
1. Fixed Size: In array-based queues, the size is fixed and can lead to overflow.
2. Limited Access: You can only access the front and rear elements directly.
dequeued and the rear pointer cannot wrap around (solved in circular
queues).
Page no: 25
IMPLEMENTATION
Page no: 26
OUTPUT
Page no: 27
AIM: Implementation of Circular linked list: Insertion and deletion.
LINKED LIST is a linear data structure where elements, called nodes , are
connected through pointers. Unlike arrays, linked lists do not require a contiguous
memory location, making them dynamic in size and efficient for certain operations.
BASIC OPERATIONS:
1. Insertion :
○ Add a new node to the list ( at the beginning, end, or a specific position
).
2. Deletion:
○ Remove a node ( by value, position, or at the beginning or end ).
3. Traversal:
○ Visit each node in the list to display or process data.
4. Search :
○ Find a node containing a specific value.
Page no: 28
2. Efficient Insertion/Deletion : Does not require shifting elements, unlike
arrays.
3. Memory Utilization : Uses only as much memory as required for the data.
DISADVANTAGES OF LINKED LISTS:
1. Extra Memory: Requires additional memory for pointers.
2. Sequential Access: Cannot access elements randomly; must traverse the list.
3. Complex Implementation: More difficult to implement compared to arrays.
Page no: 29
IMPLEMENTATION
Page no: 30
Page no: 31
OUTPUT
Page no: 32
AIM: Traversing and Searching in Trees.
PROPERTIES OF TREES:
1 . Acyclic: Trees do not contain cycles.
2 . Connected: All nodes are connected by edges.
3 . Hierarchical Structure: Represents parent-child relationships.
4 One Root : A tree has exactly one root node.
5 . Unique Path: There is exactly one path between any two nodes.
TYPES OF TREES:
1. Binary Tree :
○ Each node can have at most two children.
○ Common types:
Page no: 33
■ Full Binary Tree: Every node has 0 or 2 children.
■ Complete Binary TreE: All levels except possibly the last are filled, and
nodes are as left as possible.
■ Perfect Binary Tree: All internal nodes have two children, and all
leaves are at the same level.
2. Binary Search Tree (BST) :
○ A binary tree where:
■ Left child nodes have values smaller than the parent.
■ Right child nodes have values greater than the parent.
3. Balanced Tree :
○ A tree where the difference between the heights of left and right subtrees
for any node is at most 1.
4. General Tree :
○ A tree where nodes can have any number of children.
5. AVL Tree :
○ A self-balancing binary search tree.
6. Heap :
○ A complete binary tree where:
■ Max-Heap: Parent nodes are larger than their children.
■ Min-Heap: Parent nodes are smaller than their children.
BASIC OPERATIONS:
1 . Traversal :
○ Visit each node in a specific order:
Page no: 34
○ Remove a node and adjust the tree structure to maintain its properties.
4. Searching :
○ Find a node with a specific value.
5. Height Calculation :
○ Compute the height of the tree.
APPLICATIONS OF TREES:
1. Hierarchical Data Representation :
○ File systems, organizational charts.
2. Searching and Sorting :
○ Binary search trees, heaps.
3. Parsing Expressions :
○ Expression trees in compilers.
4. Networking :
○ Spanning trees in computer networks.
5. Database Management :
○ B-Trees and B+ Trees for indexing.
ADVANTAGES OF TREES:
● Flexible structure for representing hierarchical data.
● Efficient searching, insertion, and deletion in binary search trees.
● Optimal for certain algorithms like Huffman coding.
DISADVANTAGES OF TREES:
● Can become unbalanced, leading to inefficiency in operations.
● Complex to implement compared to simpler data structures like arrays or
linked lists.
Page no: 35
IMPLEMENTATION
Page no: 36
OUTPUT
Page no: 37
AIM: Adjacency Matrix Representation of Graphs ( Directed and Undirected )
1. DIRECTED GRAPH
In a directed graph, edges have a direction and matrix [ i][j] indicates an edge
from vertex i to vertex j.
ADJACENCY MATRIX
IMPLEMENTATION
Page no: 38
OUTPUT
Page no: 39
1. UNDIRECTED GRAPH
In an undirected graph, edges have no direction, and
matrix[i][j]=1 and
matrix[j][i]=1 represent a bidirectional edge between i and j.
ADJACENCY MATRIX:
IMPLEMENTATION
Page no: 40
OUTPUT
Page no: 41
Page no: 42