Tuesday, October 18, 2011


The slides implement a Stack based on a Singly Linked list by *reimplementing* the necessary subset of methods of the Singly linked list class.

What *I* would suggest:
*Assume* you have a Singly Linked List class already. Make it a private data member. Adapt the Singly Linked to be a SLLStack by wrapping the methods.

class NodeStack<E>
{
protected SLL<E> sll; //
public NodeStack<E>() { // constructs an empty stack
sll = new SLL(); }
public int size() { return sll.size(); }
public boolean isEmpty() {return sll.size()==0;}
public void push(E elem) {
sll.insertFront(elem);
}
public E top() throws EmptyStackException {
     if (sll.isEmpty()) throw new EmptyStackException("empty");
return sll.returnFront(); // find some way to get access to the first elem
}
   public E pop() throws EmptyStackException {   if (sll.isEmpty()) throw new EmptyStackException("empty");       return sll.removeFront();}
}
dequeue (pronounced dee-kyoo) means remove from a queue (kyoo)

deque (pronounced deck) is a certain data structure

What about "my" way of writing a Queue based on a SLL?
class MyQueue<E>
{
protected SLL<E> sll; // reference to the head node  
void enqueue(E elem)
{
sll.insertAtEnd(elem);
}

E dequeue()
{
if (sll.isEmpty())
crashAndBurn();
return sll.removeFront();
}
}

Link to the sample midterm
https://docs.google.com/View?id=ajbqhgmq9qdz_59khz242gh&pli=1

The midterm will be Tuesday, Nov 1, 2011

No comments:

Post a Comment