DSA 3 DAYS Part 3
1. Introduction
A linked list is a data structure in which each item is connected to the next item through a link or reference. Each node in the linked list contains two parts:
Data: The value or data stored in the node.
Link: The reference to the next node in the sequence.
This structure forms a chain of nodes, allowing dynamic memory allocation and efficient insertions or deletions. Linked lists are fundamental in implementing more complex data structures like trees and graphs.
2. Representation
A linked list can be represented as a sequence of nodes, where each node points to the next. For example:
[Data | Link] -> [Data | Link] -> [Data | Link] -> NULL
3. Applications of Linked Lists
Linked lists are widely used in computer science for various purposes, including:
Implementing Stacks and Queues: Efficiently manage dynamic sizes.
Graphs: Used in the adjacency list representation of graphs.
Hash Tables: Handle collisions using chaining.
Polynomial Representation: Represent polynomials by storing coefficients and exponents in nodes.
4. Arrays vs. Linked Lists
Arrays
Fixed size determined at creation.
Easy access to any element using the index in constant time.
No space overhead.
Memory usage:
n x sizeof(element)
.
Linked Lists
Dynamic size; grows or shrinks as needed.
Requires extra memory for storing links or references.
Memory usage:
n x sizeof(element) + n x sizeof(reference)
.No direct access to elements; traversal required.
5. Types of Linked Lists
Singly Linked List: Each node has one link pointing to the next node.
Doubly Linked List: Each node has two links, one to the next node and one to the previous node.
Circular Linked List: The last node links back to the first node, forming a circular structure.
6. Operations on Linked Lists
Traversal
Set
PTR = Start
.Repeat steps 3 and 4 while
PTR != NULL
:Write
info[PTR]
.Set
PTR = Link[PTR]
.
Exit.
Searching in an Unsorted Linked List
Set
PTR = Start
.Repeat while
PTR != NULL
:If
ITEM == info[PTR]
, setLOC = PTR
and exit.Else, set
PTR = Link[PTR]
.
If the search is unsuccessful, set
LOC = NULL
.Exit.
Insertion in a Linked List
At the Beginning
Start.
Create a new node.
Set
NewNode.Data = Data
.Set
NewNode.Link = Head
.Update
Head = NewNode
.Exit.
At a Specific Position
Start.
Create a new node.
Traverse the list to find the position (one node before the target position).
Set
NewNode.Data = Data
.Set
NewNode.Link = CurrentNode.Link
.Update
CurrentNode.Link = NewNode
.Exit.
At the End
Start.
Create a new node.
Traverse the list to find the last node.
Set
NewNode.Data = Data
.Set
NewNode.Link = NULL
.Update the last node's link to
NewNode
.Exit.
Deletion in a Linked List
At the Beginning
Start.
Set
PTR = Head
.Update
Head = Head.Link
.Remove
PTR
.Exit.
At a Specific Position
Start.
Traverse the list to find the node before the target node.
Update
PTR.Link
to skip the target node.Remove the target node.
Exit.
At the End
Start.
Traverse the list to find the second-to-last node.
Update
SecondLastNode.Link = NULL
.Remove the last node.
Exit.
7. Stack
A stack is a linear data structure where all operations occur at one end, called the top. It follows the Last In, First Out (LIFO) principle.
Basic Operations
Push: Add an element to the top.
Pop: Remove the top element.
Peek: View the top element without removing it.
isFull: Check if the stack is full.
isEmpty: Check if the stack is empty.
8. Queue
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. Insertions occur at the rear, and deletions occur at the front.
Operations
Enqueue: Add an element to the rear.
Dequeue: Remove an element from the front.
Peek: View the front element without removing it.
Initialize: Set up the queue.
isFull: Check if the queue is full.
isEmpty: Check if the queue is empty.
Comments
Post a Comment