Pai Ch06 Linklist
description
Transcript of Pai Ch06 Linklist
PROPRIETARY MATERIAL. © 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.
@ McGraw-Hill Education
Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by Tata McGraw Hill. This resource material is for Instructor's use only.
Linked ListsLinked Lists
(Chapter 6)(Chapter 6)
3
Introduction
Singly Linked Lists
Circularly Linked Lists
Doubly Linked Lists
Multiply Linked Lists
Applications
ADT for links
ADT for singly linked lists
Outline
4
Sequential data structures in general, Sequential data structures in general, suffer from the following drawbacks:suffer from the following drawbacks:
Inefficient implementation of insertion and Inefficient implementation of insertion and deletion operationsdeletion operations
Inefficient use of storage memoryInefficient use of storage memory
5
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. The operation and management of linked data
structures are less prone to instigate memory fragmentation.
6
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.
7
Implementation of linked listsImplementation of linked lists
to frame chunks of memory into nodes with to frame chunks of memory into nodes with the desired number of data items and fieldsthe desired number of data items and fields
to determine which nodes are free and which to determine which nodes are free and which have been allotted for usehave been allotted for use
to obtain nodes from the free storage area or to obtain nodes from the free storage area or storage pool for use storage pool for use GETNODE(X)GETNODE(X)
In java, to return or dispose nodes from the In java, to return or dispose nodes from the reserved area or pool to the free area after use reserved area or pool to the free area after use will be done automatically by JVM.will be done automatically by JVM.
8
Singly Linked ListsSingly Linked Lists
A A singly linked listsingly linked list is a linear data structure is a linear data structure each node of which has one or more data item each node of which has one or more data item fields (DATA) but only a fields (DATA) but only a single linksingle link field field (LINK)(LINK)
LINK
DATA
9
Insertion in a singly linked listInsertion 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;LINK(X) = LINK(NODE); Node X points to the original right neighbour of node NODE */LINK(NODE) = X;
end INSERT_SL.
10 class Node {class Node {String element;String element;Node next;Node next;
public Node (String e){public Node (String e){element = e;element = e;
}}}}public class linked_list {public class linked_list { public static void main(String[] args) { public static void main(String[] args) { Node head = null;Node head = null; Node tail = null;Node tail = null; Node temp = null;Node temp = null; Node traverse=null;Node traverse=null; temp = new Node ("KTrg");temp = new Node ("KTrg"); tail=temp;tail=temp; head=temp;head=temp; temp = new Node("Dungun");temp = new Node("Dungun"); temp.next=head;temp.next=head; head=temp;head=temp;
traverse=head;traverse=head;while (traverse != null){while (traverse != null){ System.out.println("traverse "+traverse.element); System.out.println("traverse "+traverse.element); traverse=traverse.next; traverse=traverse.next;}}
} }}}
11
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.
Deletion in a singly linked list
12
Locate Node
public Link find(int key) {// find link with given key, assumes non-empty list
Link current = first; // start at ‘first’while(current.iData != key) // while no match,{
if(current.next == null) // if end of list,return null; // didn’t find it
else // not end of list,current = current.next; // go to next link
}return current; // found it
}// -------------------------------------------------------------
13
Delete Node
public Link delete(int key) // delete link with given key{ // (assumes non-empty list)
Link current = first; // search for linkLink previous = first;while (current.iData != key){
if (current.next == null)return null; // didn’t find it
else{
previous = current; // go to next linkcurrent = current.next;
}} // found itif (current == first) // if first link,
first = first.next; // change firstelse // otherwise,
previous.next = current.next; // bypass itreturn current;
}// -------------------------------------------------------------
14
15
Insert First in double-headed listpublic void insertFirst(long dd) // insert at front of list{
Link newLink = new Link(dd); // make new linkif ( isEmpty() ) // if empty list,
last = newLink; // newLink <-- lastnewLink.next = first; // newLink --> old firstfirst = newLink; // first --> newLink
}
Insert Last in double-headed listpublic void insertLast(long dd) // insert at end of list{
Link newLink = new Link(dd); // make new linkif ( isEmpty() ) // if empty list,
first = newLink; // first --> newLinkelse
last.next = newLink; // old last --> newLinklast = newLink; // newLink <-- last
}
16
Singly linked list – Stack implementationDouble-headed linked list – Queue implementation.
17
Sorted List
18
19
Circularly Linked ListsCircularly Linked Lists For further improvement in processing of a For further improvement in processing of a
singly linked list one may replace the null pointer singly linked list one may replace the null pointer in the last node with the address of the first node in the last node with the address of the first node in the list. in the list.
Such a list is called as a Such a list is called as a circularly linked listcircularly linked list or a or a circular linked listcircular linked list or simply a or simply a circular listcircular list..
20
Advantages of circularly linked listAdvantages of circularly linked list accessibility of a node accessibility of a node delete operations delete operations relative efficiency in the implementation of relative efficiency in the implementation of
list based operations list based operations
Disadvantages of circularly linked listsDisadvantages of circularly linked lists getting into an infinite loop owing to the getting into an infinite loop owing to the
circular nature of pointers in the list circular nature of pointers in the list solution to this problem is to designate a solution to this problem is to designate a special node to act as the head of the list, special node to act as the head of the list, known as known as list headlist head or or head nodehead node
21
headed circularly linked listheaded circularly linked list or or circularly circularly linked list with head nodelinked list with head node. .
HEAD NODE
22
To enhance greater flexibility of movement, the linked To enhance greater flexibility of movement, the linked representation could include two links in every node, each representation could include two links in every node, each of which points to the nodes on either side of the given of which points to the nodes on either side of the given nodenode
Such a linked representation known as Such a linked representation known as doubly linked listdoubly linked list
Each node has one or more data fields but only two link Each node has one or more data fields but only two link fields termed left link (fields termed left link (LLINKLLINK) and right link () and right link (RLINKRLINK). ).
The LLINK field of a given node points to the node on its 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. left and its RLINK field points to the one on its right.
A doubly linked list may or may not have a head node. A doubly linked list may or may not have a head node. Again, it may or may not be circular.Again, it may or may not be circular.
Doubly Linked Lists
23
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
LLINK RLINK
DATA
24
Insertion in a doubly linked listInsertion 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.
25
Deletion in a doubly linked listDeletion 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);
Call RETURN(X); } end DELETE_DL.
26
Multiply Linked ListsMultiply Linked Lists
A multiply linked list as its name suggests is a A multiply linked list as its name suggests is a linked representation with multiple data and linked representation with multiple data and link fields link fields
27
ApplicationsApplications
Addition of polynomials Addition of polynomials Representation of a sparse matrixRepresentation of a sparse matrix
28
ADT for Links
Data objects: Addresses of the nodes holding data and null links
Operations:
Allocate node (address X) from Available Space to accommodate data
GETNODE ( X)
Return node (address X) after use to Available Space
RETURN(X)
Store a value of one link variable LINK1 to another link variable LINK2
STORE_LINK ( LINK1, LINK2)
Store ITEM into a node whose address is X
STORE_DATA (X, ITEM)
Retrieve ITEM from a node whose address is X
RETRIEVE_DATA (X, ITEM)
29
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)
30
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)