0% found this document useful (0 votes)
13 views12 pages

Linked Lists Group 9

The document outlines a course unit on Data Structures and Algorithms, focusing specifically on linked lists. It details the structure, types, operations, advantages, and disadvantages of linked lists compared to arrays. Additionally, it highlights applications of linked lists in various programming scenarios.

Uploaded by

bctgdg4hdg
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)
13 views12 pages

Linked Lists Group 9

The document outlines a course unit on Data Structures and Algorithms, focusing specifically on linked lists. It details the structure, types, operations, advantages, and disadvantages of linked lists compared to arrays. Additionally, it highlights applications of linked lists in various programming scenarios.

Uploaded by

bctgdg4hdg
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/ 12

COURSE UNIT: DATA STRUCTURES AND ALGORITHMS

COURSE CODE: COS2201

LECTURERS NAME: MS. NABAYINDA HARRIET

COURSE WORK: LINKED LISTS

NAME REG. NO. PARTS TO PRESENT

ATUHAIRE HAMZA 2023-08-19543  Introduction


 Types of linked lists-partly

BAKASHABA ALLAN 2023-08-18392  Types of linked lists-partly


 Operations on linked lists-partly

ASIIMWE ANDREW 2023-08-20056  Operations on linked lists-partly


 Advantages & disadvantages
 Uses
Linked Lists

1. Introduction

A linked list is a linear data structure where elements are stored in nodes, and each
node points to the next one in the sequence. Unlike arrays, linked lists do not store
elements in contiguous memory locations.

Structure of a Node
each node contains:
Data Field: Holds the actual data.
Next Field: A pointer/reference to the next node.
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
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. the first node in a linked list is called the "Head", and
the last node is called the "Tail".
Code structure of a node
class Node:
def __init__=(self, data):
self.data = data # Store the data
self.next = None # Pointer to the next node (initially None)
Comparison with arrays
Linked lists consist of nodes, and is a linear data structure we make ourselves, unlike
arrays which is an existing data structure in the programming language that we can
use. Nodes in a linked list store links to other nodes, but array elements do not need to
store links to other elements.
Arrays vs linked lists 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 above shows how an array of integers myArray = [3,5,13,2] is 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.

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.
Differences between arrays and linked lists

ARRAYS LINKED LISTS

Contiguous memory allocation Dynamic memory allocation (nodes)

Simple More complex (pointer management)

Memory usage is low Memory usage is high

Random access is possible Elements, or nodes, can be accessed


directly

shifting operations in memory needed. no shifting operations in memory


needed.

2. Types of Linked Lists

1. Singly Linked List (SLL)


o Each node has two parts:
 Data (value stored in the node)
 Pointer (link to the next node)

o Last node points to NULL.


Implementation

Advantages:

✔ Less Memory Usage – Only one pointer per node (to the next node), reducing memory
overhead.
✔ Efficient Insertion/Deletion at Head – Can insert or delete at the beginning in O(1)
time.
✔ Dynamic Size – Can grow/shrink in size without needing predefined memory like an
array.

Disadvantages:

✘ Slow Access Time (O(n)) – Cannot access elements directly by index (like arrays).
Traversal is required.
✘ One-way Traversal – Cannot move backward, only forward.
✘ Deletion at Tail is Costly (O(n)) – To delete the last node, full traversal is needed.
2. Doubly Linked List (DLL)
o Each node has three parts:
 Data
 Pointer to next node
 Pointer to previous node
o Allows traversal in both directions.

Implementation
Advantages:

✔ Two-way Traversal – Can move both forward and backward through the list.
✔ Efficient Deletion (O(1)) – Can delete a node without traversal if a pointer to it
is given.
✔ More Flexible Operations – Easier to implement complex structures like stacks
and queues.
Disadvantages:

✘ More Memory Usage – Each node stores two pointers, doubling memory
requirements.
✘ Insertion Complexity – Inserting requires updating two pointers instead of one.
✘ More Overhead – More processing needed for maintaining extra pointers.

3. Circular Linked List (CLL)


o Can be singly or doubly linked.
o Last node points back to the first node.
o No NULL in the last node.
Implementation

