Week 02 Lecture 02
Week 02 Lecture 02
Instructor:
Hafiz Tayyeb Javed
7. Linked Lists
Insertion(At Start, At End, At Middle Take
Away), Finding will discuss
7-Link Lists 1
Roadmap
• List as an ADT
• An array-based implementation of lists
7-Link Lists 2
Pointers-Based Implementation of Lists
(Linked List)
7-Link Lists 3
Linked List
7-Link Lists 4
Simple Linked List Class (1)
class Node {
public:
double data; // data
Node* next; // pointer to next
};
7-Link Lists 5
Simple Linked List Class (2)
7-Link Lists 6
Simple Linked List Class (3)
Operations of List
7-Link Lists 7
Inserting a New Node Algorithm
• bool Insert(int index, double x)
– Insert a node with data equal to x after the index elements
– If the insertion is successful
➢ Return true
➢ Otherwise, return false
– If index is <= 0 or > length of the list, the insertion will fail
• Steps
1. Locate the element at the index position
2. Allocate memory for the new node, copy data into node
3. Point the new node to its successor (next node)
4. Point the new node’s predecessor (preceding node) to the new node
7-Link Lists 8
Insertion After The Last Element (1)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this
A1 A2 A3
head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 9
Insertion After The Last Element (2)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this
A1 A2 A3
head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 10
Insertion After The Last Element (3)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this
A1 A2 A3
head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 11
Insertion After The Last Element (4)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this
A1 A2 A3 x
head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 12
Insertion After The Last Element (4)
• Suppose last points to the last element of the list
– We can add a new last item x by doing this
A1 A2 A3 x
head last
Steps
• Locate the index element
last->next = new Node(); • Allocate memory for the new node
last = last->next; • Copy data into node
last->data = x; • Point the new node to its successor
last->next = null; (next node)
• Point the new node’s predecessor
(preceding node) to the new node
7-Link Lists 13
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this
A1 A2 A3
head current
A1 A2 A3
head current
Steps
• Locate the index element
• Allocate memory for the new node
• Copy data into node tmp
• Point the new node to its successor tmp = new Node();
(next node) tmp->data= x;
• Point the new node’s predecessor tmp->next = current->next;
(preceding node) to the new node current->next = tmp;
7-Link Lists 15
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this
A1 A2 A3
head current x
Steps
• Locate the index element
• Allocate memory for the new node
• Copy data into node tmp
• Point the new node to its successor tmp = new Node();
(next node) tmp->data= x;
• Point the new node’s predecessor tmp->next = current->next;
(preceding node) to the new node current->next = tmp;
7-Link Lists 16
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this
A1 A2 A3
head current x
Steps
• Locate the index element
• Allocate memory for the new node
• Copy data into node
tmp
• Point the new node to its successor tmp = new Node();
(next node) tmp->data= x;
• Point the new node’s predecessor tmp->next = current->next;
(preceding node) to the new node current->next = tmp;
7-Link Lists 17
Insertion At The Middle (1)
• Suppose current points to the middle element of the list
– We can add a new item x by doing this
A1 A2 A3
head current x
Steps
• Locate the index element
• Allocate memory for the new node
• Copy data into node
tmp
• Point the new node to its successor tmp = new Node();
(next node) tmp->data= x;
• Point the new node’s predecessor tmp->next = current->next;
(preceding node) to the new node current->next = tmp;
7-Link Lists 18
Inserting a New Node (2)
• Possible cases of Insert
1. Insert into an empty list
2. Insert at front
3. Insert at back
4. Insert in middle
7-Link Lists 19
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;
}
7-Link Lists 20
Inserting a New Node (3)
bool List::Insert(int index, double x) {
if (index <= 0) return false;
int currIndex = 2;
Node* currNode = head;
while (currNode && index > currIndex) { Try to locate index’th node.
currNode = currNode->next; If it doesn’t exist, return
currIndex++; false
}
if (index > 1 && currNode == NULL) return false;
int currIndex = 2;
Node* currNode = head;
while (currNode && index > currIndex) { Try to locate index’th node.
currNode = currNode->next; If it doesn’t exist, return
currIndex++; false
}
if (index > 1 && currNode == NULL) return false;
int currIndex = 2;
Node* currNode = head;
while (currNode && index > currIndex) { Try to locate index’th node.
currNode = currNode->next; If it doesn’t exist, return
currIndex++; false
}
if (index > 1 && currNode == NULL) return false;
int currIndex = 2;
Node* currNode = head;
while (currNode && index > currIndex) { Try to locate index’th node.
currNode = currNode->next; If it doesn’t exist, return
currIndex++; false
}
if (index > 1 && currNode == NULL) return false;
int List::Find(double x) {
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode) return currIndex;
return 0;
}
7-Link Lists 25
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;
}
7-Link Lists 26
Any Question So Far?
7-Link Lists 27