Pratical Report On Cte 115 Data Structure
Pratical Report On Cte 115 Data Structure
PRATICAL LAB 1
Method/Procedure:
1 Define the Problem: Clearly understand the problem statement.
2 Design the Algorithm: Write down the step-by-step instructions for solving the
problem.
3 Represent the Algorithm: Use pseudocode or a flowchart.
4 Implement the Algorithm: Write the code in a programming language.
5 Test the Algorithm: Run the program with sample inputs and validate the
output.
6 Analyze Efficiency: Check the time and space complexity of the algorithm.
Result:
The algorithm successfully executes the given problem statement, providing the
correct output based on the input values.
Observation:
A well-structured algorithm improves problem-solving efficiency.
The execution time varies based on the complexity of the algorithm.
Optimization of steps can improve algorithm performance.
Precaution:
Ensure that the algorithm follows logical steps.
Avoid infinite loops by ensuring a termination condition.
Validate inputs to prevent errors during execution.
Conclusion:
Algorithms form the foundation of problem-solving in computing. A well-designed
algorithm enhances efficiency, correctness, and performance in programming
applications.
PRATICAL LAB 3
Title: Symbols, Relations, and Graph Representation in Data Structures
Aim: To understand and implement symbols, relations, and graph representation
in data structures.
Materials Required:
Computer with Python/C++/Java
Integrated Development Environment (IDE)
Graph Paper (for manual representation)
Theory:
Symbols and Relations in Data Structures
Symbols are used to represent entities such as nodes, vertices, or elements in a
data structure. Relations define how these symbols interact with each other.
Relation: A relation in data structures establishes a connection between two
elements (e.g., parent-child in trees, adjacency in graphs).
Graph Representation: A graph consists of vertices (nodes) and edges
(connections between nodes). Graphs can be represented using:
Adjacency Matrix: A 2D array representation of a graph.
Adjacency List: A list-based representation for efficient storage.
Edge List: A collection of all edges in a graph.
Diagram:
Method/Procedure:
1 Define Symbols and Relations: Represent different elements using symbols
(e.g., nodes as A, B, C).
2 Graph Construction: Choose a representation (Adjacency Matrix/List).
3 Implement in a Programming Language: Write a program to store and display a
graph.
4 Perform Graph Traversals: Implement Depth First Search (DFS) and Breadth
First Search (BFS).
5 Analyze the Output: Observe how the graph and its relations are stored and
retrieved.
Sample Code (Python - Adjacency List Representation)
class Graph:
def __init__(self):
self.graph = {}
def add_edge(self, u, v):
if u not in self.graph:
self.graph[u] = []
self.graph[u].append(v)
def display_graph(self):
for node, edges in self.graph.items():
print(f"{node} -> {', '.join(map(str, edges))}")
# Example Usage
g = Graph()
g.add_edge('A', 'B')
g.add_edge('A', 'C')
g.add_edge('B', 'D')
g.display_graph()
Result:
The graph was successfully represented using an adjacency list, and its relations
were stored efficiently.
Observation:
The adjacency list is more memory-efficient for sparse graphs.
Graph traversal (DFS/BFS) retrieves nodes based on relations.
Precautions:
Ensure proper initialization of data structures.
Avoid infinite loops in traversal algorithms.
Verify edge connections while adding to the graph.
Conclusion:
This experiment demonstrated how symbols and relations are used in graph
representation. Using adjacency lists and matrices, graphs can be stored and
traversed efficiently in data structures.
PRATICAL LAB 4
Title: Program to Sort the Elements of a Singly Linked List in Linear Order
Aim: To implement a program that sorts the elements of a singly linked list in
linear order using a data structure approach.
Materials Required:
Computer system with a C/C++/Python compiler
Knowledge of linked lists and sorting algorithms
Integrated Development Environment (IDE) such as CodeBlocks, Dev-C++,
or Visual Studio Code
Theory:
A singly linked list is a linear data structure in which each node contains a data
part and a pointer to the next node. Sorting a singly linked list in linear order
requires an efficient sorting algorithm. One such approach is Merge Sort, which
operates in O(n log n) time complexity, or Bubble Sort, which operates in O(n²)
time complexity but is easier to implement.
Sorting can be achieved by modifying the links between nodes rather than
swapping data values, which ensures better memory management and efficiency.
Diagram: sorted nodes of the given singly linked list in ascending order.
Original List
Sorted List
Method/Procedure:
1 Define a Node structure containing a data field and a pointer to the next node.
2 Implement functions to:
Insert elements into the linked list.
Traverse and display the linked list.
Sort the linked list using a sorting algorithm (e.g., Merge Sort or Bubble
Sort).
3 Execute the sorting function and print the sorted linked list.
4 Validate the correctness of the implementation by testing with different inputs.
Result:
The program successfully sorts the elements of the singly linked list in linear
order.
Observation:
The Merge Sort algorithm provides efficient sorting for large lists due to its
O(n log n) complexity.
Bubble Sort works well for smaller lists but is inefficient for larger inputs.
The sorting process can be optimized by modifying the node links instead of
swapping values.
Precaution:
Ensure proper memory allocation and deallocation to prevent memory
leaks.
Validate input values before inserting them into the list.
Check for edge cases such as an empty list or a list with only one element.
Conclusion:
The program successfully implements a sorting algorithm for a singly linked list.
Merge Sort is the preferred approach for better efficiency, while Bubble Sort is
suitable for simpler implementations. The experiment demonstrates the practical
use of sorting algorithms in data structures and highlights the importance of
efficient memory management in linked lists.
PRATICAL LAB 5
Title: Concept of Fixed and Variable Length in Data Structures
Aim: To understand and analyze the differences between fixed-length and
variable-length data structures and their impact on memory allocation and data
storage.
Materials Required:
1 Computer system with a programming environment (Python/C++)
2 Sample dataset for testing
3 Text editor or IDE
4 Notebook for observations
Theory:
Data structures are used to store and manage data efficiently. Based on length,
data structures can be classified into:
Fixed-Length Data Structures: These have a predetermined size that does not
change during execution. Examples include arrays and static records.
Variable-Length Data Structures: These can change in size dynamically during
execution. Examples include linked lists and dynamic arrays.
Fixed-length structures provide faster access time but may lead to memory
wastage.
Variable-length structures optimize memory usage but require additional
processing for memory allocation and deallocation.
Diagram:
1 Fixed-Length Data Structure (Array Example)
[ 10 | 20 | 30 | 40 | 50 ]
2 Variable-Length Data Structure (Linked List Example)
[10] -> [20] -> [30] -> [40] -> [50] -> NULL
Method/Procedure:
1 Fixed-Length Structure Implementation:
Declare an array of fixed size.
Store values in the array.
Access elements using index positions.
2 Variable-Length Structure Implementation:
Implement a linked list.
Insert and delete nodes dynamically.
Traverse the list to observe changes in memory allocation.
3 Compare memory usage and execution time for both structures.
4 Record observations based on performance and efficiency.
Result:
Fixed-length structures provide faster access time but may lead to inefficient
memory usage.
Variable-length structures offer better memory utilization but require more
processing for management.
Observation:
Arrays have a fixed memory allocation, leading to potential wastage.
Linked lists dynamically allocate memory, reducing wastage but increasing
overhead.
Variable-length structures provide flexibility but can lead to fragmentation.
Fixed-length structures are more efficient for static datasets, while variable-
length structures work well with dynamic datasets.
Precaution:
Ensure proper memory allocation and deallocation to avoid memory leaks
in dynamic structures.
Use appropriate data structures based on the application's requirements.
Handle index out-of-bound errors when using arrays.
Properly manage pointers when using linked lists to prevent segmentation
faults.
Conclusion:
Both fixed-length and variable-length data structures have their own advantages
and trade-offs. Fixed-length structures provide quick access and predictable
memory usage, while variable-length structures offer flexibility and efficient
memory management. The choice of data structure depends on the nature of the
problem and memory constraints.
PRATICAL LAB 6
Title: Concept of Ordered and Linear List in Data Structure
Aim: To understand the concept of ordered and linear lists in data structures and
implement basic operations on them.
Materials Required:
Computer system with a programming environment (C, C++, Java, or Python)
Basic understanding of arrays and linked lists
Notebook for observations
Theory:
A linear list is a data structure in which elements are arranged sequentially. It
allows traversal in a single direction, from the first element to the last. Examples
include arrays and linked lists.
An ordered list is a type of linear list where elements are arranged based on a
specific order, such as ascending or descending values. Ordered lists ensure that
every new element inserted maintains the predefined order.
Types of Linear Lists:
Array-Based List - Uses a contiguous memory allocation.
Linked List - Uses nodes connected by pointers.
Operations on Ordered and Linear Lists:
Insertion: Adding an element while maintaining order.
Deletion: Removing an element from the list.
Traversal: Visiting each element in sequence.
Search: Finding an element based on value.
Diagram: Array-Based List
Index: 0 1 2 3 4
Data: [10] [20] [30] [40] [50]
Linked List
Head → [10 | *] → [20 | *] → [30 | *] → [40 | *] → [50 | NULL]
Method/Procedure:
1 Implementation of Linear List using an Array:
Declare an array of fixed size.
Insert elements sequentially.
Perform search and deletion operations.
2 Implementation of Ordered List:
Insert elements in sorted order.
Maintain sorting after each insertion or deletion.
Use binary search for efficient searching.
3 Code Implementation Example (Python):
class OrderedList:
def __init__(self):
self.list = []
def insert(self, item):
self.list.append(item)
self.list.sort()
def delete(self, item):
if item in self.list:
self.list.remove(item)
def display(self):
print("Ordered List:", self.list)
# Example Usage
ol = OrderedList()
ol.insert(5)
ol.insert(2)
ol.insert(8)
ol.display()
ol.delete(2)
ol.display()
Result:
The ordered and linear lists were successfully implemented, and operations such
as insertion, deletion, and traversal were demonstrated.
Observation:
Linear lists store elements in sequence but do not maintain any specific
order.
Ordered lists automatically sort elements during insertion.
Searching in an ordered list is more efficient than in an unordered linear
list.
Precautions:
Ensure proper memory allocation for arrays to avoid overflow.
Maintain correct pointer updates in linked lists.
Use efficient sorting techniques for ordered lists.
Conclusion:
The concept of ordered and linear lists was successfully understood and
implemented. Ordered lists provide better search efficiency, whereas linear lists
are simple and widely used.
PRATICAL LAB 7
Title: Ordered List Operations in Data Structures
Aim: To implement and understand various operations on an ordered list, such as
insertion, deletion, and searching.
Materials Required:
Computer with a Python/C++/Java programming environment
Compiler or IDE (e.g., Visual Studio Code, CodeBlocks, PyCharm)
Basic understanding of arrays and linked lists
Theory:
An ordered list is a collection of elements arranged in a specific sequence, usually
in ascending or descending order. Common operations performed on an ordered
list include:
Insertion – Adding an element at the correct position to maintain order.
Deletion – Removing an element while preserving the order.
Searching – Finding an element using techniques like binary search for efficiency.
Ordered lists can be implemented using arrays or linked lists. The choice of
implementation affects time complexity, with array-based implementations
offering direct indexing and linked lists providing dynamic memory allocation.
Diagram: Ordered list with sequentially connected nodes
Method/Procedure:
1 Initialize an ordered list (array or linked list).
2 Insert elements while maintaining the order.
3 Delete elements from the ordered list and adjust positions accordingly.
4 Search for an element using a linear or binary search method.
5 Display the ordered list after each operation.
Result:
The ordered list operations were successfully implemented, and elements were
inserted, deleted, and searched while maintaining order.
Observation:
The insertion operation places elements in their correct order.
Deletion operation ensures the sequence remains ordered after removing
an element.
Binary search is more efficient than linear search for ordered lists.
Precautions:
Ensure elements are inserted in the correct position.
Handle edge cases like inserting at the beginning or end of the list.
Validate input data before performing operations.
Conclusion:
Ordered list operations were successfully implemented, demonstrating the
efficiency of structured data organization. Proper ordering allows optimized
searching and retrieval, making ordered lists useful in applications like databases
and indexing systems.
PRATICAL LAB 8
Title: Operations on Linked List
Aim: To implement and understand various operations on a linked list such as
insertion, deletion, searching, and traversal.
Materials Required:
Computer system with an installed C/C++/Java/Python compiler
Code editor (VS Code, Code::Blocks, etc.)
Basic knowledge of linked lists
Theory:
A linked list is a linear data structure consisting of nodes, where each node
contains two components:
Data: Stores the value
Pointer: Points to the next node in the sequence
Unlike arrays, linked lists are dynamic and allow efficient insertion and deletion
operations. There are three main types of linked lists:
Singly Linked List: Each node points to the next node.
Doubly Linked List: Each node has pointers to both the previous and next nodes.
Circular Linked List: The last node points back to the first node.
The main operations on a linked list include:
Insertion: Adding a node at the beginning, end, or a specific position.
Deletion: Removing a node from the linked list.
Searching: Finding a node based on its value.
Traversal: Visiting each node sequentially.
Diagram:
Method/Procedure:
1. Insertion Operation:
o Create a new node.
o If inserting at the beginning, update the head pointer.
o If inserting at a specific position, traverse to the desired location and
update pointers.
2. Deletion Operation:
o If deleting the first node, update the head pointer.
o If deleting a node from the middle or end, traverse to the previous
node and update the pointer.
3. Searching Operation:
o Start from the head node and traverse through the list.
o If the desired value is found, return its position; otherwise, return
NULL.
4. Traversal Operation:
o Start from the head node and print each node’s data until the last node
is reached.
Result:
Observation:
Conclusion:
Linked lists provide an efficient way to manage dynamic data, with advantages in
insertion and deletion over arrays. However, searching operations can be slower
due to the sequential traversal requirement.