[   ^ to index...   |   <-- previous   |   next -->   ]

Implementing stacks/queues with vectors

Let's pretend that our Stack implementation uses a Vector object internally, with the standard Vector operations:

class Stack { // ... some stuff, then: private: Vector contents; };

How would you implement Stack::push() and Stack::pop()?

 
 
 
 
 
 
 
 
 
 

How about a Queue::enqueue() and Queue::dequeue()?

class Queue { // ... some stuff, then: private: Vector contents; };  
 
 
 
 
 
 
 
 
 

Key insights: 1. Once you have defined a class like Vector, you can reuse it without worrying about the details. 2. A stack and a queue are really just "weaker" versions of a general Vector.


Last modified: Wed Jul 19 18:53:02 PDT 2000