// This is the file to include in your code if you want access to the // complete AStack template class
// First, get the declaration for the base stack class #include"stack.h"
// This is the declaration for AStack. // Array-based stack implementation template <typename E> classAStack: public Stack<E> { private: int maxSize; // Maximum size of stack int top; // Index for top element E *listArray; // Array holding stack elements
public: AStack(int size =defaultSize) // Constructor { maxSize = size; top = 0; listArray = new E[size]; }
~AStack() { delete [] listArray; } // Destructor
voidclear(){ top = 0; } // Reinitialize
voidpush(const E& it){ // Put "it" on stack Assert(top != maxSize, "Stack is full"); listArray[top++] = it; }
E pop(){ // Pop top element Assert(top != 0, "Stack is empty"); return listArray[--top]; }
const E& topValue()const{ // Return top element Assert(top != 0, "Stack is empty"); return listArray[top-1]; }
// This is the file to include in your code if you want access to the // complete LStack template class
// Include the link class #include"link.h"
// First, get the declaration for the base stack class #include"stack.h"
// This is the declaration for LStack. // Linked stack implementation template <typename E> classLStack: public Stack<E> { private: Link<E>* top; // Pointer to first element int size; // Number of elements
voidclear(){ // Reinitialize while (top != NULL) { // Delete link nodes Link<E>* temp = top; top = top->next; delete temp; } size = 0; }
voidpush(const E& it){ // Put "it" on stack top = newLink<E>(it, top); size++; }
E pop(){ // Remove "it" from stack Assert(top != NULL, "Stack is empty"); E it = top->element; Link<E>* ltemp = top->next; delete top; top = ltemp; size--; return it; }
const E& topValue()const{ // Return top value Assert(top != 0, "Stack is empty"); return top->element; }
longfact(int n){ // Compute n! recursively // To fit n! into a long variable, we require n <= 12 Assert((n >= 0) && (n <= 12), "Input out of range"); if (n <= 1) return1; // Base case: return base solution return n * fact(n-1); // Recursive call for n > 1 }
// This is the file to include in your code if you want access to the // complete AQueue template class
// First, get the declaration for the base stack class #include"queue.h"
// This is the declaration for AStack. // Array-based queue implementation template <typename E> classAQueue: public Queue<E> { private: int maxSize; // Maximum size of queue int front; // Index of front element int rear; // Index of rear element E *listArray; // Array holding queue elements
public: AQueue(int size =defaultSize) { // Constructor // Make list array one position larger for empty slot maxSize = size+1; rear = 0; front = 1; listArray = new E[maxSize]; }
~AQueue() { delete [] listArray; } // Destructor
voidclear(){ rear = 0; front = 1; } // Reinitialize
voidenqueue(const E& it){ // Put "it" in queue Assert(((rear+2) % maxSize) != front, "Queue is full"); rear = (rear+1) % maxSize; // Circular increment listArray[rear] = it; }
E dequeue(){ // Take element out Assert(length() != 0, "Queue is empty"); E it = listArray[front]; front = (front+1) % maxSize; // Circular increment return it; }
const E& frontValue()const{ // Get front value Assert(length() != 0, "Queue is empty"); return listArray[front]; }
// This is the file to include in your code if you want access to the // complete LQueue template class
// Include the link class #include"link.h"
// First, get the declaration for the base stack class #include"queue.h"
// Implementations for linked queue function members // Linked queue implementation template <typename E> classLQueue: public Queue<E> { private: Link<E>* front; // Pointer to front queue node Link<E>* rear; // Pointer to rear queue node int size; // Number of elements in queue
voidenqueue(const E& it){ // Put element on rear rear->next = newLink<E>(it, NULL); rear = rear->next; size++; }
E dequeue(){ // Remove element from front Assert(size != 0, "Queue is empty"); E it = front->next->element; // Store dequeued value Link<E>* ltemp = front->next; // Hold dequeued link front->next = ltemp->next; // Advance front if (rear == ltemp) rear = front; // Dequeue last element delete ltemp; // Delete link size --; return it; // Return element value }
const E& frontValue()const{ // Get front element Assert(size != 0, "Queue is empty"); return front->next->element; }