Code Fragment: LinkedDeque1



template <typename Object>
class LinkedDeque {
protected:                                      // local node structure
  // ... (insert Node here)
private:                                        // member data
  NodePtr header;				// pointer to header sentinel
  NodePtr trailer;				// pointer to trailer sentinel
  int sz;					// number of elements
public:
  LinkedDeque() {				// default constructor
    header = new Node;
    trailer = new Node;
    header->next = trailer;			// trailer follows header
    trailer->prev = header;			// header precedes trailer
    sz = 0;
  }
  // ...					// return first element
  Object& first() throw(DequeEmptyException) {
    if (isEmpty())
      throw DequeEmptyException("First of empty deque");
    return header->next->element;
  }
  void insertFirst(const Object& e) {		// insert new node at head
    NodePtr oldFirst = header->next;		// old first node
    NodePtr t = new Node(e, header, oldFirst);	// new node to insert
    oldFirst->prev = t;
    header->next = t;
    sz++;
  }
  void removeLast() {				// remove node at tail
    if (isEmpty())
      throw DequeEmptyException("Remove on empty deque");
    NodePtr old = trailer->prev;		// node to remove
    NodePtr newLast = old->prev;		// new last node
    trailer->prev = newLast;
    newLast->next = trailer;
    sz--;
    delete old;
  }
  // ... (many functions omitted)
};