scranton.list
Class PositionalListVia2Stacks

java.lang.Object
  extended byscranton.list.PositionalListVia2Stacks
All Implemented Interfaces:
PositionalList

public class PositionalListVia2Stacks
extends java.lang.Object
implements PositionalList

LinkedSList implements the PositionalList interface with a pair of stacks. A positional list may be specified as an ordered pair this = (i, L) where L is an n-tuple, L = () or L = (a0, a1, ..., an-1, and -1 <= i < #(L) Initially this = (-1, ()) The first ordinate, i, is the position indicator. It is either set to -1 to indicate the current position is undefined or 0<= i < #(L), indicating the current active location. Mapping: The list specification, (i,L) maps onto the attributes in LinkedSList as follows: If L = (), then left and current are empty. else if i != -1, left contains a0, ..., ai-1 with ai-1 at the top of the stack current contains ai, ..., an-1 with ai at the top of the stack


Field Summary
protected  Stack current
          For i>0, current contains ai, ..., an-1 with ai at the top of the stack
protected  Stack left
          For i>0, left contains a0, ..., ai-1 with ai-1 at the top of the stack
 
Constructor Summary
PositionalListVia2Stacks()
          Initialize the list as a pair of empty stacks.
 
Method Summary
 boolean atFront()
          Reports whether or not current viewing position is at front of list
 boolean atRear()
          Reports whether or not current viewing position is at rear of list
 PositionalList factory()
          Returns an empty PositionalList implementation of the type used in the method's application.
 void insert(java.lang.Object obj)
          Insert obj at the current position in the list Pre: #(L) = 0 or 0 <= i Post: L' = #(L)+1 if #(L)=0 then i'=0, L' = (obj) else if i>#(L)-1 then i' = #(L')-1, L' = (L, obj) else i' = i, L' = (a0, ..., ai-1, obj, ai, ..., an-1) Mapping: obj is pushed into the stack, current, on top of other objects in that stack
 boolean isEmpty()
          Reports whether or not the list is empty (i.e., contains no items).
 void next()
          Moves current viewing position one object towards the rear, if possible.
 void previous()
          Moves current viewing position one place towards the front, if possible.
 java.lang.Object remove()
          Removes the current item from the list
 void replace(java.lang.Object obj)
          Replaces item at current position in the list with obj.
 void toFront()
          Moves current viewing position to front of list Pre: none Post: L'==(), R'==(L, R)
 void toRear()
          Moves current viewing position to rear of list Pre: none Post: L'==(L, R), R'==()
 java.lang.String toString()
          Return a string representation of a positonal list.
 java.lang.Object view()
          Returns a reference to the current item in list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

left

protected Stack left
For i>0, left contains a0, ..., ai-1 with ai-1 at the top of the stack


current

protected Stack current
For i>0, current contains ai, ..., an-1 with ai at the top of the stack

Constructor Detail

PositionalListVia2Stacks

public PositionalListVia2Stacks()
Initialize the list as a pair of empty stacks.

Method Detail

insert

public void insert(java.lang.Object obj)
Insert obj at the current position in the list Pre: #(L) = 0 or 0 <= i Post: L' = #(L)+1 if #(L)=0 then i'=0, L' = (obj) else if i>#(L)-1 then i' = #(L')-1, L' = (L, obj) else i' = i, L' = (a0, ..., ai-1, obj, ai, ..., an-1) Mapping: obj is pushed into the stack, current, on top of other objects in that stack

Specified by:
insert in interface PositionalList
Parameters:
obj - New item inserted at the current position.

remove

public java.lang.Object remove()
Description copied from interface: PositionalList
Removes the current item from the list

Specified by:
remove in interface PositionalList
Returns:
Object, the item that was removed Pre: !atRear(), equivalently R!=() Post: R = (r0, R') Returns: r0

view

public java.lang.Object view()
Description copied from interface: PositionalList
Returns a reference to the current item in list.

Specified by:
view in interface PositionalList
Returns:
Object, a reference to current item in list Pre: !atRear(), equivalently R!=() Post: this' = this Returns: r0

replace

public void replace(java.lang.Object obj)
Description copied from interface: PositionalList
Replaces item at current position in the list with obj. Pre: !atRear(), equivalently R!=() Post: r0' = obj

Specified by:
replace in interface PositionalList
Parameters:
obj - The object replacing the current object in the list.

toFront

public void toFront()
Description copied from interface: PositionalList
Moves current viewing position to front of list Pre: none Post: L'==(), R'==(L, R)

Specified by:
toFront in interface PositionalList

toRear

public void toRear()
Description copied from interface: PositionalList
Moves current viewing position to rear of list Pre: none Post: L'==(L, R), R'==()

Specified by:
toRear in interface PositionalList

previous

public void previous()
Description copied from interface: PositionalList
Moves current viewing position one place towards the front, if possible. Pre: L!=() Post: L=(L', l#(L)-1), R'=(l#(L)-1, R)

Specified by:
previous in interface PositionalList

next

public void next()
Description copied from interface: PositionalList
Moves current viewing position one object towards the rear, if possible. Pre: R!=() Post: L'==(L, r0), R = (r0, R')

Specified by:
next in interface PositionalList

atFront

public boolean atFront()
Description copied from interface: PositionalList
Reports whether or not current viewing position is at front of list

Specified by:
atFront in interface PositionalList
Returns:
boolean true iff current position is front Pre: none Post: P'==P Returns: L==()

atRear

public boolean atRear()
Description copied from interface: PositionalList
Reports whether or not current viewing position is at rear of list

Specified by:
atRear in interface PositionalList
Returns:
boolean true iff current position is rear Pre: none Post: P'==P Returns: R==()

isEmpty

public boolean isEmpty()
Description copied from interface: PositionalList
Reports whether or not the list is empty (i.e., contains no items).

Specified by:
isEmpty in interface PositionalList
Returns:
boolean true iff the list is empty. Pre: none Post: P'==P Returns: L==() && R==()

factory

public PositionalList factory()
Description copied from interface: PositionalList
Returns an empty PositionalList implementation of the type used in the method's application. This method may be required to construct an object of the correct class during complex type extensions

Specified by:
factory in interface PositionalList
Returns:
An empty object of the implementaiton's class

toString

public java.lang.String toString()
Description copied from interface: PositionalList
Return a string representation of a positonal list. The format of the string is "Ofront\t...\tOrear"

Specified by:
toString in interface PositionalList