0% found this document useful (0 votes)
11 views25 pages

DS Notes New 3.0

The document provides an overview of linked lists, explaining their structure, advantages over arrays, and how they are stored in memory. It details the process of traversing, searching, and managing memory allocation through a free-storage list for efficient dynamic memory use. Key concepts include the dynamic nature of linked lists, the role of pointers, and the importance of garbage collection.

Uploaded by

sahil501mulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

DS Notes New 3.0

The document provides an overview of linked lists, explaining their structure, advantages over arrays, and how they are stored in memory. It details the process of traversing, searching, and managing memory allocation through a free-storage list for efficient dynamic memory use. Key concepts include the dynamic nature of linked lists, the role of pointers, and the importance of garbage collection.

Uploaded by

sahil501mulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Linked Lists

Introduction to Lists

What is a List?

• A list is a simple way to arrange data in a straight line, like a shopping list.

• You can add or remove items from a list. For example:

o Add three new items to your shopping list.

o Cross out two old ones.

• This means the list keeps changing as needed.

How Lists Are Stored in Arrays

• Arrays are a common way to store lists:

o Data is saved in a straight line in memory, one after the other.

o It's easy to find data because every position has a memory address.

• Problems with Arrays:

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

What is a Linked List?

A linked list is a way to store and organize data in a straight line.

• Difference from Arrays:

o Arrays store data in one continuous block of memory.

o Linked lists use pointers to connect data elements (called nodes), so the data can be
spread out in memory.

Each node has two parts:

1. Data Part: Holds the information (like a name or number).

2. Pointer Part: Contains the address of the next node.

Structure of a Linked List

Think of a linked list like this:


START → [Data | Pointer] → [Data | Pointer] → [Data | Pointer] → ... → [Data | NULL]

• START: Points to the first node in the list.

• NULL: Indicates the end of the list (last node).

Example:
START → [A | ⟶] → [B | ⟶] → [C | ⟶] → [D | NULL]

• A, B, C, D are the data.

• Arrows (⟶) represent pointers connecting nodes.

• The last node points to NULL to show the list ends there.

Features of Linked Lists

1. Dynamic Memory Allocation:

o Nodes can be stored anywhere in memory and are connected using pointers.

o They don’t need a continuous block of memory like arrays.

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

How is a Linked List Stored in Memory?

1. Two Arrays are Used:

o INFO Array: Stores the actual data (e.g., letters, numbers).

o LINK Array: Stores the pointer (address) of the next node.

2. Special Pointers:

o START: Points to the location (index) of the first node.

o NULL: Indicates the end of the list (often 0).

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.

Example: Storing "NO EXIT" in a Linked List

We’ll store the string "NO EXIT" in memory using arrays:

1. Arrays:

o INFO Array: Holds the characters.

o LINK Array: Points to the index of the next character in the list.

2. Data in Arrays:

Index INFO (Data) LINK (Next Index)

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, meaning the first node starts at index 9.


4. Traversal to Form the String:

o START = 9: INFO[9] = N.

o LINK[9] = 3: INFO[3] = O.

o LINK[3] = 6: INFO[6] = (Blank).

o LINK[6] = 11: INFO[11] = E.

o LINK[11] = 7: INFO[7] = X.

o LINK[7] = 10: INFO[10] = I.

o LINK[10] = 4: INFO[4] = T.

o LINK[4] = 0: NULL, end of the list.

Result: The string "NO EXIT" is formed.

Why Use This Representation?

1. Dynamic Memory: Nodes don’t need to be stored next to each other.

2. Flexibility: Multiple linked lists can share the same arrays.

3. Efficiency: Adding or removing nodes is easier compared to arrays.


Traversing a Linked List

What is Traversing?

• Traversing means visiting each node in the linked list one by one, in order.

• It is done to perform operations like:

o Printing the data.

How Does Traversing Work?

1. Setup:

o Use a pointer variable (PTR) to keep track of the current node.

o Start at the beginning of the list: PTR = START.

2. Traversal Logic:

o Read the data at the current node: INFO[PTR].

o Move to the next node by updating: PTR = LINK[PTR].

3. End of List:

o Stop when PTR = NULL (this shows you’ve reached the end of the list).

