1 linked lists (lec 6). 2 introduction singly linked lists circularly linked lists doubly linked...

Click here to load reader

Post on 19-Jan-2016

243 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

Linked Lists (Chapter 6)Outline
Sequential data structures in general, suffer from the following drawbacks:
Inefficient implementation of insertion and deletion operations
Inefficient use of storage memory
*
Each node contains at least
A piece of data (any type)
Pointer to the next node in the list
Head: pointer to the first node
The last node points to NULL

A linked representation serves to counteract the drawbacks of sequential representation by exhibiting the following merits:
Efficient implementation of insertion and deletion
operations.
Unlike sequential data structures, there is complete absence of data movement of neighbouring
elements during the execution of these operations.
Efficient use of storage memory.
*
A linked representation of data structure known as a linked list is a collection of nodes.
Each node is a collection of fields categorized as data items and links.
*
Implementation of linked lists
to frame chunks of memory into nodes with the desired number of data items and fields
to determine which nodes are free and which have been allotted for use
to obtain nodes from the free storage area or storage pool for use GETNODE(X)
*
Singly Linked Lists
A singly linked list is a concrete data structure consisting of a sequence of nodes
Each node stores
next
elem
node
A
B
C
D
Singly Linked Lists
A singly linked list is a linear data structure each node of which has one or more data item fields (DATA) but only a single link field (LINK)
LINK
DATA
*
Update head to point to next node in the list
Allow garbage collector to reclaim the former first node
*
public class SLinkedListWithTail {
/** Default constructor that creates an empty list */
public SLinkedListWithTail() {
head = null;
tail = null;
}
*
Have old last node point to new node
Update tail to point to new node
*
Removing at the Tail
Removing at the tail of a singly linked list cannot be efficient!
*
Insertion in a singly linked list
Algorithm 6.1 : To insert a data element ITEM in a non empty singly liked list START, to the right of node NODE.
Procedure INSERT_SL(START, ITEM, NODE)
/* Insert ITEM to the right of node NODE in the list START */
Call GETNODE(X);
DATA(X) = ITEM;
the original right neighbour
*
Algorithm 6.3 : Deletion of a node which is to the right of node NODEX in a singly linked list START
Procedure DELETE_SL(START, NODEX)
*
Nodes store:
prev
next
elem
trailer
header
nodes/positions
elements
node
A
B
X
C
A
B
C
p
A
B
C
p
X
q
p
q
v.setElement(e)
(p.getNext()).setPrev(v) {link p’s old successor to v}
p.setNext(v) {link p to its new successor, v}
return v {the position for the element e}
*
A
B
C
D
p
A
B
C
A
B
C
D
p
t = p.element {a temporary variable to hold the return value}
(p.getPrev()).setNext(p.getNext()) {linking out p}
p.setNext(null)
Circularly Linked Lists
For further improvement in processing of a singly linked list one may replace the null pointer in the last node with the address of the first node in the list.
*
accessibility of a node
Disadvantages of circularly linked lists
getting into an infinite loop owing to the circular nature of pointers in the list
*
headed circularly linked list or circularly linked list with head node.
HEAD NODE
*
To enhance greater flexibility of movement, the linked representation could include two links in every node, each of which points to the nodes on either side of the given node
Such a linked representation known as doubly linked list
Each node has one or more data fields but only two link fields termed left link (LLINK) and right link (RLINK).
The LLINK field of a given node points to the node on its left and its RLINK field points to the one on its right.
A doubly linked list may or may not have a head node. Again, it may or may not be circular.
Doubly Linked Lists
Advantages of a doubly linked list
The availability of two links LLINK and RLINK permit forward and backward movement during the processing of the list.
The deletion of a node X from the list calls only for the value X to be known.
Disadvantages of a doubly linked list
memory requirement
Insertion in a doubly linked list
Algorithm 6.4 : To insert node X to the right of node Y in a headed circular doubly linked list P
Procedure INSERT_DL(X, Y)
Deletion in a doubly linked list
Algorithm 6.5 : Delete node X from a headed circular doubly linked list P
procedure DELETE_DL(P, X)
{RLINK(LLINK(X)) = RLINK(X);
LLINK(RLINK(X)) = LLINK(X);
Call RETURN(X); }
end DELETE_DL.
Multiply Linked Lists
*
*
Operations:
Allocate node (address X) from Available Space to accommodate data
GETNODE ( X)
RETURN(X)
Store a value of one link variable LINK1 to another link variable LINK2
STORE_LINK ( LINK1, LINK2)
STORE_DATA (X, ITEM)
RETRIEVE_DATA (X, ITEM)
Data objects:
A list of nodes each holding one (or more) data field(s) DATA and a single link field LINK. LIST points to the start node of the list.
Operations:
CHECK_LIST_EMPTY ( LIST) (Boolean function)
Insert ITEM into the list LIST as the first element
INSERT_FIRST (LIST, ITEM)
Insert ITEM into the list LIST as the last element INSERT_LAST (LIST, ITEM)
Insert ITEM into the list LIST in order
INSERT_ORDER (LIST, ITEM)
Advance Link to traverse down the list
ADVANCE_LINK (LINK)
STORE_DATA(X, ITEM)
Retrieve data of a node whose address is X and return it in ITEM
RETRIEVE_DATA(X, ITEM)
Retrieve link of a node whose address is X and return the value in LINK1
RETRIEVE_LINK (X, LINK1)
Circular linked lists
The last node points to the first node of the list
How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.)
Head
A
B
C
Each node points to not only successor but the predecessor
There are two NULL: at the first and last nodes in the list
Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards
Head
Array versus Linked Lists
Linked lists are more complex to code and manage than arrays, but they have some distinct advantages.
Dynamic: a linked list can easily grow and shrink in size.
We don’t need to know how many nodes will be in the list. They are created in memory as needed.
In contrast, the size of a C++ array is fixed at compilation time.
Easy and fast insertions and deletions
To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements.