DS Notes New 3.0
DS Notes New 3.0
Introduction to Lists
What is a List?
• A list is a simple way to arrange data in a straight line, like a shopping list.
o It's easy to find data because every position has a memory address.
1. Slow Changes: If you want to add or delete something, you must shift other elements, which
takes time.
2. Fixed Size: Arrays cannot grow or shrink easily. If more space is needed, it's hard to adjust.
Because of these problems, arrays are called static data structures (not flexible).
Linked Lists
o Linked lists use pointers to connect data elements (called nodes), so the data can be
spread out in memory.
Example:
START → [A | ⟶] → [B | ⟶] → [C | ⟶] → [D | NULL]
• The last node points to NULL to show the list ends there.
o Nodes can be stored anywhere in memory and are connected using pointers.
2. Traversal:
o To go through the list, start at the START pointer and follow the arrows (pointers)
until you reach NULL.
3. Empty List:
o If there are no nodes, the START pointer will have the value NULL (this is called an
empty or null list).
Representation of Linked Lists in Memory
2. Special Pointers:
Key Points
• Nodes of a linked list do not need to be stored next to each other in memory.
• You can store multiple linked lists in the same INFO and LINK arrays, but each list must have
its own START pointer.
1. Arrays:
o LINK Array: Points to the index of the next character in the list.
2. Data in Arrays:
3 O 6
4 T 0 (NULL)
6 (Blank) 11
7 X 10
9 N 3
10 I 4
11 E 7
3. START Pointer:
o START = 9: INFO[9] = N.
o LINK[9] = 3: INFO[3] = O.
o LINK[11] = 7: INFO[7] = X.
o LINK[10] = 4: INFO[4] = T.
What is Traversing?
• Traversing means visiting each node in the linked list one by one, in order.
1. Setup:
2. Traversal Logic:
3. End of List:
o Stop when PTR = NULL (this shows you’ve reached the end of the list).
2. Repeat:
Example Walkthrough
2 B 3
3 C 4
4 D 5
5 A 0 (NULL)
START = 2, meaning the first node is at index 2.
Steps to Traverse:
Simplified Algorithm
2. End of List:
3. Initialization:
• Traversing ensures the linked list is processed in order, similar to working with arrays.
Searching a Linked List
What is Searching?
• Searching in a linked list means finding the location of a specific item (called ITEM) in the list.
Types of Searching
o You need to check each node one by one, comparing its value with the ITEM you're
searching for.
o You can stop searching as soon as the current node's value exceeds the ITEM.
o If ITEM is larger than the current node, stop the search (because ITEM cannot exist
later in the list).
o If ITEM > INFO[PTR]: Set LOC = NULL and stop (search unsuccessful).
2 B 3
3 C 4
4 D 0 (NULL)
• Unsorted Lists: You must go through each node one by one until you find the ITEM or reach
the end.
• Sorted Lists: You can stop early if you find a node with a value greater than ITEM.
• Return Location: If the ITEM is found, return the LOC (location); if not, return NULL.
List:
1 B 2
2 D 3
3 A 4
4 C 0 (NULL)
Steps:
Result:
List:
1 A 2
2 B 3
3 C 4
4 D 0 (NULL)
START = 1 (first node points to A).
Steps:
Result:
For both types of lists, if the ITEM is not found, the search will return LOC = NULL.
1. PTR = 1 → INFO[1] = A.
2. PTR = 2 → INFO[2] = B.
3. PTR = 3 → INFO[3] = C.
4. PTR = 4 → INFO[4] = D.
It’s a special list that keeps track of all the unused memory spaces (or cells) that can be used later.
The AVAIL pointer points to the first available unused memory cell.
How it Works:
o When a new node needs to be added to the list, we take an unused cell from the
free-storage list (the cell pointed to by AVAIL).
o After using that cell, we update AVAIL to point to the next available cell.
2. Deleting a Node:
o When a node is deleted, its memory cell is returned to the free-storage list.
o The LINK of the deleted node is updated to point to AVAIL, and then AVAIL is
updated to point to the newly freed node.
Memory Representation
Unused memory cells from these arrays are linked together to form the free-storage list.
Where:
1. Insertion Process:
o Take the first available cell from the free-storage list (pointed to by AVAIL).
2. Deletion Process:
o After deleting a node, add its memory cell back to the free-storage list.
• Prevents Memory Wastage: By reusing memory cells that are no longer needed, the system
avoids wasting memory.
• Dynamic Memory Allocation: It allows the linked list to grow and shrink dynamically without
pre-allocating large memory blocks.
By using the free-storage list, linked lists can efficiently allocate and deallocate memory, ensuring
flexibility and optimal use of resources while handling insertions and deletions.
1. Garbage Collection
Garbage collection is the process of reclaiming unused memory from deleted nodes or lists and
adding it back to the free-storage list for future use.
How It Works:
1. Immediate Reinsertion:
o In linked lists, when a node is deleted, its memory is immediately added back to the
free-storage list (pointed to by AVAIL).
▪ Immediate reinsertion may take too much time, especially for large systems,
so operating systems may perform garbage collection periodically.
1. The system scans all active lists to tag memory cells that are currently in use.
2. It collects unused memory (untagged memory) and adds it to the free-storage list for reuse.
• When the CPU is idle and has time to perform the garbage collection process.
Note: Garbage collection is typically handled automatically by the system, so the programmer does
not need to worry about it.
2. Overflow
What is Overflow?
• Overflow occurs when there is no more available space to insert new nodes. This happens
when the free-storage list (pointed to by AVAIL) is empty.
• When AVAIL = NULL, meaning there are no available memory cells for new nodes, you can:
o Modify the program to allocate more memory space to the arrays if needed.
3. Underflow
What is Underflow?
• Underflow happens when an attempt is made to delete data from an empty data structure.
For linked lists, this occurs when START = NULL, meaning the list is empty and there is no
node to delete.
o Print an "UNDERFLOW" message to notify the user that the list is empty and no
deletion can be performed.
• Garbage Collection: Automatically reclaims unused memory and adds it back to the free-
storage list.
• Overflow: Occurs when the free-storage list is empty, and no new nodes can be added.
• Underflow: Happens when trying to delete a node from an empty linked list.
By handling overflow and underflow situations, and relying on efficient garbage collection, linked lists
can operate smoothly and maintain optimal memory management.
Linked List Insertion and Related Concepts
Insertion in a linked list involves adding a new node at a specific position. The position can be:
NEW := AVAIL
AVAIL := LINK[AVAIL]
3. Store Data in the New Node:
INFO[NEW] := ITEM
Algorithm Steps:
1. Set the LINK of the new node to point to the current first node:
LINK[NEW] := START
START := NEW
Example:
• Before Insertion:
START = 5
AVAIL = 9
• After Insertion:
START = 9
AVAIL = LINK[AVAIL]
The new node is inserted after a specified node (e.g., at location LOC).
Algorithm Steps:
1. Set the LINK of the new node to point to the node originally following LOC:
LINK[NEW] := LINK[LOC]
LINK[LOC] := NEW
Example:
• Before Insertion:
Node 8 points to node 1.
• After Insertion:
The new node is inserted between two nodes A and B such that:
INFO[A] < ITEM ≤ INFO[B].
Steps:
o Traverse the list using a pointer PTR, keeping track of the previous node SAVE.
Example:
• Before Insertion:
Nodes contain: Adams → Dean → Fields → Green → Kirk.
• After Insertion:
1. Empty List:
o The LINK of the new node is set to NULL to mark the end.
3. Pointer Updates:
Summary
Insertion in linked lists is a flexible operation, allowing dynamic addition of nodes. Key considerations
include maintaining proper pointer connections and ensuring efficient memory use through the free-
storage list.
1. Adjust Pointers:
o If N is not the first node: Update the link of the preceding node (A) to point to the
next node (B), bypassing N.
▪ Update LINK[N] to point to the current first node in the AVAIL list.
▪ Update AVAIL to point to N.
Special Cases
o If START = NULL, display an error message like "List is empty" and terminate the
operation.
Deletion Algorithms
Deletes the node N at location LOC, where LOC is the node to be deleted, and LOCP is the location of
the preceding node:
o If LOCP = NULL:
o Call a procedure (e.g., FINDB) to locate LOC (the node containing ITEM) and LOCP
(the preceding node).
o If LOC = NULL, print an error message: "ITEM not in the list" and exit.
3. Perform Deletion:
o Otherwise:
Examples
• Before:
• Delete Node C:
• After:
o Nodes: A → B → D.
o AVAIL points to C.
• Before:
• After:
o List: F.
o AVAIL: Points to E → B → C.
Summary
Deletion from a linked list involves careful pointer adjustments and efficient memory management
via the AVAIL list. Special cases such as deleting the first or last node and handling underflow
conditions are critical for robust implementation.
Header linked lists simplify various operations in linked list implementations by introducing a special
header node at the beginning of the list. This node serves as a fixed reference point, enabling
consistent and uniform algorithm design.
o The last node contains a null pointer, indicating the end of the list.
o Advantages:
o The last node points back to the header node, forming a circular structure.
o Advantages:
Key Characteristics
• Uniformity: Both grounded and circular header lists treat all nodes (including the header)
similarly during operations.
• Circular List Sentinel: The header node in a circular list marks the boundaries of the list.
o All pointers are valid, avoiding the need for special conditions during traversal.
2. Simplified Algorithms:
o The uniform structure eliminates edge cases, especially for the first node.
Algorithms
Steps:
Objective: Locate both a target node (LOC) and its predecessor (LOCP). Steps:
1. Set two pointers: SAVE := START (predecessor) and PTR := LINK[START] (current node).
3. Return:
4. Deleting a Node
o LINK[LOC] := AVAIL.
o AVAIL := LOC.
o The last node points back to the first node instead of being null-terminated.
o Contains both a header and a trailer node, making it easier to handle operations at
both ends of the list.
Practical Applications
1. Buffer Management:
o Circular header lists are commonly used in circular buffers in operating systems.
2. Queue Implementations:
o Efficiently supports enqueue and dequeue without special edge cases for empty
queues.
o Eliminates explicit null pointer checks, allowing smooth traversal through all nodes.
Two-Way Lists:
A two-way list, or doubly linked list, extends the functionality of a one-way list by allowing
bidirectional traversal, making it a versatile data structure for various applications.
Structure of a Two-Way List
Additionally:
Null Pointers
• The FORW field of the LAST node points to NULL, marking the end of the list.
• The BACK field of the FIRST node points to NULL, marking the beginning of the list.
Pointer Properties
• If FORW[LOCA] = LOCB (node A points to node B), then BACK[LOCB] = LOCA (node B points
back to node A).
This ensures:
1. Forward Traversal:
2. Backward Traversal:
o Unlike a one-way list, there’s no need to traverse the list to find the preceding node
during deletion.
o The BACK pointer allows direct access to the previous node, simplifying deletion.
2. Bidirectional Traversal:
o Facilitates both forward and backward navigation, enabling operations like reversing
the list or accessing the last node easily.
3. Flexibility:
Implementation Details
1. Pointer Arrays:
o Two arrays, FORW and BACK, manage the forward and backward links.
o This contrasts with a one-way list, which only uses a single pointer array.
2. List Pointers:
3. AVAIL List:
1. History Management:
2. Navigation Systems:
3. Buffer Management:
4. Dynamic Lists:
o Suitable for managing playlists, where items can be added, removed, or rearranged
easily.
Conclusion
Two-way lists are a highly flexible and powerful data structure, particularly when bidirectional
traversal, efficient deletion, or operations at both ends of the list are required. Their ability to
maintain direct links to both predecessor and successor nodes makes them indispensable in
applications requiring dynamic, real-time data management.