0% found this document useful (0 votes)
17 views53 pages

Lecture 1 - Linked Lists

Uploaded by

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

Lecture 1 - Linked Lists

Uploaded by

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

CSC 511

Advanced
Data
Structures

Instructor:
Anthony Nwohiri
Department of Computer Sciences
University of Lagos
Who uses data structures?

Data scientists Business analysts

Software Software
engineers developers
Recommended Texts

• Classic Data Structures


2nd Edition by Debasis Samanta

• Data Structures and Algorithm Analysis


Edition 3.2 by Clifford A. Shaffer
• Online resources
1. Stack

2. Array

3. Linked lists

4. Trees
Binary tree
Course Binary search tree

Outline
Threaded binary trees
Balanced tree
Height balanced binary search tree – AVL trees
M-way
B-trees
B+ trees
Red-black trees
Applications, advantages and disadvantages

5. Heap
Course Outline (Dr. Sennaike)

7. Graphs 8. Files 9. Hashing

12. Spatial
10. Skip Lists 11. Tries Data
Structures
What is a linked data structure?
At the end
of today’s What is a Linked List?
lecture,
What are the types of linked lists?
the
student is Operations carried out on linked
expected lists
to know Application in real world
the
following Advantages and disadvantages
Data structures
The idea is to reduce the space and time complexities of different tasks.
Choosing the right data structure
for your algorithm
Consider the problem requirements

Understand the trade-offs

Know your algorithms

Consider the size of the data

Keep it simple
Primitive data types

BOOLEAN. FLOATING-POINT CHARACTER POINTERS


NUMBERS.

STRING INTEGER DATE/TIME


Complex data
structures
Then we also have some complex
data structures, which are used
to store large and connected
data. Some examples:
• Linked List
• Tree
• Graph
• Heaps
• Stack, Queue etc.
What are nodes?
A node is the basic unit of a data structure.
What are nodes?

This node (node_a)


contains a piece of data
(the number 5) and a link
to another node
(node_b):
Node implementations
Node linking
Python Implementation of Nodes
We will use a basic node that contains data and one
link to another node. The node’s data will be
specified when creating the node and immutable
(can’t be updated). The link will be optional at
initialisation and can be updated.

Remember that at the end of a node path, the link to


the next node is null because there are no more
nodes left. In Python, this means it will be set to
None.
1. Create a new class, Node. Add
an .__init__() method in the Node class that takes
a value and an optional link_node (default should
Try it yourself be None). These should be saved to the
corresponding self properties
(self.value and self.link_node).
2. We need methods to access the data and
link within the node. For this, we will use two
Try it yourself getters, .get_value() and .get_link_node().
These should each return their corresponding
value on the self object.
3. See previous code, we are only allowing the value
of the node to be set upon creation. However, we
want to allow updating the link of the node. For this,
we will use a setter to modify the self.link_node
Try it yourself attribute. The method should be called
.set_link_node() and should take link_node as an
argument. It should then update
the self.link_node attribute as appropriate.
Linked Lists

Linked list is a linear, dynamic data structure that contains


sequence of elements such that each element links to its
next element in the sequence. Each element in a linked list is
called a "Node". Head points to the first node, last node
points to NULL.
Representation of a Linked List

Monday, November 11, 2024 21


What is a Linked List?
A linked list is a linear data Nodes represent those data
structure that stores a collection elements, and links or pointers
of data elements dynamically. connect each node.

Each node consists of two fields,


The last node contains null in its
the information stored in a linked
second field because it will point
list and a pointer that stores the
to no node.
address of its next node.

A linked list can grow and shrink


It does not waste memory space.
its size, as per the requirement.
Linked Lists vs. Arrays
What Are They?
Size Matters
Memory Allocation
Cache friendliness
Memory Usage
Insertion and Deletion
Searching for Elements
Accessing Elements
Deleting Elements
Best Use Cases
Creation of a Node
and Declaration of Step 1: Define the Node
a Linked List - Structure
Phyton
Creation of a Node
and Declaration of Step 2: Initialize the Head of
a Linked List - the List
Phyton
Creation of a Node
and Declaration of Step 3: Create New Nodes
a Linked List -
Phyton
Creation of a Node
and Declaration of Step 4: Link the Nodes
a Linked List -
Phyton
Creation of a Node
and Declaration of Complete Implemenatation
a Linked List - LinkedList
Phyton
Complete Implementation of LinkedList:
Essential Traversing: To traverse all
nodes one by one.
Insertion: To insert new
nodes at specific positions.