Steps for Traversing

1. Initialize: Set PTR = START (start at the first node).

2. Repeat:

o Process INFO[PTR] (e.g., print it).

o Update PTR = LINK[PTR] to move to the next node.

3. Stop: When PTR = NULL, the traversal is complete.

Example Walkthrough

Imagine a linked list with this data:

Index INFO (Data) LINK (Next Node)

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:

1. PTR = START = 2 → Process INFO[2] = B.

2. PTR = LINK[2] = 3 → Process INFO[3] = C.

3. PTR = LINK[3] = 4 → Process INFO[4] = D.

4. PTR = LINK[4] = 5 → Process INFO[5] = A.

5. PTR = LINK[5] = 0 (NULL) → Stop (end of the list).

Simplified Algorithm

1. Initialize: Set PTR = START

2. While PTR is not NULL:

a. Process INFO[PTR] (e.g., print it)

b. Move to the next node: Set PTR = LINK[PTR]

3. Stop when PTR = NULL

Key Points to Remember

1. Dynamic Pointer Updates:

o The pointer (PTR) moves through the list by using LINK[PTR].

2. End of List:

o Traversing stops when PTR = NULL (usually 0).

3. Initialization:

o Properly set up variables like PTR before starting traversal.

Why Is Traversing Important?

• It’s the foundation for other linked list operations like:

o Searching for data.

o Inserting or deleting nodes.

o Summing node values.

• 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

1. Unsorted Linked List

o The data in the list is not in any specific order.

o You need to check each node one by one, comparing its value with the ITEM you're
searching for.

2. Sorted Linked List

o The data is sorted in ascending or descending order.

o You can stop searching as soon as the current node's value exceeds the ITEM.

Searching in an Unsorted List

Steps for Unsorted Search:

1. Start at the first node (START).

2. Compare the value at INFO[PTR] with ITEM.

o If a match is found, return the location of the node (LOC).

o If the list ends without finding ITEM, return LOC = NULL.

Algorithm for Unsorted List:

1. Set PTR = START (start at the first node).

2. While PTR is not NULL:

o If INFO[PTR] = ITEM: Set LOC = PTR (match found) and stop.

o Otherwise, move to the next node: PTR = LINK[PTR].

3. If no match is found, set LOC = NULL (search unsuccessful).


Searching in a Sorted List

Steps for Sorted Search:

1. Start at the first node (START).

2. Compare INFO[PTR] with ITEM:

o If a match is found, return LOC.

o If ITEM is smaller than the current node, continue searching.

o If ITEM is larger than the current node, stop the search (because ITEM cannot exist
later in the list).

Algorithm for Sorted List:

1. Set PTR = START (start at the first node).

2. While PTR is not NULL:

o If ITEM < INFO[PTR]: Move to the next node: PTR = LINK[PTR].

o If ITEM = INFO[PTR]: Set LOC = PTR (match found) and stop.

o If ITEM > INFO[PTR]: Set LOC = NULL and stop (search unsuccessful).

3. If no match is found, set LOC = NULL.

Example for Unsorted List

Let’s say we have a list like this:

Index INFO (Data) LINK (Next Node)

2 B 3

3 C 4

4 D 0 (NULL)

START = 2 (pointing to B).

Searching for "C":

1. PTR = START = 2 → Check INFO[2] = B (not a match).

2. PTR = LINK[2] = 3 → Check INFO[3] = C (match found).

3. Set LOC = 3 and stop.

Key Points to Remember

• 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.

Unsorted Linked List Example

List:

Let's say we have the following list:

Index INFO (Data) LINK (Next Node)

1 B 2

2 D 3

3 A 4

4 C 0 (NULL)

START = 1 (first node points to B).

Task: Search for "C"

Steps:

1. PTR = START = 1 → Check INFO[1] = B (not a match).

2. PTR = LINK[1] = 2 → Check INFO[2] = D (not a match).

3. PTR = LINK[2] = 3 → Check INFO[3] = A (not a match).

4. PTR = LINK[3] = 4 → Check INFO[4] = C (match found).

Result:

• LOC = 4 (C is found at index 4).

Sorted Linked List Example

List:

Now, consider a sorted list:

