Lecture 4.1
Lecture 4.1
Technology
Data Structures
So …
We look for an alternative implementation.
Linked List
For the array-based implementation:
1. First element is at location 0
2. Successor of item at location i is at location i + 1
3. End is at location size – 1
Fix:
1. Remove requirement that list elements be stored in consecutive
location.
2. But then need a "link" that connects each element to its
successor
Linked
LinkedLists
Lists!!!!
Linked List
Linked list nodes contain
Data part – stores an element of the list
Next part – stores link/pointer to next element
(when no next element, null value)
Implementation Overview
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
class List {
public:
List(void) { head = NULL; } // constructor
~List(void); // destructor
Steps
1. Locate index’th 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
Insertion after the last element
A0 A1 A2
first last
first last
first last
first last
first last
first current
first current
tmp
first current x
tmp
first current x
tmp
first current x
tmp
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;
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;
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;
}
Deleting a node
A0 A1 A2
current
current->next = current->next->next;
Deleting a node
A0 A1 A2
current
A0 A1 A2
current
.
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
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;
}
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;
}
}
Using List
6
int main(void)
7
result
{ 5
List list; Number of nodes in the list: 3
list.InsertNode(0, 7.0); // successful 5.0 found
4.5 not found
list.InsertNode(1, 5.0); // successful 6
list.InsertNode(-1, 5.0); // unsuccessful 5
list.InsertNode(0, 6.0); // successful Number of nodes in the list: 2