0% found this document useful (0 votes)
3 views

Lecture 3 Linked List

The document provides a comprehensive overview of linked lists, detailing their structure, operations, and types including singly linked lists, doubly linked lists, and circular linked lists. It explains how to add, retrieve, and delete data within these lists, as well as the advantages of linked lists over arrays. Additionally, it discusses applications of linked lists in various software contexts.

Uploaded by

Joshua Dalmas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 3 Linked List

The document provides a comprehensive overview of linked lists, detailing their structure, operations, and types including singly linked lists, doubly linked lists, and circular linked lists. It explains how to add, retrieve, and delete data within these lists, as well as the advantages of linked lists over arrays. Additionally, it discusses applications of linked lists in various software contexts.

Uploaded by

Joshua Dalmas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

BBIT 1201 6/4/2023

Introduction
Linked list is simply a chain of self-referential data nodes.

Data Next Data Next Data Next

Linked Lists • A node is comprised of two items:


o one (or more) attributes of data and
o the pointer to the next data node, commonly referred to as the next
reference.

• The end of the list is defined by a null next reference.

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

Linked List Class List Traversal


It is where the functionality of the linked list is stored (as methods), and It is sometimes necessary to traverse the entire length of the list to
contains a link to the first item of the list (the ‘head’.) perform some function (for example, to count the number of items, or
display summary information.)

class List The head variable is a


{ !! !!
reference to the first item in !!
Node head; the list. Data Next Data Next Data Next

List() The constructor initialises the


{ list by setting the head to
null (an empty list.) Step 1: Step through the list from
head = null; the header node forward.
} List traversal forms the basis of
Step 2: Perform the desired many of the list manipulation
add(); // discussed later The methods provide a way operation at that node. operations such as add, retrieve
find(); // discussed later to use the list. They each
Step 3: Move onto the next node, and delete.
delete(); // discussed later access the structure through
until the end of the list is
} the head reference.
reached.

5 6

1
BBIT 1201 6/4/2023

List Traversal Adding Data


