© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
© 2004 Goodrich, Tamassia Skip Lists1 S0S0 S1S1 S2S2 S3S
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
Linked Lists Chapter 4.
Queues and Linked Lists
Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
The Ranked Sequence ADT Definition A ranked sequence is a collection of items arranged in a linear order, where each item has a rank defining the relative.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Review Learn about linked lists
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
© 2004 Goodrich, Tamassia Stacks1 Abstract Data Types (ADTs) An abstract data type (ADT) is a contract between the user of a data structure and its implementor.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Lists. The Position Abstract Data Type A positionp, which is associated with some elemente in a list S, does not change, even if the rank ofe changes.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Click to edit Master text styles Stacks Data Structure.
CSCE , SPRING 2016 INSTRUCTOR: DR. NANCY M. AMATO 1.
LINKED LISTS.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Ch7. List and Iterator ADTs
Sequences and Iterators
EEL 4854 IT Data Structures Linked Lists
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Array Lists, Node Lists & Sequences
Ch7. List and Iterator ADTs
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sequences 11/22/2018 3:25 AM Doubly-Linked Lists Doubly-Linked Lists.
Linked Lists.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
CS212D: Data Structures Week 5-6 Linked List.
Linked Lists & Iterators
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Presentation transcript:

© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists

© 2004 Goodrich, Tamassia Node-Lists2 Using an index is not the only means of referring to the place where an element appears in a list (sequence). If we have a list S implemented with a (singly or doubly) linked list, then its natural to use a node instead of an index to identify where to access and update S. Well define the node list ADT, which abstracts the concrete linked list data structure using a position ADT that abstracts the notion of place in a node list.

© 2004 Goodrich, Tamassia Node-Lists Node-Based Operations Assuming List S be implemented using (singly or doubly) linked list. We would like to define methods for S that take nodes as parameters and provide nodes as return types. For instance, we could define a method remove (v) that removes the element of S at node v of the list. This could be executed in O(1) time, by simplylinking out this node by updating next and prev links of its neighbors.

© 2004 Goodrich, Tamassia Node-Lists4 Similarly, we could insert, in O(1) time, a new element e into S with an operation such as addAfter(v,e), that specifies the node v after which the node of the new element should be inserted. In this case, we simply linking in this new node. By defining methods of a list ADT by adding such node-based operations requires much more information about the implementation of our list. Certainly, its desirable to be able to use either a singly or doubly linked list, without revealing this detail to a user. To abstract and unify the different ways of storing elements in the various implementations of a list, we introduce the concept of position, which formalizes the notion of place of an element relative to others in the list.

© 2004 Goodrich, Tamassia Node-Lists Position ADT So as to safely expand the set of operations for lists, we abstract a notion of position that allows us to enjoy the efficiency of doubly or singly linked list implementation, without violating object-oriented design principles. We view a list as a collection of elements that stores each element at a position and that keeps these positions arranged in a linear order.

© 2004 Goodrich, Tamassia Node-Lists6 Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It supports just one method: object element(): returns the element stored at this position

© 2004 Goodrich, Tamassia Node-Lists7 Properties of Position ADT A position is always defined relatively, that is in terms of its neighbors. In a list, a position p will always be after some position q andbefore some position s (unless p is the first or last position). A position p, which is associated with some element e in a list S, does not change even if the index of e changes in S, unless we explicitly remove e (and, hence, destroy position p). Moreover, the position p does not change even if we replace or swap the element e stored at position p with another element.

© 2004 Goodrich, Tamassia Node-Lists Node List ADT Using the concept of position to encapsulate the idea of node in a list, we can define another type of sequence (list) ADT, called node list ADT. The node list ADT models a sequence of positions storing arbitrary objects. It establishes a before/after relation between positions. 1.Generic methods: size(), isEmpty()

© 2004 Goodrich, Tamassia Node-Lists9 2. Accessor Methods This Node List ADT supports the following methods for accessing elements in a list S :(All these methods return type position) first ():Return the position of the first element of S ; an error occurs if S is empty. last ():Return the position of the last element of S ; an error occurs if S is empty. prev ( p ):Return the position of the element of S preceding the one at position p ; an error occurs if p is the first position. next ( p ):Return the position of the element of S following the one at position p ; an error occurs if p is the last position. The above methods allow us to refer to relative positions in a list, starting at the start or end, and to move incrementally up or down the list S.

© 2004 Goodrich, Tamassia Node-Lists10 3. Update Methods In addition to the accesor methods and the generic methods, we also include the following update methods for the node list ADT, that take position objects as parameters and/or provide position object in return values: set(p,e): Replace the element at position p with e, returning the element formerly found at position p. addFirst(e): Insert a new element e into S as the first element. addLast(e): Insert a new element e into S as the last element. addBefore(p,e): Inserts a new element e into S before position p. addAfter(p,e): Inserts a new element e into S after position p. remove(p): Remove and return the element at position p in S, invalidating this position p in S.

