Linked List
Linked List
Linked List
1
Linked List
A linked list is a linear data structure, in which
the elements are not stored at continuous
memory locations. The elements in a linked list
are linked using pointers as shown in the below
image:
3
List Using Linked Memory
Various cells of memory are not allocated
consecutively in memory.
Not only to store the elements of the list.
4
List Using Linked Memory
Various cells of memory are not allocated
consecutively in memory.
Not enough to store the elements of the list.
With arrays, the second element was right next
to the first element.
5
List Using Linked Memory
Various cells of memory are not allocated
consecutively in memory.
Not enough to store the elements of the list.
With arrays, the second element was right next
to the first element.
Now the first element must explicitly tell us
where to look for the second element.
6
List Using Linked Memory
Various cells of memory are not allocated
consecutively in memory.
Not enough to store the elements of the list.
With arrays, the second element was right next
to the first element.
Now the first element must explicitly tell us
where to look for the second element.
Do this by holding the memory address of the
second element
7
Linked List
Create a structure called a Node.
object next
8
Linked List
Picture of our list (2, 6, 7, 8, 1) stored as a
linked list:
head
2 6 8 7 1 size=5
current
9
Linked List
Note some features of the list:
Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
10
Linked List
Note some features of the list:
Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
The current here is a pointer, not an index.
11
Linked List
Note some features of the list:
Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
The current here is a pointer, not an index.
The next field in the last node points to nothing.
We will place the memory address NULL which
is guaranteed to be inaccessible.
12
Linked List
Actual picture in memory:
1051 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
13
Linked List Operations
add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9); newNode 9
14
Linked List Operations
add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9); newNode 9
head
2 6 8 7 1 size=5 6
current 2 1
3
9
newNode
15
C++ Code for Linked List
16
Building a Linked List
List list; headNode size=0
17
Building a Linked List
List list; headNode size=0
currentNode
lastcurrentNode
18
Building a Linked List
List list; headNode size=0
currentNode
lastcurrentNode
currentNode
lastcurrentNode
19
Building a Linked List
currentNode
headNode 2 6 8 7 1 size=5
lastcurrentNode
20
C++ Code for Linked List
int get() {
if (currentNode != NULL)
return currentNode->get();
};
21
Analysis of Linked List
add
• we simply insert the new node after the current
node. So add is a one-step operation.
22
Analysis of Linked List
add
• we simply insert the new node after the current
node. So add is a one-step operation.
remove
remove is n step operation, as we need to traverse
the list from the start to keep the track of Last
current node.
Or save the pointer of current node and last current
node then it would be one step operation.
23
Analysis of Linked List
add
• we simply insert the new node after the current node.
So add is a one-step operation.
remove
remove is n step operation, as we need to traverse the
list from the start to keep the track of Last current
node.
Or save the pointer of current node and last current
node then it would be one step operation.
find
worst-case: may have to search the entire list
24