Lecture 3 Linked List
Lecture 3 Linked List
Introduction
Linked list is simply a chain of self-referential data nodes.
1 2
The Node
Introduction The list node is a simple self-referential structure that stores an item of
data, and a reference to the next item.
head: The first node of a linked list
tail: The last node of a linked list - it has a null next reference.
tail class Node
{ The data variable is where the
head int data; information to be stored resides. It may
be of any primitive or reference type
Node next; appropriate for the data.
next next next next
The next variable is the self-referential
public Node(int data) link to the next data item.
element element element element {
this.data = data; The constructor initialises the node
Baltimore Rome Seattle Toronto
this.next = null; object by storing the data that was given
} as an argument, and sets the next
} reference to null.
Such a linked list is called a singly linked list.
3 4
5 6
1
BBIT 1201 6/4/2023
while (current != null) Step 2: Continue stepping • At the start (head) of a list
through the list, until the
{
end of the list (a null • In the middle of the list
System.out.println(current.data);
reference) is reached.
current = current.next; • At the end (tail) of the list
} Step 3: At each step, print
} out the data present in the • At the appropriate place to preserve sort order
current node.
Because of the way a linked list is defined, we can only access data in
one direction, and sequentially (one item after another.)
7 8
Data Next
9 10
Adding Data to the Tail Linked Lists: Adding Data to the Tail
Adding data to the middle or tail of the list is essentially the same process. Step 1: Traverse the list to
The diagram below illustrates adding to the end (tail.) public void addToTail(int data)
{ the desired insertion point (in
Node insert = head; this case, the last item of the
list.)
Header while (insert.next != null) Step 2: Create a new node to
head Data Next Data Next null insert = insert.next; store the given data.
Step 3: Set the new node’s
Node newNode = new Node(data); next reference to that of the
Data Next insertion point node (null in
Step 1: Traverse the list to Step 3: Set the new node’s newNode.next = insert.next; this case).
the desired insertion point next reference to that of the
insert.next = newNode; Step 4: Reset the insertion
(in this case, the last item insertion point node (null in
} point’s next reference to point
of the list.) this case).
to the newly created node.
Step 2: Create a new node Step 4: Reset the insertion
to store the given data. point’s next reference to
point to the newly created
node. 1 Next 2 Next null
11 5 Next 12
2
BBIT 1201 6/4/2023
3
BBIT 1201 6/4/2023
21 22
23 24
4
BBIT 1201 6/4/2023
25 26
Linked List is an ordered collection of elements of the • Images are linked with each other. So, an image viewer
An array is a collection of elements of a similar data
type.
same type in which each element is connected to the
next using pointers.
software uses a linked list to view the previous and the
Array elements can be accessed randomly using the Random accessing is not possible in linked lists. The next images using the previous and next buttons.
array index. elements will have to be accessed sequentially.
New elements can be stored anywhere and a
• Web pages can be accessed using the previous and the
Data elements are stored in contiguous locations in
memory.
reference is created for the new element using next URL links which are linked using a linked list.
pointers.
Insertion and Deletion operations are costlier since the Insertion and Deletion operations are fast and easy in • The music players also use the same technique to
memory locations are consecutive and fixed. a linked list.
switch between music.
Memory is allocated during the compile time (Static Memory is allocated during the run-time (Dynamic
memory allocation). memory allocation).
Size of the array must be specified at the time of array Size of a Linked list grows/shrinks as and when new
declaration/initialization. elements are inserted/deleted.
27 28
29 30
5
BBIT 1201 6/4/2023
Linked Lists: Adding Data Specified Position in List List Traversal: Getting Element at a Specific
Position
31 32
Deleting Data
33