Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views41 pages

2 Linked List-3

The document provides an overview of linked lists, including definitions, properties, types (singly, doubly, and circular), and operations such as insertion, deletion, and traversal. It highlights the advantages and disadvantages of linked lists compared to arrays, emphasizing their dynamic size and efficient insertion/deletion, but also their lack of random access and extra memory usage. Real-time applications of linked lists are discussed, particularly in managing healthcare records and social networking applications.

Uploaded by

hhasehm4
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)
9 views41 pages

2 Linked List-3

The document provides an overview of linked lists, including definitions, properties, types (singly, doubly, and circular), and operations such as insertion, deletion, and traversal. It highlights the advantages and disadvantages of linked lists compared to arrays, emphasizing their dynamic size and efficient insertion/deletion, but also their lack of random access and extra memory usage. Real-time applications of linked lists are discussed, particularly in managing healthcare records and social networking applications.

Uploaded by

hhasehm4
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/ 41

Data Structures and Algorithms

0404201

2. Linked
List
1
Content
– Linked List definition
– Linked List Properties
– Single vs. Double vs. Circular Linked List
– Linked List Operations
– Pseudo code
– Advantages and disadvantages of Linked Lists
– Real time applications of linked lists

2
Introduction
• In a system, if we maintain a sorted list of IDs in an array
id[] = [1000, 1010, 1050, 2000, 2040].

• If we want to insert a new ID 1005, then to maintain the sorted


order, we have to move all the elements after 1000 (excluding
1000).

• Deletion is also expensive with arrays until unless some special


techniques are used. For example, to delete 1010 in id[],
everything after 1010 has to be moved due to this so much work is
being done which affects the efficiency of the code.

Reference: https://www.geeksforgeeks.org/data-structures/linked-list/
3
Introduction
Arrays vs Linked Lists
• Access time
– Array-based: constant access time
– Pointer-based: the time to access the ith node depends on i

• Insertion and deletions


– Array-based: require shifting of data
– Pointer-based: require a list traversal

• Unlike arrays, linked lists use nodes to store elements which are not
stored in contiguous memory locations.

4
Linked List Definitions
• A linked list is a linear data structure used to store a collection of
elements.
• Linked lists allocate memory for each element separately and only when
necessary.
• The elements in a linked list are linked using pointers as shown in the
below image:

Reference: https://www.geeksforgeeks.org/data-structures/linked-list/
5
Linked List Definitions
Example:

• A linked list is a collection of nodes where each node contains data as well as
the memory address of the next node in the list.

• Here, you can see that the addresses of the nodes are not necessarily
immediately sequential. The first node has an address of 200 and the second
node has an address of 801, instead of 201 as you might expect.

https://www.freecodecamp.org/news/how-linked-lists-work/
6
Linked List Definitions
Example:

• Then how are the nodes stored linearly?


• Even though the nodes are not in a contiguous memory, the
nodes are stored linearly through links. Every node has the
address of its succeeding node. That is how each node can
access its succeeding node.
https://www.freecodecamp.org/news/how-linked-lists-work/
7
Linked List Components
• A linked list consists of a sequence of nodes

• Nodes are the building block of the linked list. After all, a linked list
is a collection of nodes.

• A node in a linked list consists of two parts:

• The last node contains a null link


• The list may (or may not) have a header

https://www.freecodecamp.org/news/how-linked-lists-work/
8
Head and Tail in a Linked List
• As mentioned earlier, a linked list is a collection of nodes.

https://www.freecodecamp.org/news/how-linked-lists-work/
9
Linked List Types
• Types of Linked Lists:
– Singly Linked List (SLL)
– Doubly Linked List(DLL)
– Circularly Linked List (CLL)

10
Linked List Types
1. Single-linked list:
In a singly linked list, each node contains a reference to the next node
in the sequence. Traversing a singly linked list is done in a forward
direction.

Reference: https://www.geeksforgeeks.org/data-structures/linked-list/
11
Linked List Types
2. Double-linked list:
In a doubly linked list, each node contains references to both the
next and previous nodes. This allows for traversal in both forward
and backward directions, but it requires additional memory for the
backward reference.

12
Reference: https://www.geeksforgeeks.org/data-structures/linked-list/
Linked List Types
Double-linked list Advantages and Disadvantages:
Advantages:
• Convenient to traverse the list backwards.
• Simplifies insertion and deletion of nodes.