Advantages:

✔ Efficient for Repeated Traversal – No need to check for NULL; looping is


automatic.
✔ Useful in Round-Robin Scheduling – Used in CPU scheduling where processes
cycle in a loop.
✔ Can Start from Any Node – No strict head or tail, making insertions/removals
flexible.

Disadvantages:

✘ Complex Implementation – Requires careful pointer updates to maintain the


circular structure.
✘ Infinite Loop Risk – Incorrect handling can cause infinite loops in traversal.
✘ Difficult to Reverse – Reversing a circular list requires extra logic.
4. Circular Doubly Linked List (CDLL)
o Each node has two pointers (next and previous).
o Last node connects to the first node in both directions.

Implementation
Advantages:

✔ Bidirectional Traversal – Can move both forward and backward efficiently.


✔ No NULL Pointers – The list never ends; no need for NULL checks at head or
tail.
✔ Efficient Deletion & Insertion – Any node can be inserted or deleted in O(1)
time if a pointer to it is given.
✔ Good for Repeated Processes – Used in CPU scheduling, real-time systems,
multiplayer games, and memory allocation.

Disadvantages:

✘ More Memory Usage – Each node has two pointers (next and prev), increasing
space complexity.
✘ Complex Implementation – Pointer management is harder due to circular
structure.
✘ Risk of Infinite Loops – If traversal conditions are incorrect, it can cause an
infinite loop.

4. Operations on Linked Lists

A. Insertion

1. At the Beginning
o Allocate memory for a new node.
o Point the new node to the current head.
o Update head to the new node.
2. At the End
o Traverse to the last node.
o Set its next pointer to the new node.
o Make the new node's next pointer NULL.
3. At a Given Position
o Traverse to the previous node of the target position.
o Update pointers accordingly.

B. Deletion

1. Delete from Beginning


o Change the head pointer to the second node.
o Free memory of the previous head.
2. Delete from End
o Traverse to the second last node.
o Set its next pointer to NULL.
o Free memory of the last node.
3. Delete a Specific Node
o Find the previous node of the node to be deleted.
o Change its next pointer to skip the deleted node.

C. Searching

• Traverse the list and compare each node’s data with the target value.

D. Traversal

• Start from the head and visit each node until reaching NULL.

Advantages Of Linked List:


i. Dynamic data structure: A linked list is a dynamic arrangement so it can grow
and shrink at runtime by allocating and deallocating memory. So there is no
need to give the initial size of the linked list.
ii. No memory wastage: In the Linked list, efficient memory utilization can be
achieved since the size of the linked list increase or decrease at run time so
there is no memory wastage and there is no need to pre-allocate the memory.
iii. Implementation: Linear data structures like stacks and queues are often easily
implemented using a linked list.
iv. Insertion and Deletion Operations: Insertion and deletion operations are quite
easier in the linked list. There is no need to shift elements after the insertion or
deletion of an element only the address present in the next pointer needs to be
updated.
v. Flexible: This is because the elements in Linked List are not stored in
contiguous memory locations unlike the array.
vi. Efficient for large data: When working with large datasets linked lists play a
crucial role as it can grow and shrink dynamically.
vii. Scalability: Contains the ability to add or remove elements at any position

Disadvantages Of Linked List:

i. Memory usage: More memory is required in the linked list as compared to an


array. Because in a linked list, a pointer is also required to store the address of
the next element and it requires extra memory for itself.
ii. Traversal: In a Linked list traversal is more time-consuming as compared to an
array. Direct access to an element is not possible in a linked list as in an array
by index. For example, for accessing a node at position n, one has to traverse
all the nodes before it.
iii. Random Access: Random access is not possible in a linked list due to its
dynamic memory allocation.
iv. Lower efficiency at times: For certain operations, such as searching for an
element or iterating through the list, can be slower in a linked list.
v. Complex implementation: The linked list implementation is more complex
when compared to array. It requires a complex programming understanding.

8. Applications of Linked Lists

• Dynamic memory allocation


• Undo functionality in editors
• Implementing stacks & queues
• Graph adjacency lists
• Music playlists and navigation systems

You might also like