LINKED LISTS
Data Structures and Algorithm
2
Outline
• Linked lists
• Basic operations of linked lists
• Insert, find, delete, print, etc.
• Variations of linked lists
• Circular linked lists
• Doubly linked lists
3
Array Implementation...
• Requires an estimate of the maximum size of
the list
waste space
• printList and find: linear
• findKth: constant
• insert and delete: slow
• e.g. insert at position 0 (making a new element)
• requires first pushing the entire array down one spot to
make room
• e.g. delete at position 0
• requires shifting all the elements in the list up one
• On average, half of the lists needs to be moved for
either operation
4
Pointer Implementation (Linked List)
• Ensure that the list is not stored contiguously
• use a linked list
• a series of structures that are not necessarily adjacent in
memory
Each node contains the element and a pointer to a
structure containing its successor
the last cell’s next link points to NULL
Compared to the array implementation,
the pointer implementation uses only as much space as is needed
for the elements currently on the list
but requires space for the pointers in each cell
5
Linked Lists
A B C
Head
• A linked list is a series of connected nodes
• Each node contains at least
• A piece of data (any type)
• Pointer to the next node in the list
• Head: pointer to the first node
node
• The last node points to NULL
A
data pointer
6
A Simple Linked List Class
• We use two classes: Node and List
• Declare Node class for the nodes
• data: double-type data in this example
• next: a pointer to the next node in the list
class Node {
public:
double data; // data
Node* next; // pointer to next
};
7
A Simple Linked List Class
• Declare List, which contains
• head: a pointer to the first node in the list.
Since the list is empty initially, head is set to NULL
• Operations on List
class List {
public:
List(void) { head = NULL; } // constructor
~List(void); // destructor
bool IsEmpty() { return head == NULL; }
Node* InsertNode(int index, double x);
int FindNode(double x);
int DeleteNode(double x);
void DisplayList(void);
private:
Node* head;
};
8
A Simple Linked List Class
• Operations of List
• IsEmpty: determine whether or not the list is empty
• InsertNode: insert a new node at a particular
position
• FindNode: find a node with a given value
• DeleteNode: delete a node with a given value
• DisplayList: print all the nodes in the list
9
Inserting a new node
• Node* InsertNode(int index, double x)
• Insert a node with data equal to x after the index’th elements.
(i.e., when index = 0, insert the node as the first element;
when index = 1, insert the node after the first element, and so on)
• If the insertion is successful, return the inserted node.
Otherwise, return NULL.
(If index is < 0 or > length of the list, the insertion will fail.)
• Steps
index’th
1. Locate index’th element element
2. Allocate memory for the new node
3. Point the new node to its successor
4. Point the new node’s predecessor to the new node
newNode
10
Inserting a new node
• Possible cases of InsertNode
1. Insert into an empty list
2. Insert in front
3. Insert at back
4. Insert in middle
• But, in fact, only need to handle two cases
• Insert as the first node (Case 1 and Case 2)
• Insert in the middle or at the end of the list (Case
3 and Case 4)
11
Inserting a new node
Node* List::InsertNode(int index, double x) { Try to locate
if (index < 0) return NULL; index’th node. If it
doesn’t exist,
int currIndex = 1;
Node* currNode = head; return NULL.
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = new Node;
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
12
Inserting a new node
Node* List::InsertNode(int index, double x) {
if (index < 0) return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = new Node;
newNode->data = x;
if (index == 0) {
newNode->next = head; Create a new node
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
13
Inserting a new node
Node* List::InsertNode(int index, double x) {
if (index < 0) return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = new Node;
Insert as first element
newNode->data = x;
if (index == 0) { head
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next; newNode
currNode->next = newNode;
}
return newNode;
}
14
Inserting a new node
Node* List::InsertNode(int index, double x) {
if (index < 0) return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
Node* newNode = new Node;
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode; Insert after currNode
}
currNode
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
} newNode
15
Finding a node
• int FindNode(double x)
• Search for a node with the value equal to x in the list.
• If such a node is found, return its position. Otherwise,
return 0.
int List::FindNode(double x) {
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode) return currIndex;
return 0;
}
16
Deleting a node
• int DeleteNode(double x)
• Delete a node with the value equal to x from the list.
• If such a node is found, return its position. Otherwise, return 0.
• Steps
• Find the desirable node (similar to FindNode)
• Release the memory occupied by the found node
• Set the pointer of the predecessor of the found node to the
successor of the found node
• Like InsertNode, there are two special cases
• Delete first node
• Delete the node in middle or at the end of the list
17
Deleting a node
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Try to find the node with
Node* currNode = head; its value equal to x
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
18
Deleting a node
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++; prevNode currNode
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
19
Deleting a node
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
} head currNode
return 0;
}
20
Printing all the elements
• void DisplayList(void)
• Print the data of all the elements
• Print the number of the nodes in the list
void List::DisplayList()
{
int num = 0;
Node* currNode = head;
while (currNode != NULL){
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout << "Number of nodes in the list: " << num << endl;
}
21
Destroying the list
• ~List(void)
• Use the destructor to release all the memory used by the list.
• Step through the list and delete each node one by one.
List::~List(void) {
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
// destroy the current node
delete currNode;
currNode = nextNode;
}
}
22
6
7 result
5
Using List Number of nodes in the list: 3
5.0 found
4.5 not found
6
int main(void) 5
{ Number of nodes in the list: 2
List list;
[Link](0, 7.0); // successful
[Link](1, 5.0); // successful
[Link](-1, 5.0); // unsuccessful
[Link](0, 6.0); // successful
[Link](8, 4.0); // unsuccessful
// print all the elements
[Link]();
if([Link](5.0) > 0) cout << "5.0 found" << endl;
else cout << "5.0 not found" << endl;
if([Link](4.5) > 0) cout << "4.5 found" << endl;
else cout << "4.5 not found" << endl;
[Link](7.0);
[Link]();
return 0;
}
23
Variations of Linked Lists
• Circular linked lists
• The last node points to the first node of the list
A B C
Head
• How do we know when we have finished traversing
the list? (Tip: check if the pointer of the current
node is equal to the head.)
24
Variations of Linked Lists
• Doubly linked lists
• Each node points to not only successor but the
predecessor
• There are two NULL: at the first and last nodes in
the list
• Advantage: given a node, it is easy to visit its
predecessor. Convenient to traverse lists backwards
A B C
Head
25
Array versus Linked Lists
• Linked lists are more complex to code and manage than
arrays, but they have some distinct advantages.
• Dynamic: a linked list can easily grow and shrink in size.
• We don’t need to know how many nodes will be in the list. They are
created in memory as needed.
• In contrast, the size of a C++ array is fixed at compilation time.
• Easy and fast insertions and deletions
• To insert or delete an element in an array, we need to copy to
temporary variables to make room for new elements or close the gap
caused by deleted elements.
• With a linked list, no need to move other nodes. Only need to reset
some pointers.