Professional Documents
Culture Documents
package ListStack;
/** Cell models one cell used to assemble a linked list. */
public class Cell
{ private Object val; // value in the cell
private Cell next; // the address of the next cell in the list
/** getNext returns the address of the cell chained to this one */
public Cell getNext()
{ return next; }
/** setNext resets the address of the cell chained to this one
* @param link - the address of the Cell that is chained to this one */
public void setNext(Cell link)
{ next = link; }
}
package ListStack;
/** Stack models a stack data structure (using a linked list) */
public class Stack
{ private Cell top; // the top cell of the stack, which is modelled by
// a linked list of Cells.
// invariant: top == null means the stack is empty
import ListStack.*;
/** TestStack does simple tests of linked-list stacks. */
public class TestStack
{ public static void main(String[] args)
{ // push and pop some integers:
Stack s1 = new Stack();
// wrap the integers:
Integer i1 = new Integer(1);
Integer i2 = new Integer(2);
// push them:
s1.push(i1);
s1.push(i2);
// pop one:
Object ob = s1.pop();
int v = ((Integer)ob).intValue();
System.out.println(v); // prints 2
System.out.println(s1.isEmpty()); // prints false
s1.push(new Integer(3));
package Queue;
/** Cell models a cell of a linked list. */
public class Cell
{ private Object val; // value in the cell
private Cell next; // the address of the next cell in the list
/** getNext returns the address of the cell chained to this one */
public Cell getNext()
{ return next; }
/** setNext resets the address of the cell chained to this one
* @param link - the address of the Cell that is chained to this one */
public void setNext(Cell link)
{ next = link; }
}
package Queue;
/** Queue implements a queue */
public class Queue
{ private Cell front; // marks the first Cell of the queue
private Cell rear; // marks the last Cell of the queue
/** dequeue removes the element from the front of the queue
* @return the element removed
* @exception RuntimeException if the queue is empty */
public Object dequeue()
{ if ( front == null )
{ throw new RuntimeException("Queue error: queue empty"); }
Object answer = front.getVal();
if ( front == rear ) // queue contains just this one element?
{ rear = null; } // if true, then set queue to empty
front = front.getNext();
return answer;
}
package Queue;
public class TestCell
{
/** lengthOf calculates the number of cells in a linked list
* @param l - the leading cell of the list
* @return the length of the linked list */
public int lengthOf(Cell l)
{ int length = 0;
if ( l == null )
{ length = 0; }
else { length = 1 + lengthOf(l.getNext());}
return length;
}
/** append constructs a list that is its two arguments connected together.
* @param l1 - the first list
* @param l2 - the second list
* @return the address of the concatenated list */
public Cell append(Cell l1, Cell l2)
{ Cell answer;
if ( l1 == null )
{ answer = l2; }
else { Cell c = append(l1.getNext(), l2);
answer = new Cell( l1.getVal(), c);
}
return answer;
}