Index INFO (Data) LINK (Next Node)

1 A 2

2 B 3

3 C 4

4 D 0 (NULL)
START = 1 (first node points to A).

Task: Search for "C"

Steps:

1. PTR = START = 1 → Check INFO[1] = A (not a match).

2. PTR = LINK[1] = 2 → Check INFO[2] = B (not a match).

3. PTR = LINK[2] = 3 → Check INFO[3] = C (match found).

Result:

• LOC = 3 (C is found at index 3).

Searching for an Item Not in the List

For both types of lists, if the ITEM is not found, the search will return LOC = NULL.

Example for Unsorted List (Searching for "E"):

1. PTR = 1 → INFO[1] = B. Not a match.

2. PTR = 2 → INFO[2] = D. Not a match.

3. PTR = 3 → INFO[3] = A. Not a match.

4. PTR = 4 → INFO[4] = C. Not a match.

5. Reached the end of the list: LOC = NULL.

Example for Sorted List (Searching for "E"):

1. PTR = 1 → INFO[1] = A.

2. PTR = 2 → INFO[2] = B.

3. PTR = 3 → INFO[3] = C.

4. PTR = 4 → INFO[4] = D.

5. Reached the end of the list: LOC = NULL.

Memory Allocation in Linked Lists

Memory Allocation in Linked Lists


To manage memory for linked lists efficiently, a Free-Storage List is used to track unused memory
cells. This helps with dynamic memory allocation for inserting and deleting nodes.

Free-Storage List (Free Pool)

What is the Free-Storage List?

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:

1. Inserting a New Node:

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.

Example: Free-Storage List in Linked Lists

Memory Representation

We use two arrays for the linked list:

• INFO[]: Stores the data for each node.

• LINK[]: Stores the pointer to the next node in the list.

Unused memory cells from these arrays are linked together to form the free-storage list.

The structure for maintaining the linked list in memory is as follows:

LIST(INFO, LINK, START, AVAIL)

Where:

• START: Points to the first node of the actual linked list.

• AVAIL: Points to the first node in the free-storage list.

How It Works in Practice:

1. Insertion Process:

o Take the first available cell from the free-storage list (pointed to by AVAIL).

o Update AVAIL to point to the next available cell (i.e., LINK[AVAIL]).


o Use the available cell to store the new node's data and its pointer.

2. Deletion Process:

o After deleting a node, add its memory cell back to the free-storage list.

o Update LINK of the deleted node to point to the current AVAIL.

o Finally, update AVAIL to point to the newly freed node.

Advantages of 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.

Garbage Collection, Overflow, and Underflow in Linked Lists

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).

2. Periodic Garbage Collection (by Operating Systems):

o Why Use This Method?:

▪ Immediate reinsertion may take too much time, especially for large systems,
so operating systems may perform garbage collection periodically.

o Steps in Periodic Garbage Collection:

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 Does Garbage Collection Occur?

• When free space is running low or exhausted.

• 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.

How to Handle Overflow:

• When AVAIL = NULL, meaning there are no available memory cells for new nodes, you can:

o Print an "OVERFLOW" message to alert the user.

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.

How to Handle Underflow:

• When START = NULL, indicating an empty list, you can:

o Print an "UNDERFLOW" message to notify the user that the list is empty and no
deletion can be performed.

Summary of Key Terms:

• 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

Overview of Insertion in a Linked List

Insertion in a linked list involves adding a new node at a specific position. The position can be:

1. At the beginning of the list.

2. After a given node.

3. In a sorted linked list, maintaining order.

Linked lists in memory are represented as:


LIST(INFO, LINK, START, AVAIL)

• INFO: Contains the data of a node.

• LINK: Points to the next node in the list.

• START: Points to the first node of the list.

• AVAIL: Points to the first available (free) node in memory.

General Steps for Insertion

1. Check for Available Space:

o If AVAIL = NULL, there is no free space. Print an OVERFLOW message.

2. Allocate Space for the New Node:

o Use the first available node for the new node:

NEW := AVAIL

AVAIL := LINK[AVAIL]
3. Store Data in the New Node:

o Assign the value to the INFO field of the new node:

INFO[NEW] := ITEM

