0% found this document useful (0 votes)
2 views

L2

The document provides an overview of linked lists, a dynamic data structure consisting of nodes connected by pointers, which can grow or shrink in size. It details various types of linked lists, such as singly, doubly, and circular linked lists, along with their advantages and disadvantages compared to arrays. Additionally, it outlines key operations for manipulating linked lists, including insertion and deletion methods.

Uploaded by

samevi8763
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)
2 views

L2

The document provides an overview of linked lists, a dynamic data structure consisting of nodes connected by pointers, which can grow or shrink in size. It details various types of linked lists, such as singly, doubly, and circular linked lists, along with their advantages and disadvantages compared to arrays. Additionally, it outlines key operations for manipulating linked lists, including insertion and deletion methods.

Uploaded by

samevi8763
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/ 20

CSE 2001 - Data Structures and

Algorithms
Module 2: Linear Data Structure- Linked List

1
Contents
• Linked List
• Singly Linked List
• Operation on the linear list using singly linked storage
structures
• Circular List
• Applications of Linked list.

2
Linked List
A linked list is a data structure used for storing collections of data.
A linked list has the following properties.
1. Successive elements are connected by pointers
2. The last element points to NULL
3. Can grow or shrink in size during the execution of a program
4. Can be made just as long as required (until systems memory exhausts)
5. Does not waste memory space (but takes some extra memory for pointers).
6. It allocates memory as the list grows.

3
Representation
Head Data link Data link Data link NULL
Node Node Node

• LL - Consists of a sequence of elements where each element (called a node) comprises of two items -
the data and a reference (pointer) to the next node
• The last node has a reference to null.
• The entry point of the linked list is called the head (start) of the list.
• Head is not a separate node but a reference to the first node.
• If the list is empty, then the head is pointing to a null reference

4
Linked Lists ADT
The following operations make linked lists an ADT:
Main Linked Lists Operations
1. Insert: inserts an element into the list
2. Delete: removes and returns the specified position element from the list
Auxiliary Linked Lists Operations
3. Delete List: removes all elements of the list (disposees the list)
4. Count: returns the number of elements in the list
5. Find the nth node from the end of the list

5
Advantages of Linked Lists
• Dynamic Size: Linked lists can grow and shrink dynamically during runtime, making them
more flexible than arrays with a fixed size.
• Efficient Insertions/Deletions: Insertions and deletions are more efficient, especially for
large data sets, as they only require changing pointers rather than shifting elements (as in
arrays).
• No Wasted Memory: Linked lists use memory efficiently by allocating memory as needed for
each element (node), avoiding the need to pre-allocate a block of memory-like arrays.
• Easy Implementation of Data Structures: Linked lists are a foundation for implementing
other data structures like stacks, queues, and graphs.
• Efficient Use of Memory: Memory is only allocated when needed, unlike arrays that may
allocate extra memory to accommodate potential future growth

6
Disadvantages of Linked Lists:
• Higher Memory Overhead: Each node requires additional memory for the pointer(s), which
can result in significant memory usage for large lists.
• Slower Access Times: Linked lists do not allow random access to elements; you must
traverse the list sequentially, which can be time-consuming, especially for large lists.
• Complexity in Algorithms: Operations like searching or accessing a specific element can be
more complex and slower than arrays.
• Pointer Management: Managing pointers can introduce bugs or lead to issues like memory
leaks if not handled properly, especially in languages like C or C++.
• Cache Performance: Due to non-contiguous memory allocation, linked lists may perform
worse than arrays regarding cache performance, which can affect speed.

7
Arrays Vs Linked Lists
Feature Array Linked List
Definition Collection of elements of similar data Collection of nodes, each with data and a
type. pointer to the next node.
Memory Location Elements are stored in contiguous Nodes can be stored anywhere in memory
memory locations. (non-contiguous).
Memory Static memory allocation (fixed size). Dynamic memory allocation (size can grow
Management or shrink at runtime).
Element Dependency Elements are independent of each Elements (nodes) are linked through
other. pointers.
Insertion/Deletion Takes more time (due to shifting of Takes less time (only pointers need to be
elements). adjusted).

8
Arrays Vs Linked Lists
Feature Array Linked List
Access Speed Faster access through direct indexing Slower access (O(n)) as traversal is required
(O(1)). from the head.
Memory Allocation Allocated at compile-time. Allocated at runtime.

Memory Utilization Inefficient if array is not fully used Efficient, as memory is allocated only as
(unused memory space). needed.
Usage Scenario Suitable when size is known in Suitable when frequent insertions/deletions
advance and frequent access is are required.
needed.

9
Types of Linked List
• Singly Linked List: Each node has a reference to the next node in the
sequence.
Head Data link Data link Data link NULL
Node Node Node
• Doubly Linked List: Each node has two references, one to the next node and
one to the previous node.

Head link Data link link Data link NULL


Node Node

10
Types of Linked List
• Circular Linked List: The last node returns to the first node, forming a
circular structure.
Head Data link Data link Data link
Node Node Node

Head link Data link link Data link


Node Node

11
Operations – Creation of LL

Head NULL

Head Data=10 Link=NULL

@xxxx10

12
Operations – Insertion
Head Data=10 Link=NULL

@xxxx10

Head Data=10 Link=xxx20 Data=20 Link= xxx30 Data=30 Link=NULL

@xxxx10 @xxx20 @xxx30

END

13
Operations – Insertion(End)
Step :1 Identify the end node
Head Data=10 Link=xxx20 Data=20 Link= xxx30 Data=30 Link=NULL

@xxxx10 @xxx20 @xxx30

Link == Null ?

Link == Null ? Yes

Link == Null ?

14
Operations – Insertion(End)
• Create a new node with the given data.
• If the list is empty, set head to the new node.
• Else, traverse the list to find the last node.
• Set the link of the last node to the new node
Operations – Insertion(End)
Step :2 Insert the node after the end node

Head Data=10 Link=xxx20 Data=20 Link= xxx30 Data=30 Link=xxx40

@xxxx10 @xxx20 @xxx30

Data=40 Link=NULL

@xxx40

16
Operations – Insertion(Beginning)
• Create a new node.
• Set the link of the new node to head.
• Update head to point to the new node.
Operations – Insertion(Beginning)
Step 1: Copy the reference of the Head node to new node of link
Head Data=10 Link=xxx20 Data=20 Link= xxx30 Data=30 Link=NULL

@xxxx10 @xxx20 @xxx30

Data=40 Link=xxxx10

@xxx40

18
Operations – Insertion(Beginning)
Step 2: copy the new node reference to Head
Head Data=10 Link=xxx20 Data=20 Link= xxx30 Data=30 Link=NULL

@xxxx10 @xxx20 @xxx30

Data=40 Link=xxxx10

@xxx40

19
Remaining operations
• insertBefore(int bNodeData, int data): Insert a node before a node with specified data.
• insertAfter(int aNodeData, int data): Insert a node after a node with specified data.
• deleteBeg(): Delete the node at the beginning.
• deleteEnd(): Delete the node at the end.
• deletePos(int pos): Delete a node at a specified position.
• getPosition(int data): Get the position of a node by its data.
• display(): Display the entire linked list.
• search(int data): Search for a node with specific data.
Refer the Code

20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy