Linked Lists: Data Structures and Algorithms I
Linked Lists: Data Structures and Algorithms I
Linked Lists: Data Structures and Algorithms I
The Concept
Sequentially related information can be stored in non-sequential storage locations
List Nodes are the containers that hold the information
Links are stored with the information to determine the location of the next item
Links are usually array subscripts or pointers
next
In this example, the first node in the list is at 3 first 3 List contents: 1092, 9873, 198, 983,
40932, 76
sub 0 1 2 3 4 5
9822
NULL pointer
next
126
Head
List contents: 10956, 76, 4357, 9822, 126
Self-Referential Structures
A struct or class that contains a member that refers to another object of the same type
Note: class X cannot have a member of type X, but it can have a member of type X* or X& class Node{ ITEM_TYPE data; Node * next; }
Each Node object contains a pointer that can contain the address of another Node object
Array or Dynamic?
Array Implementation User manages free store (in the array)
but more code Array size limits storage
Dynamic Implementation System manages free store (via new and delete) More efficient (faster),
Easy to use Storage is limited only by heap size
Array takes storage Storage used even if not completely proportional to used number of nodes
1 2
3 4
Insert at Beginning
This is the easiest case
It is a special case in a more general insert routine //x to the front of the list! Node * temp; temp = new Node(x); temp->next = head; head = temp; or more simply: head=new Node(x, head);
The new node must point to the address found in head pointer head must then point to the new node
It is customary to have another pointer (besides head) to specify the location of the current list item
Surprisingly, it usually does not point directly to the current item!
Current Pointer
3 5 2 7 1 3 4 1
This idea reduces the complexity of many linked list algorithms Next we look at how you would insert a new node at the current position
2 7
1 3
4 1
//Insert 11/4 at current Node *temp; temp Fraction x(11,4); temp = new Node(x,curr->next); curr->next = temp;
11 4
x 11 4
Illegal Dereferencing
Just remember two things: 1. Never dereference a NULL pointer 2. Never dereference a NULL pointer if (curr == NULL){ temp = new Node(x,head); head = temp; } else { temp = new Node(x,curr->next);
curr->next = temp;
}
Removing Current
3 5
curr
2 7
1 3
4 1
temp
Did we dereference a NULL pointer? Removing the first item is a special case!
List Disposal
Node *temp; //delete all nodes while(head){ temp=head; head=head->next; delete temp; } //head is now NULL
Each Node must be deleted individually A temporary pointer is required for this operation
temp holds the address of the head node while head is set to the next (if any)
Since we do not explicitly store the number of items in the list, it is expensive to determine the current list length!
You could improve efficiency by storing the list size
head
1st
2nd
temp
3rd
Inserting at first location is a special case This is easier than the array implementation!