Download presentation
Presentation is loading. Please wait.
1
Lists
3
The Position Abstract Data Type
6
A positionp, which is associated with some elemente in a list S, does not change, even if the rank ofe changes inS, unless we explicitly removee (orchange its neighbors). Example: qps 0123456 After the first element (rank 0) is removed. qps 012345
10
The List Abstract Data Type
15
Invalid Positions
16
List ADT position Doubly linked list DNode Doubly Linked List Implementation
18
public interface Position { Object element(); }
21
Dnode element(){…}; getNext(){…}; getPrev(){…}; setNext(){…}; setPrev(){…}; setElement(){…}; Interface Hierarchy for Positions impl. Position element();
34
public interface List { /** Returns the number of elements in this list. */ public int size(); /** Returns whether the list is empty. */ public boolean isEmpty(); /** Returns the first node in the list. */ public Position first(); /** Returns the last node in the list. */ public Position last(); /** Returns the node after a given node in the list. */ public Position next(Position p) throws InvalidPositionException, BoundaryViolationException; /** Returns the node before a given node in the list. */ public Position before(Position p) throws InvalidPositionException, BoundaryViolationException; public Position after(Position p) throws InvalidPositionException, BoundaryViolationException;
35
/** Inserts an element at the front of the list. */ public Position insertFirst(Object e); /** Inserts and element at the back of the list. */ public Position insertLast(Object e); /** Inserts an element after the given node in the list. */ public Position insertAfter(Position p, Object e) throws InvalidPositionException; /** Inserts an element before the given node in the list. */ public Position insertBefore(Position p, Object e) throws InvalidPositionException; /** Removes a node from the list. */ public Object remove(Position p) throws InvalidPositionException; /** Replaces the element stored at the given node. */ public Object replace(Position p, Object e) throws InvalidPositionException; public void swapElements( Position a, Position b ) throws InvalidPositionException; }
36
class InvalidPositionException extends Exception { public InvalidPositionException() {super();} public InvalidPositionException(String s) { super(s); } } class EmptyContainerExeption extends Exception { public EmptyContainerExeption() {super();} public EmptyContainerExeption(String s) { super(s); } } class BoundaryViolationExeption extends Exception { public BoundaryViolationExeption() {super();} public BoundaryViolationExeption(String s) { super(s); } }
37
Class NodeList
45
List first(); last(); isFirst(); isLast(); before(); after(); replaceElement(); swapElement(); insertFirst(); insertLast(); NodeList … …. Interface Hierarchy for Lists impl.
46
Data Structure Exercises 8.1 Generate a list containing 10 elements: 0, 1, …, 9. Note that some methods specified in the List interface (such as after(), insertAfter()) are missing. You have to provide the implementation of these methods in the NodeList class.
47
public class GenerateList { public static void main(String args[]) { Position temp; NodeList x = new NodeList(); for (int i = 0; i < 10; i++) { temp = x.insertFirst(new Integer(i)); try { System.out.println( ((Integer)(temp.element())).intValue()); } catch (InvalidPositionException e){} }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.