Linked List
Linked List
Given an ordered-list:
What happens when we add/delete an element to the list?
1 2 3 4
We can only travel in the direction of the link.
Bogies
root 5 8 10 15
data elements
BOGIE
Trolley
Connector
Next Data
Node
LINKED-LISTS
We can implement this organization by storing the information
and handle to the next element with the other data as follows:-
struct node {
node * next; // handle to next list element
data_type data;
};
Data Next
root 2 5 8 10 15
LINKED-LISTS – TRAVERSAL
void traverse (node * root)
{
node * current = root;
while ( current != ) {
process data at current;
current = next field of current element
}
} root 2 5 8 10 15
current
LINKED-LIST
ADD AN ELEMENT AFTER CURRENT ELEMENT
6
Curr Step 1
new_element = get a new element;
new_Element data field of new_element = data;
root 2 5 8 10 15
Curr Step 2
6
new_Element next field of new_Element =
next field of current;
root 2 5 8 10 15
Curr 6
Step 3
new_ Element next field of current = new_element;
LINKED-LIST – WARNING:
FRAGILE, HANDLE LINKS WITH CARE
7
Step 1 Curr
new_element = get a new element
data field of new_element = data new_element
root 2 5 8 10 15
7
Curr
Step 2
New_element next field of current = new_element;
root 2 5 8 10 15
7
Curr
new_element
LINKED-LIST
DELETE AN ELEMENT AFTER CURRENT ELEMENT
root 5 6 9 12 15
root 5 6 9 12 15
root 5 6 9 12 15
temp
This element is no longer
Curr accessible
GARBAGE
root 2 5 8 10 15
7
Curr
New Element
LINKED LIST - IMPLEMENTATION USING
DYNAMICALLY ALLOCATED STORAGE
delete q; q = NULL; p
What happens to p?
q
p
q = p;
It’s “garbage”!
DANGLING REFERENCES AND
GARBAGE
Dangling references are particularly serious
problem for storage management, as they might
compromise the integrity of the entire run-time
structure during program execution.
Garbage is a less serious but still troublesome
problem. A data object that has become
garbage ties up storage that might otherwise be
reallocated for another purpose.
A buildup of garbage can force the program to
terminate prematurely because of lack of
available free storage.
LINKED LIST - IMPLEMENTATION USING
DYNAMICALLY ALLOCATED STORAGE
struct Node {
int data;
Node *next;
};
Class LinkedList {
private:
Node * root;
public:
LinkedList() { root = NULL; }
void add (int data);
void remove (int data);
void print ();
~LinkedList();
};
void LinkedList:: print()
{
Node *current = root;
while (current != NULL) {
cout << current->data;
current = current->next;
}
root 2 5 8 10 15
current
bool LinkedList::add(int data)
{
Node *temp = new Node;
if (temp == NULL) return false;
else {
temp->data = data;
Node *current, *previous;
current = root;
while (current != NULL && current->data < data;) {
previous = current;
current = current->next;
}
temp -> next = current;
if (current == root) root = temp;
temp
else previous->next = temp;
return true;
} 9
}
root 2 5 8 10 15
Previous Current
void LinkedList::remove(int data)
{
Node *current, *previous;
current = root;
while (current != NULL && current->data < data) {
previous = current;
current = current->next
}
if (current != NULL && current->data == data) {
if (current == root)
root = root -> next;
else
previous->next = current->next;
delete current;
}
}
THE DESTRUCTOR
The destructor is the counterpart of the constructor.
It is a member function that is called automatically when
a class object goes out of scope.
Its purpose is to perform any cleanup work necessary
before an object is destroyed.
Destructors are required for more complicated classes,
where they're used to release dynamically allocated
memory.
~LINKEDLIST() { }
struct Node { int data; Node *next; };
Class LinkedList {private: Node * root; public: … ~LinkedList(); };
root
A
2 5 8 10 15
root
A
2 5 8 10 15
temp1 temp2