Disadvantage:
• Increase memory space requirements.
• List manipulations are slower (because more links must be changed)
• Greater chance of having bugs (because more links must be
manipulated)

13
Linked List Types
3. Circular-linked list:
In a circular linked list, the last node points back to the head node,
creating a circular structure. It can be either singly or doubly linked.

• Last node references the first node


• Every node has a successor
• No node in a circular linked list contains NULL
Reference: https://www.geeksforgeeks.org/data-structures/linked-list/
14
Properties of Linked Lists
• A node’s successor is the next node in the sequence
– The last node has no successor

• A node’s predecessor is the previous node in the sequence


– The first node has no predecessor

• A list’s length is the number of elements in it


– A list may be empty (contain no elements)

• Each node contains a value and a link to its successor


• The header is pointing at the first node in the list
• The header pointer reference to null if the list is empty

15
Properties of Linked Lists
• The pointer always points to the next member of the list.
• If the pointer is NULL, then it is the last node in the list.
• A linked list is held using a local pointer variable which points to
the first item of the list.
• If that pointer is also NULL, then the list is considered to be empty.

16
Properties of Linked Lists
• Linked lists are appropriate when the number of data elements to
be represented in the data structure at once is unpredictable.
• Linked lists are dynamic, so the length of a list can increase or
decrease as necessary.
• Each node does not necessarily follow the previous one physically
in the memory.
• Linked lists can be maintained in sorted order by inserting or
deleting an element at the proper point in the list.

17
Operations
Operations that can be implemented on a linked list:
• Creating a linked list
• Insert a node
• Delete a node
• Modifying a node’s data value
• Modifying the order of the nodes (for sorting)
• List traversal
• Searching for a node

Difficulties:
There is no names or indexes given for the nodes, thus, a node is
accessible by some pointers. 18
Creating a Linked List
Creating an empty linked list
Empty Linked list is a single pointer having the value of NULL.
– Create a header pointer head
– Make the header point to null

• A header node is just an initial node that exists at the front of every list,
even when the list is empty.
• The purpose is to keep the list from being null, and to point at the first
element.
• It is good to have a pointer with the name “tail” to point at the last node.

Pseudo-code
head = null
tail = null 19
Pseudo Code Conventions
The pseudo code to describe the operations’ algorithms uses
the following terms:
– p/current :a pointer to a node.
– head :a pointer to the first node in the list (header).
– tail: a pointer to the last node in the list (tail).
– info :node data.
– Link/next :node link, a pointer also.
– null :a value of a pointer that is not an actual address.
– p-> info :the data in the node that is pointed by p.
– p-> link :the link in the node that is pointed by p, which is another
node.
– p = head :make p point at the same node as the head does.
– head = p : make the head pointer point at the same node as p
does. 20
Pseudo Code Conventions
Example 1:
– Node * head: creating a pointer called head
– head: retrieving the whole node pointed at by the head (in the example
node a)
– head-> info: retrieving the info portion of the node above, in this case 10
– head-> link: retrieving the whole node pointed at by the head (in the
example the node b)
– head-> link->info: retrieving the value 15
– head-> link->link: retrieving the node c
– head-> link-> link-> info: retrieving the value 3

head
a b c d

10 15 3 y
21
Pseudo Code Conventions
Example 2:
– Node * head: creating a pointer called head
– head: retrieving the whole node pointed at by the head (in the example
node a)
– head-> info: retrieving the info portion of the node above, in this case 11
– head-> link: retrieving the whole node pointed at by the head (in the
example the node b)
– head-> link->info: retrieving the value 18
– head-> link->link: retrieving the node c
– head-> link-> link-> info: retrieving the value 24
a b c
head

22
Inserting a New Node
Options to insert a new node in a linked list

– Insert a new first node

Required List
Traversal
– Insert a new last node (traversal first).
– Insert a node before a given node (specified by a
reference/ pointer)
– Insert a node after a given node (specified by a reference/
pointer)

Required List
Searching
– Insert before a given value (searching first)
– Insert after a given value (searching first)

• All are possible, but differ in difficulty 23


Inserting a New Node
Creating the new node to be inserted

The node has no name. Creating an object Pseudo-code


without name will be lost in the memory. Algorithm create (value x)
node* p
Solution: Create a pointer to point at the new
p->info = x
node p->link = null
end
Steps:
– Create a new pointer to point at the new
node p
– Create the node by:
• Fill-in the node data if given. 4
• Make the link of the node points to null.

24
Inserting a New Node
Example 1: Insert a new node before the first one

