TD DList Corr
TD DList Corr
This list.h header file defines a basic doubly linked list structure and associated function
prototypes for manipulating the list nodes. Let's break down the components:
1. Structure Definition
• Node is a struct type that represents a node in the doubly linked list.
• It contains an integer value (value) and two pointers:
o next (a pointer to the next node).
o prev (a pointer to the previous node).
• The next and prev pointers enable traversal in both directions, making this a doubly
linked list.
2. Function Prototypes
The following function prototypes are defined for performing operations on the doubly linked
list:
These function prototypes provide the necessary functionality to manipulate the doubly linked
list. They allow us to create, modify, and navigate through nodes in both directions. With these
building blocks, we can implement a variety of operations on the doubly linked list, as
demonstrated in the following solutions.
We initialize the doubly linked list by creating the first node and then adding additional nodes,
linking them properly in both directions.
Node* initializeList() {
Node* head = createNode(1); // Create first node
Node* current = head;
for (int i = 2; i <= 5; i++) {
Node* newNode = createNode(i);
setNext(current, newNode); // Link current node to the new node
setPrev(newNode, current); // Set the previous pointer of the new node
current = newNode; // Move current pointer
}
return head; // Return the head of the list
}
We insert a new node at the beginning of the list by adjusting the pointers such that the new node
points to the old head, and the head points to the new node.
This function removes a node from a specified position by adjusting the previous and next
pointers of the surrounding nodes.
if (position == 0) {
*head = getNext(current); // Update head if deleting the first node
if (*head != NULL) {
setPrev(*head, NULL);
}
deleteNode(current); // Delete the node
return;
}
We iterate through the list starting from the head, printing each node's value until we reach the
end.
void traverse_forward(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d -> ", getValue(current)); // Print node value
current = getNext(current); // Move to next node
}
printf("NULL\n"); // Indicate end of list
}
We traverse the list starting from the tail by first moving to the last node and then following the
previous pointers to the head.
We use the two-pointer technique (slow and fast pointers) to find the middle node, where the
slow pointer moves one step at a time, and the fast pointer moves two steps at a time.
We use the Floyd’s Cycle-Finding Algorithm (Tortoise and Hare) to detect a cycle by using two
pointers moving at different speeds.
We traverse the original list and create new nodes with the same values, linking them in a similar
way as the original list.
9. Kth Node from End: Find the K-th Node from the End of the List
We use two pointers: one is moved k steps ahead, and then both pointers move simultaneously
until the first pointer reaches the end, at which point the second pointer points to the K-th node
from the end.
Final Notes:
• This implementation meets the given requirements and follows the same structure and
logic you used for singly linked lists, adapted for doubly linked lists.
• It includes all necessary functionality such as insertion, deletion, traversal, and cycle
detection.