Operation
on Linked
Lists
Deletion: To delete nodes Searching: To search for an
from specific positions. element from the linked list.
Singly Linked
List

Doubly Linked
Linked list List
Circular Singly
Linked List
Circular Linked
List
Circular Doubly
Linked List

Monday, November 11, 2024 31


Singly linked list
Traversal in Singly Linked List
Traversal in Singly Linked List
Deleting a Node in a Linked List
Inserting a Node in a Linked List
Doubly Linked List
Double linked list (DLL) is a sequence of elements in which every
node has links to its previous element and next element in the
sequence. So, we can traverse forward by using next field and can
traverse backward by using previous field. Every node in a double
linked list contains three fields and they are shown in the following
figure...

Monday, November 11, 2024 37


Doubly Linked List
Advantages over singly linked list???
Disadvantages over singly linked list???

Monday, November 11, 2024 38


Doubly linked list - operations

The following operations are performed in a


doubly linked list:
• Display (traversal, search) - similar to SLL
• Insertion
• Deletion

Monday, November 11, 2024 39


Doubly linked list – insertion at the front of a linked list
Inserting new node E at the front of a Linked List

Monday, November 11, 2024 40


Doubly linked list - insertion after/before a
given node
Inserting new node E in-between nodes B and
C

Monday, November 11, 2024 41


Doubly linked list - insertion at the end
Since a Linked List is typically represented by the head of
it, we have to traverse the list till end and then change
the next of last node to new node.

Monday, November 11, 2024 42


Doubly linked list - deletion
Algorithm
Let the node to be deleted be del.

1. If node to be deleted is head node, then change the head


pointer to next current head.
2. Set next of previous to del, if previous to del exits.
3. Set prev of next to del, if next to del exits.

Monday, November 11, 2024 43


Circular Linked List
Circular linked list is a sequence of elements in which every
element has link to its next element in the sequence and the last
element has a link to the first element in the sequence.
There is no NULL at the end. A circular linked list can be a singly
circular linked list or doubly circular linked list.

Monday, November 11, 2024 44


Circular Linked List
Advantages
1. Traversal starting from any node
2. Saves time
3. Useful for implementation of queue.
4. Useful in applications to repeatedly go around the list

Disadvantages
§ Uses more memory than arrays.
§ Could end up in an infinite loop.
§ Not easy to reverse.

Monday, November 11, 2024 45


Circular Linked List - Traversal
When do we stop traversal in a circular linked list?

Monday, November 11, 2024 46


Circular Singly Linked List - Insertion

To implement a circular singly linked list, we take an external


pointer that points to the last node of the list. If we have a pointer
pointing to the last node, then last -> next will point to the first
node.

The pointer last points to node Z and last -> next points to node P.
Monday, November 11, 2024 47
Circular Singly Linked List - Insertion

Why have we taken a pointer that points to the last node instead
of first node ?

Monday, November 11, 2024 48


Circular Singly Linked List – Insertion at the begining
To Insert a node at the beginning of the list, follow these step:
1. Create a node, say T.
2. Make T -> next = last -> next.
3. last -> next = T.

After insertion,

Monday, November 11, 2024 49


Circular Singly Linked List - Insertion at the end

Monday, November 11, 2024 50


Circular Singly Linked List – Insertion in-between nodes

Monday, November 11, 2024 51


Circular Singly Linked List - Deletion

In a circular linked list, the deletion operation can


be performed in three ways those are as follows...
• Deleting from Beginning of the list
• Deleting from End of the list
• Deleting a Specific Node

Monday, November 11, 2024 52


Applications of
Linked Lists

• Can be used to implement Stacks, Queues, Graphs.


• Can be used to implement Hash Tables.
• Undo functionality in Photoshop or Word.
• A polynomial can be represented in an array or in a
linked list.
• Dynamic memory allocation.
• Circular linked list is used is PCs, where multiple
applications are running.
• Music playlist

Monday, November 11, 2024 53

You might also like