1 / 15

Linked Lists (Chapter 6)

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.

Download Presentation

Linked Lists (Chapter 6)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists(Chapter 6)

  2. Outline • Introduction • Singly Linked Lists • Doubly Linked Lists • Applications

  3. Sequential data structures in general, suffer from the following drawbacks: - Inefficient implementation of insertion and deletion operations - Inefficient use of storage memory

  4. 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.

  5. 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.

  6. 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)

  7. 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

  8. 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.

  9. 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.

  10. 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.

  11. 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

  12. 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.

  13. 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.

  14. 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)

  15. 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)

More Related