Faculty of Information Technology - Computer Science Department 1
Data Structures
Faculty of Information Technology - Computer Science Department 2
Chapter 4
Linked Lists
Faculty of Information Technology - Computer Science Department 3
Outline
Linked Lists – an overview
Types of Linked Lists
Array Versus Linked List
Designing the Node of a Linked List
Singly Unordered Linked Lists
Singly Unordered Linked List Class Implementation
Faculty of Information Technology - Computer Science Department 4
Linked Lists – an overview
A linked list is a sequence of data structures, which are connected together via
links.
Node Node Node
head next next next
Data Items Data Items Data Items
NULL
links
Faculty of Information Technology - Computer Science Department 5
Linked Lists – an overview
A linked list can be visualized as a chain of nodes, where every node points to the next
node.
The linked list has a head pointer, that points to the first node in the list.
Pointer to
the first node
Node Node Node
head next next next
Data Items Data Items Data Items
NULL
Faculty of Information Technology - Computer Science Department 6
Linked Lists – an overview
Each Node carries a data item(s) and a link field called next.
Data Items: are the data stored in the node
next: a link that points to the next node
The last node carries a link as NULL to mark the end of the list.
End of the
Node Node Node list
head next next next
Data Items Data Items Data Items
NULL
Data stored Pointer to
in the node the next node
Faculty of Information Technology - Computer Science Department 7
Types of Linked Lists
Single Linked List − Item navigation is forward only.
Doubly Linked List − Items can be navigated forward and backward.
Faculty of Information Technology - Computer Science Department 8
Types of Linked List
Circular Single Linked List − The last item contains a link to the first element as the next.
Circular Doubly Linked List − Last item contains link to the first element as next and the
first element has a link to the last element as previous.
Faculty of Information Technology - Computer Science Department 9
Array Versus Linked List
Static arrays are structures whose size is fixed at compile time and therefore cannot be
extended or reduced to fit the data set.
A dynamic array can be extended by doubling the size but there is overhead associated
with the operation of copying old data and freeing the memory associated with the old
data structure.
One potential problem of using arrays for storing data is that arrays require a
contiguous block of memory which may not be available if the requested contiguous
block is too large.
The advantages of using arrays are that each element in the array can be accessed very
efficiently using an index.
2
Faculty of Information Technology - Computer Science Department 10
Array Versus Linked List
BUT for applications that can be better managed without using contiguous memory, we
define a concept called “linked lists”.
A linked list is a collection of objects linked together by references from one object to
another object.
By convention, these objects are named nodes.
So the basic linked list is a collection of nodes where each node contains one or more
data fields AND a reference to the next node.
The last node points to a NULL reference to indicate the end of the list.
Faculty of Information Technology - Computer Science Department 11
Array Versus Linked List
The entry point into a linked list is always the first or head of the list.
It should be noted that the head is NOT a separate node, but a reference to the first Node
in the list.
If the list is empty, then the head has the value NULL.
Unlike Arrays, nodes cannot be accessed by an index since the memory allocated for each
individual node may not be contiguous.
We must begin from the head of the list and traverse the list sequentially to access the
nodes in the list.
Insertions of new nodes and deletion of existing nodes are fairly easy to handle where in
the array, insertions or deletions may require adjustment of the array (overhead), but
insertions and deletions in linked lists can be performed very efficiently.
Faculty of Information Technology - Computer Science Department 12
Designing the Node of a Linked List
A node is a class with at least a data field
and a reference to a node of the same type.
A node is called a self-referential
object because it contains a pointer to a variable
of the same type.
Faculty of Information Technology - Computer Science Department 13
Example 1:
Creating the first node
head null
head 0 null
head 10 null
Adding the second node
n 7 null
head 10 n 7 null
head 10 7 null
8
Faculty of Information Technology - Computer Science Department 14
Example 2:
Outputs:
9 15
Faculty of Information Technology - Computer Science Department
Unordered Singly Linked Lists
A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed
in only one direction from the head to the last node.
10
Faculty of Information Technology - Computer Science Department 16
Unordered Singly Linked Lists
Main Operations on Singly Unordered Linked Lists
Check if the list has no elements (isEmpty).
Add a node to the beginning of the list (addFirst).
Add a node to the end of the list (addLast).
Add a node to a specific position on the list (addIn).
Delete a node from the beginning of the list (delFirst).
Delete a node from the end of the list (delLast).
Delete a node with a specific value from the list (delIn).
Sort the linked list elements (Sort) 10
Faculty of Information Technology - Computer Science Department 17
Unordered Singly Linked List Class Implementation
10
Faculty of Information Technology - Computer Science Department 18
Add First Operation
head 10 7 null
n e
null
10 7 null
n ehead
head 10 7 null
n e
head e 10 7 null 11
Faculty of Information Technology - Computer Science Department 19
Add Last Operation
head 10 7 3 null
n e
null
head 3 null
ptr 10 7
10 7 3 null
n e null
12
head 10 7 3 e null
20
Faculty of Information Technology - Computer Science Department
Add In Operation
head 10 3
7 null
n e null
head 10 7 3 null
ptr
ptr
head 10 7 3 null
n e null
13
head 10 7 e 3 null
21
Faculty of Information Technology - Computer Science Department
Delete First Operation
head 10 7 null
head 10 7 null
head 7 null
Faculty of Information Technology - Computer Science Department 22
Delete Last Operation
head 10 7 null
3
ptr
head 10 7 3 null
head 10 7 null
23
Faculty of Information Technology - Computer Science Department
Delete In Operation
head 10 7 3 5
ptr2 ptr1 null
head 10 7 3 5
null
Faculty of Information Technology - Computer Science Department 24
Sort Operation
25
Faculty of Information Technology - Computer Science Department
Exercises
1. Add a method to UnorderedSLL class to print the elements of a linked list.
2. Add a method to UnorderedSLL class to find the maximum element of a linked list.
3. Add a method to UnorderedSLL class to find the sum of all even numbers stored in the
link list.
4. Add a method to UnorderedSLL class to find the number of elements in the link list.
5. In the main class, create an unordered singly linked list and test all methods in
UnorderedSLL class.
Faculty of Information Technology - Computer Science Department 26
Ordered Singly Linked List
For the Ordered linked list, no arbitrary adding is permitted. So, to add a new element
to the list, this element should be inserted in its proper location using the insert(e)
operation.
10
Faculty of Information Technology - Computer Science Department 27