DSA LinkedList
DSA LinkedList
AND ALGORITHMS –
LINKED LIST
Dr. Muhammad Awais Sattar
Assistant Professor RSCI
1
LINKED LIST
“A linked list is a linear collection of data elements, called
nodes, where the linear order is given by means of
pointers.”
Each node is divided into two parts:
• The first part contains the information of the element and
• The second part contains the address of the next node (link /next
pointer field) in the list.
info next info next info next
list null
LINKED LIST VS LIST
TYPES OF LINKED LISTS
There are four key types of linked lists:
Singly linked lists
Doubly linked lists
Circular linked lists
Circular doubly linked lists
LINKED LIST-NOTATIONS
Node: A node includes two data members: info and
next. The info member is used to store information, and
this member is important to the user. The next member
is used to link nodes to form a linked list.
LINKED LISTS IN MEMORY
Computer Memory
To explain what linked lists are, and how linked lists are
different from arrays, we need to understand some
basics about how computer memory works.
Computer memory is the storage your program uses
when it is running. This is where your variables, arrays
and linked lists are stored.
LINKED LISTS IN MEMORY
Variables in Memory
Let's imagine that we want to store the integer "17" in a
variable myNumber. For simplicity, let's assume the integer
is stored as two bytes (16 bits), and the address in memory
to myNumber is 0x7F30.
0x7F30 is actually the address to the first of the two bytes of
memory where the myNumber integer value is stored.
When the computer goes to 0x7F30 to read an integer value,
it knows that it must read both the first and the second byte,
since integers are two bytes on this specific computer.
LINKED LISTS IN MEMORY
The imageshows how the variable myNumber
= 17 is stored in memory.
The example above shows how an integer
value is stored on the simple, but popular,
Arduino Uno microcontroller.
This microcontroller has an 8 bit architecture
with 16 bit address bus and uses two bytes
for integers and two bytes for memory
addresses.
For comparison, personal computers and
smart phones use 32 or 64 bits for integers
and addresses, but the memory works
basically in the same way.
LINKED LISTS IN MEMORY
Arrays in Memory
To understand linked lists, it is useful to first
know how arrays are stored in memory.
Elements in an array are stored contiguously
in memory. That means that each element is
stored right after the previous element.
The image shows how an array of integers
myArray = [3,5,13,2] is stored in memory.
We use a simple kind of memory here with
two bytes for each integer, like in the
previous example, just to get the idea.
LINKED LISTS IN MEMORY
The computer has only got the
address of the first byte of myArray
To access the 3rd element with code
myArray[2] the computer starts at
0x7F28 and jumps over the two first
integers.
The computer knows that an integer
is stored in two bytes, so it jumps 2x2
bytes forward from 0x7F28 and reads
value 13 starting at address 0x7F32.
LINKED LISTS IN MEMORY
When removing or inserting elements in an array, every
element that comes after must be either shifted up to
make place for the new element, or shifted down to take
the removed element's place.
Such shifting operations are time consuming and can
cause problems in real-time systems for example.
The image below shows how elements are shifted when an
array element is removed.
LINKED LISTS IN MEMORY
Instead of storing a collection of data as an array, we can create a linked
list.
Linked lists are used in many scenarios, like dynamic data storage, stack
and queue implementation or graph representation, to mention some of
them.
A linked list consists of nodes with some sort of data, and at least one
pointer, or link, to other nodes.
A big benefit with using linked lists is that nodes are stored wherever there
is free space in memory, the nodes do not have to be stored contiguously
right after each other like elements are stored in arrays
Another nice thing with linked lists is that when adding or removing nodes,
the rest of the nodes in the list do not have to be shifted.
LINKED LISTS IN MEMORY
The image shows how a linked list can be stored
in memory. The linked list has four nodes with
values 3, 5, 13 and 2, and each node has a
pointer to the next node in the list.
Each node takes up four bytes. Two bytes are
used to store an integer value, and two bytes
are used to store the address to the next node in
the list
LINKED LISTS IN MEMORY
To make it easier to see how the nodes relate to each other,
we will display nodes in a linked list in a simpler way, less
related to their memory location, like in the image below:
The first node in a linked list is called the "Head", and the
last node is called the "Tail".
LINKED LISTS IN MEMORY
Unlike with arrays, the nodes in a linked list are not placed right
after each other in memory.
This means that when inserting or removing a node, shifting of
other nodes is not necessary, so that is a good thing.
Something not so good with linked lists is that we cannot access a
node directly like we can with an array by just writing myArray[5]
for example.
To get to node number 5 in a linked list, we must start with the
first node called "head", use that node's pointer to get to the next
node, and do so while keeping track of the number of nodes we
have visited until we reach node number 5.
LINKED LISTS TYPES
There are three basic forms of linked lists:
• Singly linked lists
• Doubly linked lists
• Circular linked lists
LINKED LISTS – SINGLY
A singly linked list is the simplest kind of linked lists. It
takes up less space in memory because each node has
only one address to the next node, like in the image
below.
LINKED LISTS -DOUBLY
A doubly linked list has nodes with addresses to both the
previous and the next node, like in the image below, and
therefore takes up more memory. But doubly linked lists
are good if you want to be able to move both up and
down in the list.
LINKED LISTS - CIRCULAR
A circular linked list is like a singly or doubly linked list
with the first node, the "head", and the last node, the
"tail", connected.
In singly or doubly linked lists, we can find the start and
end of a list by just checking if the links are null. But for
circular linked lists, more complex code is needed to
explicitly check for start and end nodes in certain
applications.
Circular linked lists are good for lists you need to cycle
through continuously.
LINKED LISTS - CIRCULAR
The image below is an example of a singly circular linked
list: