2.1 Singly Linked Lists
2.1 Singly Linked Lists
Second part contains the address of the next node in the list.
Linked List
• A singly linked list is a
concrete data structure
consisting of a sequence of next
nodes
• Each node stores
– element
– link to the next node node
element
A B C D
Key Points
Linked list
Linear collection of self-referential class
objects, called nodes
Connected by pointer links
Accessed via a pointer to the first node
of the list
Link pointer in the last node is set to null
to mark the list’s end
• Linked list contains a Pointer Variable called START,
which contains the address of the first node.
Self-Referential Structures
Self-referential structures
Structure that contains a pointer to a structure of the same type
Can be linked together to form useful data structures such as lists,
queues, stacks and trees
Terminated with a NULL pointer (0)
Diagram of two self-referential structure objects linked
together 3 2
100 … 500
struct node {
int info;
struct node *link;
};
link
Points to an object of type node
Referred to as a link of one node to next node.
Linked Representation of Data
358 797
In a linked representation, 561 501
345 724
data is not stored in a
contiguous manner. Instead, 555
data is stored at random 490 701 513
locations and the current data
location provides the
information regarding the 358 797
location of the next data. 561 501
345 724
Adding item 498 on to the linked list 555 490 701 513
Q: What is the cost of adding an
item? 498
Q: how about adding 300 and 800
onto the linked list 358 797
561 501
345 724
Deleting item 358 from the linked
list 555 490 701 513
Q: What is the cost of deleting an
item?
498
Q: What is the cost of searching for
an item?
Linked List
How do we represent a linked list in the memory
Each location has two fields: Data Field and Pointer
(Link) Field.
Linked List Implementation
START Node
Element
Data Pointer (Link)
Null Pointer
Field Field
struct node {
int data;
1 300 5
struct node *link;
};
2 500 0 NULL
struct node my_node; 3
3 100 4
Example:
4 200 1
5 400 2
Why Linked List?
Arrays: pluses and minuses
+ Fast element access.
-- Expensive insertion deletion
-- Impossible to resize.
INFO LINK
Start 2 1
2 A 6
3 E 9
4 C 7
5
6 B 4
7 D 3
8
9 F 0
10
Memory Representation (2)
Multiple lists in memory
INFO LINK
Start1 2 1 S4 0
2 A 6
3 E 9
Start 210 4 C 7
5 S2 8
6 B 4
7 D 3
8 S3 1
9 F 0
10 S1 5
Memory Representation (3)
INFO part of a node may be a record with multiple data items.
Records in memory using Linked lists
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
bat cat sat vat NULL
Traversal in Linked List
Is to process information in each nodes’ info
part
We initially only knows the address of first node
that’s to perform operations such as
traversal ,searching , insertion at middle,
deletion at middle. We have to traverse the
linked list sequentially.
We will use while loop to perform traversal
Initialisation ptr=start
Condition while ptr!= null
Increment ptr=link[ptr]
Algorithm Traversal In linked
list
Traversal(INFO,LINK,PTR,START): It is to traverse
the linked list .
1. Set PTR := START [Initializes pointer
Ptr]
2. Repeat step 3 and 4 while PTR != NULL
[Condition]
3. Apply Process to INFO[PTR]
4. Set PTR := LINK[PTR] [Ptr now points to
next node]
[End of step 2 loop]
5. Exit
Searching in linked list
As linked list is sequential data structure and
initially we only know the address of first
node only that’s why only linear search can
be applied to linked list and not binary
search.
to perform searching first traverse
the linked list upto the point that
Info[PTR]=ITEM, if it found then
break.
Algorithm of searching in
unsorted linked list
Search(INFO,START,LINK,PTR,ITEM,LOC): ): It is to search ITEM the
linked list where INFO representing the information part in each node,
LINK is the link part holding the address of next node. PTR id pointer
variable, START is pointer variable holding the address of first node.
ITEM holds the value to search. LOC hold address of node found
location
1. Set PTR := START [Initializes pointer Ptr]
2. Repeat step 3 while PTR!= NULL
3. If ITEM = INFO[PTR], then:
Set LOC := PTR and Exit
Else
Set PTR := LINK[PTR] [Ptr now points to next node]
[End of If structure]
[End of step 2 loop]
4. [Search is unsuccessful] Set LOC := NULL
5. Exit
Algorithm of searching in
sorted linked list
SEARCH (INFO, LINK, START, ITEM, LOC)
1. Set PTR: = START.
2. Repeat Step 3 While PTR ≠ NULL.
3. If ITEM > INFO [PTR], then:
Set PTR: = LINK [PTR]. [PTR points to next node]
Else if ITEM = INFO [PTR], then:
Set LOC: = PTR, and EXIT. [Search is successful.]
Else:
Set LOC := NULL, and EXIT . [ITEM exceeds INFO[PTR]…]
[End of if Structure.]
[End of step 2 Loop.]
4. Return LOC .
5. Exit.
Memory Allocation
Together with the linked list, a special list is maintained
which consists of unused memory cells.
INFO LINK
START 2 1 0
2 A 6
3 E 9
10
AVAIL C 4 7
5 8
6 B 4
7 D 3
8 1
9
F 0
10
5
Garbage Collection
Garbage collection is a technique of collecting all the
deleted spaces or unused spaces in memory.
Types of insertion:
Insertion at the beginning
Insertion between two nodes
Insertion after given node.
Algorithmic syntax to perform 4
steps to insert node(NOT
ALGO)
1.Check availability of node in avail list
If AVAIL= null ,then:
Write: overflow
2.If available then remove the node from avail list
Set NEW:=AVAIL
Set AVAIL:=LINK[AVAIL]
3.Insert element to info part of that node
Set INFO[NEW]=ITEM
4.Link the node with linked list where i want to perform insertion
Set LINK[NEW]:=START //when insert at the beginning
Set START:=NEW
OR
Set LINK[NEW]=LINK[LOC]//when insert after some location
LOC
Set Link[LOC]=NEW
Checking the Available List
AVAIL
ITEM Ø
Free-Storage List
NEW
Insertion at the beginning of Linked List
START
ITEM
(1) Insertion Algorithm (Beginning of the list)
INSFIRST (INFO, LINK, START, AVAIL, ITEM):to insert
element at the beginning
ITEM
(2) Insertion Algorithm (After a given
node)
INSLOC (INFO, LINK, START, AVAIL, LOC, ITEM): LOC
is location of node after which we want to insert new node
1. [List Empty?] If START := NULL, then: Set LOC: = NULL, and Return.
2. [Special Case?] If ITEM < INFO [START], then: Set LOC := NULL, and
Return.
3. Set SAVE: = START and PTR := LINK [START]. [Initializes pointers]
4. Repeat step 5 and 6 while PTR ≠ NULL.
5. If ITEM < INFO [PTR] then:
Set LOC: = SAVE, and Return.
[End of If Structure.]
6. Set SAVE: = PTR and PTR: = LINK [PTR]. [Update pointers]
[End of Step 4 Loop.]
7. Set LOC: = SAVE.
8. Return.
Deletion from a Linked List
A node N is to be deleted from the Linked List.
Node N is between node A and node B.
Types of Deletion:
Deleting the node following a given node
Deleting the Node with a given ITEM of Information.
Deletion from Linked List
START
ITEM Ø
3. EXIT.
Deleting the Node with a given ITEM of
Information
DELETE (INFO, LINK, START, AVAIL, ITEM)
1. Call FIND_B (INFO, LINK, START, ITEM, LOC, LOCP)
[Find the Location of node N and its preceding
node]
2. If LOC = NULL, then: Write: ITEM not in LIST and EXIT.
3. [Delete node].
If LOCP = NULL, then:
Set START: = LINK [START]. [Delete First node]
Else:
Set LINK [LOCP]: = LINK [LOC].
[End of If Structure.]