25
Inserting a New Node
Example 2: Insert a new node before the first one

26
Inserting a New Node
Example 3: Insert a new node after the last one

27
Inserting a New Node
Example 4: Insert a new node after the last one

28
Inserting a New Node
Example 5: Insert a new node in the middle

29
Inserting a New Node
Example 6: Insert a new node in the middle

30
Inserting a New Node
Insert a new node before the first one in an empty list
Steps: Pseudo-code
– Create a new pointer to point at the new Algorithm insert(node* head,
node value x)
– Create the node by: node* p
• Fill-in the node data if given. p->info = x
p->link = null
• Make the link of the node points to null.
head = p
– Make the head point to the created node. tail = p
– Make the tail point to the created node. p= null
– Optional: release the created pointer. end
head
head p p p

a a
Step 1 Step 2
Step 3 Step 4 31
List Traversal
• List traversal: make a pointer points at a node then to its successor till
reaching the end of the list (or reaching a specific node).
• Starting at the first node:
– current=head // make the current pointer (traversal pointer) points at
what head points at, which is the first node in the list.
• Moving next:
– current=current->link

current

head

a b c
32
Searching for a Node
Searching for a node: Finding a node in a linked list.

Steps:
– Create a pointer for traversal/searching purpose (current).
– Make this pointer points at what the head does.
– Make this pointer follows the links of the node sequentially.
– Stop when the pointer reach a link with null value or tail pointer or when the
value is found.
– Optional: release the created pointer.

Pseudo-code
Algorithm search(node* head, value x)
current = head
while current != null and current->info != x
current = current->link
return current
end 33
Delete a Node
• Delete Options
– Delete the first node
– Delete the last node
– Delete a node in the middle

To delete a node:
1) No pointer shall be point at it (No identity/no name)
2) The garbage collection will remove the node eventually

34
Delete a Node
• To delete the first element, change the link in the header
head

one two three

• To delete some other element, change the link in its predecessor


head
(predecessor)
one two three

• Deleted nodes will eventually be garbage collected

35
Delete a Node
Delete the first node
Steps
– Deleting the first node in a list is a special case, because the node’s
predecessor is the list header

Pseudo-code
Algorithm deleteFirstNode(node * head)
head = head->link
end

36
Delete a Node
Delete a middle or last node
Steps
– Required traversing through the nodes.
– Locating the node based on its value, x.
– In order to delete a node, you have to change the link in its predecessor.

Pseudo-code
Algorithm deleteNode(node * head, value x)
node* previous, current
current = head;
previous = head;
while current->link =! null && current->info != x
previous = current
current = current-> link
previous -> link = current -> link
end
37
Modify a value
Modify a node value (data/info)
It is another traversing process

Steps:
– Search/traverse through the nodes till the value found
– Change the value

Pseudo-code
Algorithm search(node* head, x, newvalue)
current = head
while current != null and current->info != x
current = current->link current
current -> info = newvalue head

end
one two three

38
Advantages and disadvantages of Linked
Lists
Advantages:
•Dynamic Size: Linked lists can grow or shrink dynamically, as
memory allocation is done at runtime.
•Insertion and Deletion: Adding or removing elements from a linked
list is efficient, especially for large lists.
•Flexibility: Linked lists can be easily reorganized and modified
without requiring a contiguous block of memory.

Disadvantages:
•Random Access: Unlike arrays, linked lists do not allow direct access
to elements by index. Traversal is required to reach a specific node.
•Extra Memory: Linked lists require additional memory for storing the
pointers, compared to arrays.

39
Real Time applications of linked lists
Linked lists are a more complex data structure that can be used in
situations where the size of the collection is not known in advance or
where elements need to be added or removed frequently.

Application 1: Managing healthcare records for a large hospital or clinic.


• Patients may need to be added or removed from the system at any
time, and their medical history and treatment information may need to
be updated frequently.
• A linked list would allow healthcare providers to easily add or remove
patient records and modify their medical information as needed .

Reference: https://hackernoon.com/arrays-vs-linked-lists-which-is-better
40
Real Time applications of linked lists
Application 2: building a social network or messaging application
• In these types of applications, users may need to be added or removed
from the system at any time, and messages may need to be added or
removed from their conversation threads.

• A linked list would allow you to easily add or remove users and
messages and keep track of the order in which they were added.

Reference: https://hackernoon.com/arrays-vs-linked-lists-which-is-better
41

You might also like