0% found this document useful (0 votes)
6 views30 pages

DSA LinkedList

The document provides an overview of linked lists, defining them as a linear collection of nodes where each node contains data and a pointer to the next node. It discusses various types of linked lists, including singly, doubly, circular, and circular doubly linked lists, and explains their memory storage, advantages, and operations such as insertion, deletion, and searching. Additionally, it includes code implementation for creating and traversing a singly linked list.

Uploaded by

Sameer Sohail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views30 pages

DSA LinkedList

The document provides an overview of linked lists, defining them as a linear collection of nodes where each node contains data and a pointer to the next node. It discusses various types of linked lists, including singly, doubly, circular, and circular doubly linked lists, and explains their memory storage, advantages, and operations such as insertion, deletion, and searching. Additionally, it includes code implementation for creating and traversing a singly linked list.

Uploaded by

Sameer Sohail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

DATA STRUCTURE

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:

 If we put the same four nodes from the previous example


together using this new visualization, it looks like this:

 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:

 The image below is an example of a doubly circular


linked list:
CREATE SINGLY LINKED LISTS
 One way to create the linked list is to first
generate the node for number 10, then the
node for 8, and finally the node for 50. Each
node must be initialized properly and
incorporated into the list.
• Creation could be done in four steps:
1.
1. In the first step, a new Node is created
2. in the second step, the info member of this node 2.
is set to 10
3.
3. In the third step, the node’s next member is set
to null
4. The fourth step is making p a pointer to the 4.
newly created node
INSERTION SINGLY LINKED LISTS
Adding a node at the beginning of a linked
list is performed in four steps.
 INSERTION IN THE BEGINNING:
• Inserting a node at the beginning of a list is
straightforward:
• First, a new node is created (figure a)
• The info member of the node is initialized
(figure b)
• The next member is initialized to point to the
first node in the list, which is the current value
of head (figure c)
• head is then updated to point to the new node
(figure d)
INSERTION SINGLY LINKED LISTS
 INSERTION AT THE END:
• Inserting a node at the end of a list is likewise
easy to accomplish as illustrated in the next slide:
• The new node is created, and the info member of
the node is initialized (figures a and b)
• The next member is initialized to null, since the
node is at the end of the list (figure c)
• The next member of the current last node is set
to point to the new node (figure d)
• Since the new node is now the end of the list, the
tail pointer has to be updated to point to it (figure
e)
• As before, if the list is initially empty, both head
and tail would be set to point to the new node
DELETION SINGLY LINKED LISTS
 The operation of deleting a node consists of
returning the value stored in the node and releasing
the memory occupied by the node
 Again, we can consider operations at the beginning
and end of the list
 To delete at the beginning of the list, we first
retrieve the value stored in the first node (head →
info)
 Then we can use a temporary pointer to point to
the node, and set head to point to head → next
 Finally, the former first node can be deleted,
releasing its memory
 These operations are illustrated in figure 3.6(a) – (c)
DELETION SINGLY LINKED LISTS
 Two special cases exist when carrying out this deletion
 The first arises when the list is empty, in which case the
caller must be notified of the action to take
 The second occurs when a single node is in the list,
requiring that head and tail be set to null to indicate the
list is now empty
DELETION SINGLY LINKED LISTS
 Deleting at the end of a list requires additional
processing
 This is because the tail pointer must be backed up to
the previous node in the list
 Since this can’t be done directly, we need a
temporary pointer to traverse the list until tmp →
next = tail
 This is illustrated in figures (a) – (c) in the next slide
 Once we have located that node, we can retrieve
the value contained in tail → info, delete that node,
and set tail = tmp
 This is illustrated in figures (d) – (f) in the next slide
DELETION SINGLY LINKED LISTS
 The same special cases apply to deleting a node from
the end of a list as they did to deleting a node from the
beginning of a list
 Now these deletion operations simply delete the
physically first or last node in the list
 What if we want to delete a specific node based on its
info member?
 In that case we have to locate the specific node, then
link around it by linking the previous node to the
following node
 But again, to do this we need to keep track of the
previous node, and we need to keep track of the node
containing the target value
 This will require two pointers, as shown in figure 3-8 (a)
– (d)
DELETION SINGLY LINKED LISTS
 As can be seen, the two extra pointers, pred and tmp,
are initialized to the first and second nodes in the list
 They traverse the list until tmp → info matches the
target value
 At that point, we can set pred → next = tmp → next
which “bypasses” the target node, allowing it to be
deleted
 There are several cases to consider when this type of
deletion is carried out
 Removing a node from an empty list or trying to delete
a value that isn’t in the list
 Deleting the only node in the list
 Removing the first or last node from a list with at least
two nodes
SEARCHING SINGLY LINKED LISTS
 The purpose of a search is to scan a linked list to find a
particular data member
 No modification is made to the list, so this can be done
easily using a single temporary pointer
 We simply traverse the list until the info member of the
node tmp points to matches the target, or tmp → next is
null
 If the latter case occurs, we have reached the end of the
list and the search fails
SINGLY LINKED LIST
IMPLEMENTATION
class Node:
node1.next = node2
def __init__(self, data): node2.next = node3
node3.next = node4
self.data = data currentNode = node1
self.next = None
while currentNode:
node1 = Node(3) print(currentNode.data, end=" -> ")
node2 = Node(5) currentNode = currentNode.next
print("null")
node3 = Node(13)
node4 = Node(2)

You might also like