Lecture 5
Linked List on Data Structure
(Basic)
Pointers, Pointer Arrays and Linked lists
Pointer
A variable P is called a Pointer if P points to an element in an array called DATA,
i.e., P contains the address of an element in DATA. Analogously, an array PTR is
called a pointer array if each element of PTR is a pointer. Pointers and pointer
arrays are used to facilitate the processing of information in DATA.
A pointer is just like an int: a number. It happens to be a number that identifies a
memory location, and if something is stored in that memory location you can call
it an address. Like an int, a pointer can be stored in a variable. A variable that
stores a pointer could be called a pointer variable
Pointer Arrays
Pointers: store an address. Used to access objects
Arrays: indexed collections of identical-type objects. (aggregate)
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.
Disadvantages of arrays as storage data structures:
slow insertion in ordered array
Fixed size
Linked lists solve some of these problems
Linked lists are general purpose storage data structures
Linked lists
A linked list, or one way list, is a linear collection of data elements, called
nodes, where the linear order is given by means of pointers.
Each node is divided into two parts:
The first part (or Data part), contains the information of the element, and
The second part (or Address Part), called the link field or next pointer field,
contains the address of the next node in the list.
The pointer of the last node contains a special value,called the null pointer.
A pointer variable called START which contains the address of the first
node.
A special case is the list that has no nodes, such a list is called the null list
or empty list and is denoted by the null pointer in the variable START.
Start
node
Data Next
node
Data Next
node
Data
Linked list with 3 nodes
node
A
data
pointer
Representation of Linked lists in memory
INFO
START
3
LINK
START=3, INFO[3]=45
67
LINK[3]=2, INFO[2]=67
45
LINK[2]=5, INFO[5]=75
80
LINK[5]=4, INFO[4]=80
75
90
LINK[7]=0, NULL value, So the list has ended
6
7
8
LINK[4]=7, INFO[7]=90
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
START
INFO LINK
X
Fig : PTR : = LINK[PTR]
Traversing a linked lists
Some examples 5.1, 5.5, 5.3, 5.4, 5.5
PTR
START
INFO LINK
X
Fig : PTR : = LINK[PTR]
Traversing a linked lists
Example 5.6/p5.9
For printing the information at each node of a linked list, must traverse the list.
Algorithm 5.1 : PRINT(INFO, LINK, START)
Algorithm Prints the information at each node of the list.
1. Set PTR : =START.
2. Repeat steps 3 and 4 while PTR : NULL:
3. Write : INFO[PTR].
4. Set PTR : =LINK[PTR]. (updates pointer)
(end of step 2 loop)
5. Exit.
Traversing a linked lists
Example 5.7/p5.9
For Finding the number NUM of elements in a linked list, must traverse the list.
Algorithm 5.1 : COUNT(INFO, LINK, START, NUM)
1. Set NUM: =0.
2. . Set PTR : =START.
3. Repeat steps 4 and 5 while PTR : NULL:
4. Set NUM : =NUM+1 (increase NUM by 1).
5. Set PTR : =LINK[PTR] (updates pointer).
6. (end of step 3 loop)
7. Exit.
Memory allocation: Garbage collection
Memory space can be reused if a node is deleted from a list
i.e deleted node can be made available for future use
The operating system of a computer may periodically collect all the deleted space
on to the free storage list. Any technique which does this collection called garbage
collection
Computer
programs
Takes
space
avail list
Periodic
Collector
Garbage
(Deleted Space)
Avail List (Free space)
Sent to
avail list
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 Underflow occurs when
START = NULL and
There is an deletion operation
Insertion into a linked list
1.
2.
3.
Node N is to be inserted in to the list between nodes A and B
Three pointer fields are changed as follows:
The next pointer field of node A now points to the new node N, to which AVAIL previously
pointed.
AVAIL now point to the second node in the free pool, to which node N previously pointed.
The next pointer field of node N now points to node B, to which node A previously pointed.
START
Node A
START
Node
B
(a) Before insertion
Node
B
Node A
AVAIL
(a) After insertion
Node N
Node N
Free storage list
Inserting a new node
Possible cases of Insert Node
1.
2.
3.
4.
Insert into an empty list
Insert in front
Insert at back
Insert in middle
But, in fact, only need to handle two cases:
1.
2.
Insert as the first node (Case 1 and Case 2)
Insert in the middle or at the end of the list (Case 3 and Case 4)
Inserting a new node
INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit
2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]= ITEM
4. IF LOC = NULL then [Insert as 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
5. Exit.
Check for available
memory
Inserting a new node
INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit
2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]= ITEM
4. IF LOC = NULL then [Insert as first Node]
Set LINK[]= START and START=NEW.
Else: [Insert after node with location LOC]
Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW
5. Exit.
Create a new node
Inserting a new node
INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit
2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]= ITEM
4. IF LOC = NULL then [Insert as first Node]
Set LINK[NEW]= START and START=NEW.
Insert as first element
START
Else: [Insert after node with location LOC]
Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW
5. Exit.
NEW