150 likes | 168 Views
Linked Lists (Chapter 6). Outline. Introduction Singly Linked Lists Doubly Linked Lists Applications. Sequential data structures in general, suffer from the following drawbacks: - Inefficient implementation of insertion and deletion operations - Inefficient use of storage memory.
E N D
Outline • Introduction • Singly Linked Lists • Doubly Linked Lists • Applications
Sequential data structures in general, suffer from the following drawbacks: - Inefficient implementation of insertion and deletion operations - Inefficient use of storage memory
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 neighboring elements during the execution of these operations. • Efficient use of storage memory. The operation and management of linked data structures are less prone to create memory fragmentation.
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. • The data item fields hold the information content or data to be represented by the node. • The link fields hold the addresses of the neighbouring nodes or of those nodes which are associated with the given node as dictated by the application.
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) • to return or dispose nodes from the reserved area or pool to the free area after use RETURN(X)
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) DATA LINK
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 */ CallGETNODE(X); DATA(X) = ITEM; LINK(X) = LINK(NODE); Node X points to the original right neighbour of node NODE */ LINK(NODE) = X; end INSERT_SL.
Deletion in a singly linked list 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) if (START = NIL) then Call ABANDON_DELETE; /*ABANDON_DELETE terminates the delete operation */ else {TEMP =LINK(NODEX); LINK(NODEX) = LINK(TEMP); Call RETURN(TEMP); } end DELETE_SL.
Doubly Linked Lists • 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.
DATA LLINK RLINK 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) LLINK(X) = Y; RLINK(X) = RLINK(Y); LLINK(RLINK(Y)) = X; RLINK(Y) = X; end INSERT_DL.
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) if (X = P) then ABANDON_DELETE; else {RLINK(LLINK(X)) = RLINK(X); LLINK(RLINK(X)) = LLINK(X); CallRETURN(X); } end DELETE_DL.
ADT for Singly Linked Lists 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 if list LIST is empty 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)
Delete the first node from the list LIST DELETE_FIRST(LIST) • Delete the last node from the list LIST DELETE_LAST(LIST) • Delete ITEM from the list LIST DELETE_ELEMENT (LIST, ITEM) • Advance Link to traverse down the list ADVANCE_LINK (LINK) • Store ITEM into a node whose address is X 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)