Linked List Outline
Linked List Outline
Linked List Outline
Linked Lists
Why linked lists
Start
node node node
Data Next Data Next Data
data pointer
linked lists
•A linked list organizes a collection of data items (elements ) such that elements
can easily be added to and deleted from any position in the list.
•Only references To next elements are updated in insertion and deletion
operations.
•There is no need to copy or move large blocks of data to facilitate insertion and
deletion of elements.
•Lists grow dynamically.
Representation of Linked lists in memory
INFO LINK
1
START START=3, INFO[3]=45
2 67 5 LINK[3]=2, INFO[2]=67
3
3 45 2 LINK[2]=5, INFO[5]=75
80 7 LINK[5]=4, INFO[4]=80
4
LINK[4]=7, INFO[7]=90
5 75 4
LINK[7]=0, NULL value, So the list has ended
6
7 90 0
8
Traversing a linked lists
LIST be a linked list in memory stored in linear arrays INFO and LINK with START
pointing to the first element and NULL indicating the end of LIST.
We want to traverse LIST in order to process each node exactly once.
Pointer variable PTR points to the node that is currently being processed.
LINK[PTR] points to the next node to be processed.
Thus update PTR by the assignment
PTR : =LINK[PTR]
PTR
Periodic
Collector
Computer
programs Garbage Sent to
(Deleted Space)
avail list
Takes
space …
avail list Avail List (Free space)
Overflow and Underflow
• Overflow:
– Sometimes data are inserted into a data structure but there is no
available space.
– This situation is called overflow
– Example: In linked list overflow occurs when
• AVAIL= NULL and
• There is an insertion operation
• Underflow:
– Situation:
• Want to delete data from data structure that is empty.
– Example: In linked list overflow occurs when
• START = NULL and
• There is an deletion operation
Insertion into a linked list
• Node N is to be inserted in to the list between nodes A and B
• Three pointer fields are changed as follows:
1. The next pointer field of node A now points to the new node N, to which AVAIL previously
pointed.
2. AVAIL now point to the second node in the free pool, to which node N previously pointed.
3. The next pointer field of node N now points to node B, to which node A previously pointed.
START
Node Node
A B
START
(a) Before insertion
Node Node
A B
5. Exit.
Inserting a new node
5. Exit.
Inserting a new node
5. Exit. NEW
Inserting a new node
5. Exit.
NEW