Case 1: Insertion at the Beginning of the List

The new node becomes the first node.

Algorithm Steps:

1. Set the LINK of the new node to point to the current first node:

LINK[NEW] := START

2. Update START to point to the new node:

START := NEW

Example:

• Before Insertion:

START = 5

AVAIL = 9

• Insert ITEM = 75 at the beginning:

o Use node 9 for the new node.

o Update LINK[9] to point to node 5.

o Update START to point to node 9.

• After Insertion:

START = 9

AVAIL = LINK[AVAIL]

List: 75 → existing nodes

Case 2: Insertion After a Given Node

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]

2. Update the LINK of LOC to point to the new node:

LINK[LOC] := NEW
Example:

• Before Insertion:
Node 8 points to node 1.

• Insert ITEM = Jones into node 10, after node 8:

o Update LINK[10] to point to node 1.

o Update LINK[8] to point to node 10.

• After Insertion:

Node 8 → Node 10 → Node 1

Case 3: Insertion into a Sorted Linked List

The new node is inserted between two nodes A and B such that:
INFO[A] < ITEM ≤ INFO[B].

Steps:

1. Find the Location of Node A:

o Traverse the list using a pointer PTR, keeping track of the previous node SAVE.

o Stop when ITEM ≤ INFO[PTR].

2. Insert the New Node:

o If SAVE = NULL, insert the node at the beginning.

o Otherwise, adjust the LINK fields as in Case 2.

Example:

• Before Insertion:
Nodes contain: Adams → Dean → Fields → Green → Kirk.

• Insert ITEM = Jones:

o Traverse until Green.

o Insert Jones between Green and Kirk.

• After Insertion:

Adams → Dean → Fields → Green → Jones → Kirk

Special Cases in Insertion

1. Empty List:

o If START = NULL, the new node becomes the first node.

o Set LINK[NEW] = NULL.


2. Insertion at the End:

o The LINK of the new node is set to NULL to mark the end.

3. Pointer Updates:

o AVAIL: Always points to the next available node.

o START: Updated only if the node is inserted at the beginning.

o LINK: Adjusted to maintain the list structure.

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.

Deletion from a Linked List

Key Operations in Deletion

When deleting a node (N) from a linked 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.

o If N is the first node: Update START to point to B, effectively removing N.

2. Return Memory to the Free Pool:

o Add the deleted node's memory back to the free-storage list:

▪ Update LINK[N] to point to the current first node in the AVAIL list.
▪ Update AVAIL to point to N.

Special Cases

1. Deleting the First Node:

o Simply update START to point to the second node in the list.

2. Deleting the Last Node:

o Update the LINK field of the preceding node to NULL.

3. Empty List (Underflow):

o If START = NULL, display an error message like "List is empty" and terminate the
operation.

Deletion Algorithms

1. Deleting the Node After a Given Node

Deletes the node N at location LOC, where LOC is the node to be deleted, and LOCP is the location of
the preceding node:

1. Check if LOC is the First Node:

o If LOCP = NULL:

▪ Update START := LINK[START].

2. If LOC is Not the First Node:

o Update LINK[LOCP] := LINK[LOC].

3. Return Node N to Free List:

o Set LINK[LOC] := AVAIL and AVAIL := LOC.

2. Deleting a Node with a Specific ITEM

Deletes the first node N that contains the value ITEM:

1. Find the Node:

o Call a procedure (e.g., FINDB) to locate LOC (the node containing ITEM) and LOCP
(the preceding node).

2. If Node Not Found:

o If LOC = NULL, print an error message: "ITEM not in the list" and exit.

3. Perform Deletion:

o If N is the First Node (LOCP = NULL):


▪ Update START := LINK[START].

o Otherwise:

▪ Update LINK[LOCP] := LINK[LOC].

4. Return Node N to Free List:

o Set LINK[LOC] := AVAIL and AVAIL := LOC.

Examples

1. Deleting a Specific Node

• Before:

o Nodes: A → B → C → D, where START = A and AVAIL = NULL.

• Delete Node C:

o Locate C (LOC) and its predecessor (B).

o Update LINK[B] to point to D.

o Add C to the free list: LINK[C] := AVAIL and AVAIL := C.

• After:

