5 - Linked List C++
5 - Linked List C++
C++
struct NodeType
{
char info[4];
NodeType *link;
};
1. Menelusuri list
2. Menyisipkan sebuah node ke list
3. Menghapus sebuah node dari list
4. Mengkopi list
List Overview
• Linked lists
– Abstract data type (ADT)
• Basic operations of linked lists
– Insert, find, delete, print, etc.
• Variations of linked lists
– Circular linked lists
– Doubly linked lists
Linked Lists
A B C
Head
data pointer
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
};
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
• Steps
index’th
1. Locate index’th element element
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
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;
}
Linked Lists / Slide 18
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
Linked Lists / Slide 19
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;
}
Linked Lists / Slide 20
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;
}
Linked Lists / Slide 21
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;
}
Linked Lists / Slide 22
List::~List(void) {
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
// destroy the current node
delete currNode;
currNode = nextNode;
}
}
Linked Lists / Slide 24 6
7 result
5
List list;
list.InsertNode(0, 7.0); // successful
list.InsertNode(1, 5.0); // successful
list.InsertNode(-1, 5.0); // unsuccessful
list.InsertNode(0, 6.0); // successful
list.InsertNode(8, 4.0); // unsuccessful
// print all the elements
list.DisplayList();
if(list.FindNode(5.0) > 0) cout << "5.0 found" << endl;
else cout << "5.0 not found" << endl;
if(list.FindNode(4.5) > 0) cout << "4.5 found" << endl;
else cout << "4.5 not found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
return 0;
}
Linked Lists / Slide 25
A B C
Head
A B C
Head
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.