© 2004 Goodrich, Tamassia Node-Lists11 The node list ADT allows us to view an ordered collection of objects ( list ) in terms of their places, without worrying about the exact way those places are represented (or, implemented). See Figure below. ProvidenceNew YorkParisBaltimore rqsp Figure 6.1: A node list. The positions in the current order are p, q, r, and s.

© 2004 Goodrich, Tamassia Node-Lists12 No Redundancy They may at first seem to be redundancy in the above set of operations for the node list ADT, since we can perform operation: addFirst(e) with addBefore(first(),e), and addLast(e) with addAfter(last(),e). But these substitutions can only be done for nonempty list.

© 2004 Goodrich, Tamassia Node-Lists13 Error Conditions Note that an error condition occurs if a position passed as argument to one of the list operations (methods) is invalid. Reasons for a position p to be invalid include: p = null p was previously removed from the list p is a position of a different list p is the first position of the list and we call prev(p) p is the last position of the list and we call next(p).

© 2004 Goodrich, Tamassia Node-Lists14 Example 6.2 We show a series of operations for an initially empty list node S. We use variables p 1, p 2 and so on, to denote different positions, and we show the object currently stored at such a position in parentheses. OperationOutputS isEmpty()true(-) last()error(-) addFirst(8)-(8)(8) first()p 1 (8)(8) addAfter(p 1,5)-(8,5) next(p 1 )p 2 (5)(8,5) addBefore(p 2,3)-(8,3,5) prev(p 2 )P 3 (3)(8,3,5) addFirst(9)-(9,8,3,5) last()p 2 (5)(9,8,3,5) remove(first())9(8,3,5) Set(P 3,7)3(8,7,5) addAfter(first(),2)-(8,2,7,5) Size()4(8,2,7,5)

© 2004 Goodrich, Tamassia Node-Lists15 Applications of Node List ADT The node list ADT, with its built-in notion of position, is useful in a number of settings. Following are two examples: a program that simulates a game of cards could model each persons hand as a node list. Inserting and removing cards from a persons hand could be implemented using the methods of the node list ADT, with the positions being determined by the ordering of cards in the suit. a simple text editor embeds the notion positional insertion and removal relative to a cursor, that represents the current position in the list of characters of text being edited.

© 2004 Goodrich, Tamassia Node-Lists16 Implementing (Array list) Vector ADT with Node-List ADT Vector ADT: size() and isEmpty() get(integer i) set(integer i, object e) add(integer i, object e) remove(integer i) List ADT: size(), isEmpty() first(), last() prev(p), next(p) set(p, e) insertBefore(p, e) insertAfter(p, e) insertFirst(e) insertLast(e) remove(p)

© 2004 Goodrich, Tamassia Node-Lists17 Node List ADT Exceptions An interface for the node list ADT uses the following exceptions to indicate error conditions: BoundaryViolationException : Thrown if an attempt is made to access an element whose position is outside the range of positions of the list, (ex., calling next on the last position). InvalidPositionException : Thrown if a position provided as argument in invalid, (ex., it is null or it has no associated list).

© 2004 Goodrich, Tamassia Node-Lists Doubly Linked List Implementation A doubly linked list provides a natural implementation of the List ADT Nodes implement position ADT and store: element link to the previous node link to the next node Special trailer and header sentinel nodes Nodes thus define a method element that return the element stored at the node prevnext elem trailer header nodes / positions elements node

© 2004 Goodrich, Tamassia Node-Lists19 Insertion We visualize operation addAfter(p, X), which returns position q ABXC ABC p ABC p X q pq

© 2004 Goodrich, Tamassia Node-Lists20 Insertion Algorithm Algorithm addAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p){link v to its predecessor} v.setNext(p.getNext()){link v to its successor} (p.getNext()).setPrev(v){link ps old successor to v} p.setNext(v){link p to its new successor, v} return v{the position for the element e}

© 2004 Goodrich, Tamassia Node-Lists21 Deletion We visualize remove(p), where p = last() ABCD p ABC D p ABC

© 2004 Goodrich, Tamassia Node-Lists22 Deletion Algorithm Algorithm remove(p): t = p.element(){a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()){linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null){invalidating the position p} p.setNext(null) return t

© 2004 Goodrich, Tamassia Node-Lists23 Performance In the implementation of the Node List ADT by means of a doubly linked list, we conclude the following: The space used by a list with n elements is O(n) The space used by each position of the list is O(1) All the methods of the Node List ADT run in O(1) time Operation element() of the Position ADT runs in O(1) time Thus, a doubly linked list is an efficient implementation of the Node List ADT