






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







What is DS? (Definition) Data Structures and (DS) is a fundamental area of computer science that involves: organizing data in a way that it can be efficiently accessed and modified (data structures), defining procedures or formulas for solving problems (algorithms). Together, data structures and algorithms help make software more efficient and effective. It is done by enabling tasks like managing large amounts of data, searching for information quickly, or even sorting data in a logical order. Understanding DS With Easy Example Data Structure In Google Maps, the entire map is represented as a graph, a data structure that consists of nodes (points like locations, intersections) and edges (routes connecting these points). This structure efficiently manages the complex network of roads and paths, allowing the app to quickly access any location data. Types of Data Structures Data structures are categorized into two types based on their characteristics and the operations they support:
1. Linear Data Structures Arrays: A collection of elements identified by index, stored in continuous memory locations. They are great for quick access but less flexible in terms of resizing. Linked Lists: Composed of nodes that are not stored contiguously but are linked with pointers, making it easy to insert and delete nodes without reorganization. Stacks: These are based on the Last In, First Out (LIFO) principle. They allow adding and removing elements only from the top of the stack, ideal for certain types of data processing, like undo mechanisms in text editors. Queues: Operate on the First In, First Out (FIFO) principle. Elements are added at the back and removed from the front, which is useful for bandwidth management and implementing caches. 2. Non-Linear Data Structures
Trees: Hierarchical data structures with a root value and subtrees of children with a parent node, used for faster retrieval, insertion, and deletion of data organized in a hierarchical manner. Graphs: Consists of nodes (vertices) and edges connecting them, used to represent networks like circuits, paths in cities, or relationships on social media.
1. Primitive Data Structures Primitive data structures are the basic building blocks of data manipulation. These include: Int Represents integer values. Eg. integers (int), which store whole numbers like 42 or -7; int studentCount = 30; Char Used to store single characters.
Queue: Operates on the First In, First Out (FIFO) principle, with elements added at one end and removed from the other. This is ideal for handling tasks like print queue management or task scheduling. II) Non-Linear Data Structures Non-linear structures allow data to be stored in a non-sequential manner, facilitating quick retrieval and linkage based on relationships: Tree: A hierarchical structure consisting of nodes, with each node containing data and references to child nodes, optimizing search, insert, and delete operations. Graph: Composed of vertices (nodes) and edges (connections between nodes), graphs are powerful for modeling complex relationships like networks (social, telecommunications, etc.). Types of Data Structures (Non-Primitive) These data structures can be categorized into two main types: Linear and Non-Linear.
1. Linear Data Structures Linear data structures organize data in a sequential manner, meaning that elements are arranged in a specific order, and each element is connected to its previous and next element. This linear arrangement facilitates easy traversal, insertion, and deletion. 2. Non-Linear Data Structures Non-linear data structures do not organize data in a sequential manner. They are ideal for representing relationships that involve hierarchy or connectivity without a strict linear order. Difference between linear and non-linear data structure Feature Linear Data Structures Non-Linear Data Structures
Organization Data elements are arranged in a sequential, linear order. Data elements are arranged hierarchically or in a network. Examples Arrays, Linked Lists, Stacks, Queues Trees, Graphs Data Access Data is accessed sequentially. Random access is possible in arrays. Data is accessed based on relationships or levels. Traversal Traversal is linear; start at one end and follow through to the other end. Traversal can be complex, such as depth- first or breadth-first in trees and graphs. Memory Allocation Memory is generally allocated at compile time (static) or at runtime but contiguous in memory. Memory is allocated at runtime, often non-contiguously. Insertion/Deletion Simple operations but may involve shifting elements (except in linked lists). Can be complex, depending on the position within the structure (e.g., balancing a tree). Use Cases Suitable for simple data storage and retrieval where data naturally forms a sequence. Suitable for representing complex relationships and hierarchical structures. Efficiency Operations generally require shifting or linear traversal, which can be inefficient. Efficiency depends on the type and complexity of the operations, often optimized with algorithms like balancing or pathfinding. Implementation Typically easier to implement and understand. Often requires more complex algorithms and understanding of the data connections. Linear Data Structures These data structures can be broadly divided into two categories based on how they manage memory: Static and Dynamic.
1. Static Data Structures Static data structures have a fixed size, which means the amount of memory they require is determined at compile time before the program runs.
Fixed Size: Once an array is declared, its size cannot be changed. Adding more elements than the declared size requires creating a new larger array and copying the existing elements to it. Homogeneous Elements: All elements in an array are of the same type. Contiguous Memory Allocation: Elements are stored in contiguous memory locations, which facilitates quick access through indexing. Direct Access: Elements can be accessed directly using their index in constant time.
1. One-Dimensional Arrays (1D Arrays)
a. temperatures = [68, 70, 72, 66, 64, 68, 71] # temperatures for seven days b. print(temperatures[2]) # Access the temperature of the third day
c. This prints 72, which is the temperature on the third day.
This structure allows for efficient insertion and deletion of elements as it avoids the overhead of reallocating or reorganizing the entire data structure. Uses & Applications of Linked Lists Linked lists are used in various scenarios where data dynamically grows and shrinks, making them suitable for applications that require frequent additions and deletions. Some common uses include: Dynamic Memory Allocation : Linked lists are excellent for applications where the memory size needed changes dynamically because they are more flexible than static data structures like arrays. Implementation of Other Data Structures: Linked lists serve as the underlying data structure for other complex data structures such as stacks, queues, and even some types of hash tables. Undo Functionality in Applications: Many applications use linked lists to implement undo functionality by keeping a list of operations or changes that can be reverted one by one. Music and Image Viewers: Linked lists can be used to implement functionalities where users can easily navigate forward or backward through a sequence of songs or images. Polynomial Arithmetic: Linked lists are ideal for storing and manipulating polynomials. Each node can store a coefficient and an exponent, with the list used to dynamically add terms as needed. Characteristics of Linked Lists Dynamic Structure: The size of a linked list can grow or shrink dynamically, as each node is dynamically allocated. Non-contiguous Storage: Each node in a linked list can be stored anywhere in the memory, as each node points to the next node in the list. Single or Double Links: Linked lists can be singly linked (each node points to the next node) or doubly linked (nodes have links to both the next and the previous node), enhancing traversal capabilities. Header Node : Often, a linked list has a header node (or head), which marks the start of the list and does not typically hold any actual data relevant to the application. No Random Access: Linked list elements cannot be accessed randomly or directly. Accessing an element in a linked list requires traversal from the head of the list to the desired position.