Linked List
Contents
• Linked List
• Types of Linked List
• Linked List vs Array
2
List Implementation using Linked Lists
• 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
• Use a linked list instead of an array when
– You have an unpredictable number of data elements
– You want to insert and delete quickly.
Linked Lists
A B C 0
Head / START
⦿ A linked list is a series of connected nodes
⦿ Each node contains at least
– A piece of data (any type)
– Pointer to the next node in the list node
⦿ Head: pointer to the first node A
⦿ The last node points to NULL data pointer
ARRAY VS. LINKED LIST
Aspect Array Link List
Size Fixed number. Grow and contract since of
Size need to be insertions and deletions.
specific during Maximum size depends on
declaration. heap.
Storage capacity Static: It’s location is Dynamic: It’s node is
allocated during compile located during run time.
time.
Order and sorting Stored consecutively. Stored randomly.
Accessing the Direct or random access Sequential access method.
element method. Traverse starting from the first
Specify the array index or node in
subscript. the list by pointer.
Searching Binary search and linear Linear search
search
Linked Representation
• Data structure for a linked list:
first
Node
•Data
•Link (pointer): used to store the address of the next
node.
Anatomy of a linked list
⦿ A linked list consists of:
– A sequence of nodes
myList
a b c d
Each node contains a value and a link (pointer or reference) to some other node
The last node contains a null link
More terminologies
⦿ A node’s successor is the next node in the sequence
– The last node has no successor
⦿ A node’s predecessor is the previous node in the sequence
– The first node has no predecessor
⦿ A list’s length is the number of elements in it
– A list may be empty (contain no elements)
Linked Lists
Types of linked lists:
– Singly linked list
• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction
– Circular Linked List
• Pointer in the last node points
back to the first node
– Doubly linked list
• Two “start pointers” – first element and last element
• Each node has a forward pointer and a backward pointer
• Allows traversals both forwards and backwards
– Circular, doubly linked list
• Forward pointer of the last node points to the first node and backward pointer of the first node points to the
last node
Conventions of Linked List
There are several conventions for the link to indicate the end of the
list.
1. A null link that points to no node (0 or NULL)
2. A dummy node that contains no item
3. A reference back to the first node, making it a circular list.
Convention of the linked list
• A special list is maintained which consists of unused memory cells.
• This list, which has its own pointer, is called the list of available space
or the free storage list or the free pool.
• The operating system of a computer may periodically collect all the
deleted space onto the free storage list. Any technique which does this
collection is called garbage collection.
Conventions
⦿ Sometimes new data are to be inserted into the data structure but there is no
available space, i.e. the free storage list is empty. This situation is usually
called overflow.
means when AVAIL = NULL and there is an insertion.
⦿ The term underflow refers to the situation where one wants to delete data
from a data structure that is empty. The programmer may handle underflow
by printing the message UNDERFLOW.
means If START = NULL,
Linked Lists
Basic operations commonly include:
Construction: Allocate and initialize a list object (usually
Empty: empty)
Insert: Check if list is empty
Delete: Add an item to the list at any point
Traverse: Remove an item from the list at any
point
Go through the list or a part of it,
accessing and processing the
elements in the order they are stored.
Traversing a linked list
TRAVERSE ( INFO, LINK, START )
This algorithm traverses the linked list and displays the data in each
node.
1. [UNDERFLOW] If START = NULL then write: UNDERFLOW and
exit.
2. Set PTR = START [Initializes pointer to the first node].
3. Repeat steps 4 and 5 while PTR ≠ NULL.
4. Write INFO[PTR] [Displays the data of the current node].
5. Set PTR = LINK[PTR] [Moves pointer to the next node].
6. Exit.
Inserting at the Beginning of a list
INFIRST ( INFO, HEAD, AVAIL, ITEM )
This algorithm inserts ITEM as the first node of the list.
1. [OVERFLOW] if AVAIL=NULL then write : OVERFLOW and exit.
2. [remove first node from AVAIL list]
set NEW=AVAIL and AVAIL=LINK[AVAIL].
3. Set INFO[NEW]=ITEM [copies new data into new node]
4. Set LINK[NEW]=HEAD [new node now points to original first node]
5. Set HEAD=NEW [changes START so it points to the new node]
6. Exit
Inserting after a Given Node
INSLOC ( INFO, LINK, START, AVAIL, LOC, ITEM )
This algorithm inserts ITEM so that ITEM follows the node
with location LOC or inserts
ITEM as the first node when LOC = NULL.
1. [OVERFLOW] If AVAIL= NULL then write
OVERFLOW and exit.
2. [remove first node from AVAIL list]
set NEW: = AVAIL and AVAIL:= LINK [AVAIL]
3. Set INFO [NEW]:= ITEM [copies new data into new
node]
4. If LOC=NULL then [insert as a first node ]
set LINK [NEW]:= START and START:= NEW
Else [insert after node with location Loc]
set LINK[NEW]:=LINK[LOC] and LINK [LOC]:= NEW
[end of if structure]
Inserting into a Sorted Linked List
FINDA ( INFO, LINK, START, ITEM, LOC )
This procedure finds the location LOC of the last node in a sorted list such that INFO [LOC]
< ITEM, or sets LOC = NULL.
1. [list empty?] If START=NULL then set LOC=NULL and return.
2. [Special case? ] If ITEM < INFO[START], then sets LOC= NULL and return.
3. Set SAVE = START and PTR = LINK [START]. [Initializes pointer].
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]. [Updates pointers]
[End of Step 4 Loop]
7. Set LOC = SAVE.
8. Return .
Deletion from a Linked Lists
• Let LIST be a linked list with node N between A and B. Node N is to be deleted from
linked lists.
• The deletion occurs as soon as the nextpointer field of node A is changed so that is points
to node B.
• When a node N is deleted from a list it immediately return its memory space from AVAIL
list.
• Three pointers field are changed as follows:
i. The next pointer field of node A now points to node B where node N previously pointed.
ii. The next pointer field of N now points to the original first node in the free pool, where
AVAIL previously pointed.
iii. AVAIL now points to deleted node N.
Deletion Algorithm
• Algorithm which deletes nodes from linked lists come up in various situations.
• The first one deletes the node following a given node, and second one deletes the
node with a given ITEM of information.
• All of our deletion algorithm will return the memory space of the deleted node N to
the beginning of the AVAIL list.
• All our algorithm will include the following pair of assignments, where LOC is the
location of deleted node N.
LINK [LOC]: = AVAIL and then AVAIL: = LOC
Deletion in Linked List
START DATA LIST
Node A Node N Node B
A 0
AVAIL
FREE STORAGE LIST
Deleting the Node Following a Given Node
• Let LIST be the linked list in memory. Suppose we are given a location
LOC of a node N in a LIST.
• We are given the location LOCP of the node preceding N or, when N
is the first node then LOCP = NULL.
Algorithm
DEL ( INFO, LINK, START, AVAIL, LOC, LOCP )
This algorithm deletes the node N with location LOC. LOCP is the location of the
node which precedes N or, when N is the first node, LOCP = NULL.
1. If LOCP = NULL, then:
Set START: = LINK [ START]. [Deletes first node.]
Else:
Set LINK [LOCP]: = LINK [LOC]
[Delete node N]
[ End of If structure]
2. [ Return deleted node to the AVAIL list.]
Set LINK [LOC]: = AVAIL and AVAIL: = LOC.
3. Exit
Deleting the Node with Given ITEM of Information
• Let LIST be a linked list in memory and given an ITEM of information to be deleted from the
LIST the first node N which contains an ITEM.
• Before deleting node N from the list, it is needed to know the location of the preceding
node N.
• First procedure finds the location LOC of the node N containing ITEM and the location
LOCP of the node preceding node N.
• If N is the first node, set LOCP= NULL.
• If ITEM does not appear in LIST, we set LOC = NULL.
• Traverse the list using pointer variable PTR and comparing ITEM with INFO [PTR] at
each node.
• While traversing keep track of the location of preceding node by using pointer variable
SAVE. The SAVE and PTR are updated by assignment.
Deleting the Node with Given ITEM of Information
SAVE = PTR and PTR = LINK [PTR]
• The traversing continues as long as INFO [PTR] ! =ITEM.
• The PTR contains the location of node N and SAVE contains the location LOCP of
the node preceding node N.
Algorithm
FINDB (INFO, LINK, START, ITEM, LOC, LOCP]
This function finds the location LOC of the first node N which contains ITEM and the location LOCP of the
node preceding N. if ITEM does not appear in the list, then the function set LOC=NULL and it item appears
in the first node then it sets LOCP=NULL]
1.[list empty?] if START=NULL
then set LOC=NULL and LOCP=NULL and
return. [end of if structure]
2. [ITEM in first node?] if
INFO[START]==ITEM then
Set LOC=START and LCOP=NULL and
return. [end of if structure]
3 . Set SAVE=START and PTR=LINK[START]
Algorithm
5. if INFO[PTR]=ITEM then
Set LOC= PTR and LOCP=SAVE and return
[end of IF structure]
6. set SAVE=PTR and PTR=LINK[PTR] [ updates
pointers] [end of step
4 loop]
7. set LOC=NULL. [search unsuccessful]
8. return .
Algorithm for Deletion
DELETE(INFO,LINK,START,AVAIL,ITEM)
This algorithm deletes from a linked list the first node N which contains the
given ITEM of information.
1.[Use previous procedure to find the location of N and its
preceding node.] Call FINDB (INFO, LINK,START,ITEM,
LOC, LOCP)
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]
Algorithm for Deletion
[End of If structure]
4. [Return deleted node to the AVAIL list]
set LINK[LOC] = AVAIL and AVAIL = LOC
5. Exit
Singly Linked Lists and Arrays
Singly linked list Array
Elements are stored in linear Elements are stored in linear
order, accessible with links. order, accessible with an
index.
Do not have a fixed size. Have a fixed size.
Cannot access the previous Can access the previous
element directly. element easily.
No binary search. Binary search.
Header Linked Lists
• A header linked lists is a linked list which always contain a special node,
called Header node at the beginning of the list.
1. A Grounded Header list : It is a header list where the last node contains the
null pointer. The term grounded comes from the fact that many texts use the
ground symbol to indicate the null pointer.
2. A Circular Header List: It is a header list where the last node points back to
the header node.
• The list pointer START always points to the header node.
• LINK [START] = NULL indicates that grounded header list is empty.
• LINK [START] = START indicates that a circular header list is empty.
Header Linked Lists
SSTTAARRTT
HEADER NODE
START
GROUNDED HEADER LIST
HEADER NODE
CIRCULAR HEADER LIST
CIRCULAR HEADER
LIST
NAME SSN SEX SALRY LINK
1 0
START • START = 5.
2 SAIRA 193-54-667 FEMALE 22, 800 12 • Adnan is the
last
5 3 ASAD MALE 19, 000 7 employee,
NAINA 293-64-266 FEMALE 27, 200 14 LINK [10] =
4 5
191, 600 6
5 913-54-777
AREEBA FEMALE 14, 700 9
The header record 6
may
also to NOMAN 009 MALE 16, 400 10
7
used
informati 563-24-787 11
on entire store 8
life. about NISHA FMALE 19, 000 2
AVAIL 9 443-98-201
ADNAN MALE 15, 500 5
the
1
8 0 13
1 RABIA 333-99-708 FEMALE 34, 200 4
1 1
1 113-50-333 FEMALE
ESHALL k
22, 800 3
2
Circular Linked List
• In circular linked list the algorithm begins with PTR = LINK [START]
(not PTR = START) and ends when PTR = START (not PTR = NULL).
• Circular linked list are frequently used instead of ordinary linked lists because
many operations are much easier to state and implement using header list. The
following are two properties of circular lists.
1. The null pointer is not used and hence all pointers contains valid addresses.
2. Every ordinary node has a predecessor, so the first node may not require a
special case.
Traversing a Circular Lists
( Traversing a circular Header List) Let list be a circular header list in memory. This
algorithm traverses list, applying an operation PROCESS to each node of LIST.
1. Set PTR = LINK [START]. [Initializes the pointer
PTR]
2. Repeat step 3 and 4 while PTR [PTRSTART
now points to the next node]
3. Apply PROCESS to INFO [PTR].
4. Set PTR = LINK [PTR]
[End of step 2 loop]
5. Exit
Doubly Linked Lists / Two Way Lists
• A two-way list is a linear collection of data elements called nodes, where each
node N into three parts.
1. An information field INFO which contains the data of N.
2. A pointer field FORW which contains the location of the next node in the list.
3. A pointer field BACK which contains the location of the preceding node in the list.
• This lists also requires two list pointer variables: FIRST, which points to the first node
in
list and LAST, which points to the last node in the list.
• The null pointer appears in the FORW field of the last node in the list and also in
the BACK field of the first node in the list.
TWO WAY LIST / DOUBLY LINKED LIST
INFO field of node N LAST
FIRST
BACK pointer field of node N
FORW pointer field of node N
Node N
TWO WAY LIST / DOUBLY LINKED LIST
NAME FORW BACK
FIRST 1 SAIRA 7 8
2 6
5
3 ASAD 11 5
4 NAIN 12 7
5 A
AREEBA 3 0
LAST
6 0
9 7 NOMA 4 1
8 N
NISHA 1 11
AVAIL 0 12
9 ADNA
N 2
10 1
0 RABIA 8 3
1 ESHALL 9 4
1