The code below will traverse the entire list, and print out the data Data is added to a linked list by wrapping the data to add into a node, and
contained in each node. then placing that node at the appropriate place in the data structure.
Depending on the circumstances and purpose of the list, there are a
public void traverse() Step 1: Maintain a variable
{ to store the current position
number of places where data may be added:
Node current = head; in the list.

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

Adding Data to the Head Adding Data to the Head


Adding data to the head of a list is the easiest and quickest way in which it The algorithm is simple to translate into source code, as each step
can be done. corresponds with just one simple instruction.

public void addToHead(int data) Step 1: Create a new node to


{ store the given data.
head Data Next Data Next
Node newNode = new Node(data);
Step 2: Set the new node’s
newNode.next = head; next reference to the first node
Data Next
(previous head node).
Step 1: Create a new node to
store the given data. head = newNode; Step 3: Reset the head
} reference to point to the newly
Step 2: Set the new node’s
The order in which the link next reference to the first node.
created node.
manipulations are done are very
Step 3: Reset the head Header
important; they must always be done
from right to left, otherwise data nodes reference to point to the newly head Data Next Data Next
created node.
will be lost.

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

public void addToTail(int data) Retrieving Data


{ Data retrieval consists of traversing the data structure until a matching
node is found. The data portion of the node is then returned (if the data is
Node insert = head; not found, some form of failure signal should be returned instead.)
The whole data node should not be returned, as it is part of the list’s
while (insert.next != null) internal structure.
insert = insert.next;
? ?
Data Next Data Next
Node newNode = new Node(data);
data
newNode.next = insert.next;
Step 1: Traverse the list. If the data keys match, return the
data portion of the node.
Step 2: While traversing,
insert.next = newNode; compare the search ‘key’ value Step 3: If the list is exhausted
with the data in each node. without the data being found,
} return a search failure signal.
13 14

Retrieving Data public int retrieve(int key)


public int retrieve(int key) {
Step 1: Traverse the list.
{ Node current = head;
Node current = head; Step 2: While traversing,
compare the search ‘key’
while (current != null) value with the data in each
{ node.
while (current != null)
if (current.data == key)
If the data keys match,
{
return data;
return the data portion of if (current.data == key)
the node.
current = current.next; return data;
} Step 3: If the list is
exhausted without the data
return -1; being found, return a current = current.next;
} search failure signal.
}
Note: this program assumes that only positive integers are being stored.
This way, the calling program can easily assume that a non-positive answer return -1;
(e.g. -1) is the signal for a failed retrieval attempt.
retrieve (6)
}
15 16

Deleting Data Deleting Data


Deletion is very similar to retrieval. As before, the list is traversed to find public boolean delete (int key) Step 1: Traverse the list,
data matching a given ‘key’ value. However, instead of returning the data, { maintaining both current
the node is to be deleted. Node current = head; and previous references.
Node previous = null;
Step 2: If the search key
The node can be deleted by having the next references ‘jump over’ the
matches the current data
node to delete. To do this, the node before the one to delete must be while (current != null)
node, ‘jump over’ the
known, and as such, the traversal needs to keep track of two references. {
current node, and return
if (current.data == key)
success.
{
? previous.next = current.next; Step 3: If the list is
Data Next Data Next Data Next return true; exhausted without the data
} being found, return a
search failure signal.
previous = current;
Note: a boolean variable
Step 1: Traverse the list, Step 2: If the search key Step 3: If the list is current = current.next;
is used in this code to
maintaining both current matches the current data exhausted without the }
return success (true) and
and previous references. node, ‘jump over’ the data being found, return failure (false).
current node, and return a search failure signal. return false;
success. }
17 18

3
BBIT 1201 6/4/2023

public boolean delete (int key)


{
Node current = head; Doubly-linked lists
Node previous = null;
• Here is a doubly-linked list (DLL):
while (current != null)
{ myDLL
if (current.data == key)
{
a b c
previous.next = current.next;
return true;
}
• Each node contains a value, a link to its successor (if
previous = current; any), and a link to its predecessor (if any)
current = current.next; • The header points to the first node in the list and to
} the last node in the list (or contains null links if the list
is empty)
19 20
return false;
}

DLLs compared to SLLs DLLs compared to SLLs


• Advantages: • Disadvantages:
– Can be traversed in either direction (may be – Requires more space
essential for some programs)
– List manipulations are slower (because more links must be
– Some operations, such as deletion and inserting changed)
before a node, become easier.
– Greater chance of having bugs (because more links must be
• In singly linked list, to delete a node, pointer to the
manipulated)
previous node is needed.
• In DLL, we can get the previous node using previous
pointer.

21 22

Advantages of Circular Linked


Circular Linked List
List
• We can go to any node from any node in the circular
linked list which was not possible in the singly linked list
if we reached the last node.
• It is easy go to head from the last node
• In a circular list, any node can be starting point means
we can traverse each node from any point.

23 24

4
BBIT 1201 6/4/2023

Advantages of Linked Lists Singly Linked List vs Array


• Size of linked lists is not fixed, they can expand and
shrink during run time. Singly linked list Array
• Insertion and deletion operations are fast and easier in Elements are stored in linear Elements are stored in linear
linked lists. order, accessible with links. order, accessible with an
index.
• Memory allocation is done during run-time (no need to
allocate any fixed memory). Do not have a fixed size. Have a fixed size.
• Data Structures like stacks, queues, and trees can
be easily implemented using Linked list.

25 26

Singly Linked List vs Array Applications of Linked List


Arrays Linked Lists

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

Linked Lists: Adding Data to the Tail


Applications of Linked List
• Escalators — Circular linked List.
• Used in switching between applications and programs
(Alt + Tab) in the Operating system (implemented using
Circular Linked List)
• It can be used to implement stacks, queues, graphs, and
trees.
• UNDO, REDO or DELETE operations in a notepad

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

You might also like