Title: Lists
1Lists
2(No Transcript)
3The Position Abstract Data Type
4(No Transcript)
5(No Transcript)
6A position
p
, which is associated with some element
e
in a list
S
, does not change, even if the rank of
e
changes in
S
, unless
we explicitly remove
e
(or
change
its neighbors).
Example
0
1
2
3
4
5
6
q
p
s
After the first element (rank 0) is removed.
0
1
2
3
4
5
q
p
s
7(No Transcript)
8(No Transcript)
9(No Transcript)
10The List Abstract Data Type
11(No Transcript)
12(No Transcript)
13(No Transcript)
14(No Transcript)
15Invalid Positions
16Doubly Linked List Implementation
position
List ADT
DNode
Doubly linked list
17Doubly Linked List Implementation
18public interface Position Object
element()
19(No Transcript)
20(No Transcript)
21Interface Hierarchy for Positions
Position element()
impl.
Dnode element() getNext()
getPrev() setNext() setPrev()
setElement()
22(No Transcript)
23(No Transcript)
24(No Transcript)
25(No Transcript)
26(No Transcript)
27(No Transcript)
28(No Transcript)
29(No Transcript)
30(No Transcript)
31(No Transcript)
32(No Transcript)
33(No Transcript)
34public 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, BoundaryViolationExcepti
on / Returns the node before a given node in
the list. / public Position before(Position p)
throws InvalidPositionException,
BoundaryViolationException public Position
after(Position p) throws InvalidPositionExcep
tion, 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
36class InvalidPositionException extends Exception
public InvalidPositionException()
super() public InvalidPositionException(Stri
ng s) super(s) class EmptyContainerExeptio
n extends Exception public
EmptyContainerExeption() super() public
EmptyContainerExeption(String s) super(s)
class BoundaryViolationExeption extends
Exception public BoundaryViolationExeption()
super() public BoundaryViolationExeption(Str
ing s) super(s)
37Class NodeList
38(No Transcript)
39(No Transcript)
40(No Transcript)
41(No Transcript)
42(No Transcript)
43(No Transcript)
44(No Transcript)
45Interface Hierarchy for Lists
List first() last() isFirst() isLast()
before() after() replaceElement()
swapElement() insertFirst() insertLast()
impl.
NodeList .
46Data 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.
47public class GenerateList public static void
main(String args) Position temp NodeList x
new NodeList() for (int i 0 i lt 10 i)
temp x.insertFirst(new Integer(i))
try System.out.println( ((Integer)(temp.el
ement())).intValue()) catch
(InvalidPositionException e)