o Nodes: A → B → D.

o AVAIL points to C.

2. Deleting Multiple Nodes

• Before:

o Nodes: E → B → C → F, START = E, AVAIL = NULL.

• Delete Nodes E, B, and C Sequentially:

o After deleting C: Add C to AVAIL.

o After deleting B: Add B to AVAIL, pointing to C.

o After deleting E: Add E to AVAIL, pointing to B.

• 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: Summary and Analysis

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.

Types of Header Linked Lists

1. Grounded Header List:

o The last node contains a null pointer, indicating the end of the list.

o Advantages:

▪ Simplifies traversal since null acts as a natural stopping condition.

2. Circular Header List:

o The last node points back to the header node, forming a circular structure.

o Advantages:

▪ Eliminates null pointers, making traversal easier.

▪ Every node has a predecessor, which simplifies insertion and deletion


algorithms.

Key Characteristics

• Header Node: Acts as a fixed starting point for all operations.

o Grounded List: Empty when LINK[START] = NULL.

o Circular List: Empty when LINK[START] = START.

• 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.

Advantages of Circular Header Lists


1. No Null Pointers:

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

1. Traversing a Circular Header List

Steps:

1. Initialize a pointer: PTR := LINK[START].

2. Loop through the list:

o Repeat until PTR = START.

o Apply any required operation to INFO[PTR].

3. Update pointer: PTR := LINK[PTR].

2. Searching for an Item

Objective: Locate the node containing a specific item (ITEM). Steps:

1. Set PTR := LINK[START].

2. Repeat until PTR = START:

o If INFO[PTR] = ITEM, return LOC := PTR.

3. If ITEM is not found, return LOC := NULL.

3. Finding a Node and Its Predecessor

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).

2. Traverse until PTR = START or INFO[PTR] = ITEM.

3. Return:

o LOC: Location of the target node.

o LOCP: Location of the predecessor.

4. Deleting a Node

Objective: Remove a specific node (LOC) and adjust pointers. Steps:


1. Use the predecessor-finding procedure to locate LOC and LOCP.

2. Update the predecessor's link: LINK[LOCP] := LINK[LOC].

3. Return the deleted node to the free-storage list:

o LINK[LOC] := AVAIL.

o AVAIL := LOC.

Other Linked List Variations

1. Circular Linked List:

o The last node points back to the first node instead of being null-terminated.

o Useful for cyclic data processing (e.g., round-robin scheduling).

2. Header and Trailer List:

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.

3. Efficient Data Traversals:

o Eliminates explicit null pointer checks, allowing smooth traversal through all nodes.

Two-Way Lists:

Two-Way Lists (Doubly Linked Lists): Summary and Analysis

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

Each node in a two-way list contains:

1. INFO: Stores the data.

2. FORW: Points to the next node in the list.

3. BACK: Points to the previous node in the list.

Additionally:

• FIRST: Points to the first node in the list.

• LAST: Points to the last node in the list.

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

A fundamental property of a two-way list is mutual linkage:

• If FORW[LOCA] = LOCB (node A points to node B), then BACK[LOCB] = LOCA (node B points
back to node A).

This ensures:

• Forward and backward traversal are consistent.

• The structure is well-connected in both directions.

Traversal of Two-Way Lists

1. Forward Traversal:

o Start from the FIRST node.

o Use the FORW pointer to move to the next node.

o Continue until you encounter a NULL pointer.

2. Backward Traversal:

o Start from the LAST node.

o Use the BACK pointer to move to the previous node.

o Continue until you encounter a NULL pointer.

Advantages of Two-Way Lists


1. Efficient Deletion:

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:

o Supports insertion and deletion at both ends of the list.

o Enables easier access to both the next and previous nodes.

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:

o FIRST: Points to the first node.

o LAST: Points to the last node.

3. AVAIL List:

o Manages available space using a one-way list structure for simplicity.

o FORW serves as the pointer to manage free nodes.

Example Use Cases

1. History Management:

o Efficient undo/redo operations in applications like text editors.

2. Navigation Systems:

o Supports back and forward traversal in systems such as web browsers.

3. Buffer Management:

o Enables bidirectional buffer processing in real-time applications.

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.

You might also like