due: Wednesday, April 15, 1998, electronically by 11:30 pm
Despite their different behavior, one can implement a queue data structure by employing two instances of a stack data structure. This implementation is efficient: n queue operations can be performed using on average a constant number of stack operations per queue operation. Assuming that each stack operation takes a constant amount of work, then performing n queue operations can take only linear (O(n)) time.
What we've provided
As a starting point we've provided a few files:
% make catqYou will not need to edit this file for this assignment.
/cse/courses/cse326/98sp/project1You should make a copy of this directory into your home directory
% cp -r /cse/courses/cse326/98sp/project1 ~/project1and edit your copy.
What to hand in
You are required to hand in your project1 directory with your implementation of the Queue data structure. Included in this directory should be these two files:
How to hand in
We are using the turnin program to hand in project assignments. When you are ready to submit your solution, you should have all of your files sitting in some directory. Suppose this directory is named ~/326stuff/myproj1dir. Suppose also, that you're in section AA. To submit your solution, type the following:
This will submit the contents of your directory with a time stamp. You can submit more than once, but only the last submission is kept. Check the contents of your last submission by:% cd ~/326stuff/myproj1dir % make clean % cd ../ % turnin -c cse326=AA -p project1 myproj1dir
man turnin for more details and options.% turnin -v -c cse326=AA -p project1
Computing at home
It is fine to develop your program at home using another C++ compiler, but you must turn in the program using turnin and your program must compile and work correctly using g++ on the instructional machines. In this way we can compile and run your program on our test suite.
Look at our Stack implementation using templates in Stack.h. It illustrates the general structure for declaring a template class:
Everything inside the brackets is just like it would be in normal class specification/implementations, except the declarations use some unknown type T. The identifier T is used as a placeholder to represent the type of data that will be stored on the stack.template <class T> class Stack { ... // Stack specification and implementation with respect to type T ... };
Instantiating template classes to create new objects is similar to normal classes, except we must specify the type for the template. For example, here is how you create a stack of integers capable of holding 100 objects:
Note that its use after instantiation is just like any other object.Stack<int> mystack(100); mystack.Push(8); mystack.Push(4); int result = mystack.Pop();
The only caveat of using C++ templates is that both the specification and implementation must be specified in an include file. This is because the compiler cannot compile code for a template class until it sees how it is being instantiated. If code needs to have both int stacks (Stack<int>) and (char *) stacks (Stack<char *>), the compiler will generate different code for each. The end result, however, is what we desired: the ability to specify both types of stacks with a single set of source code.
Note that the recent C++ standard allows for compilers to support template class implementation in a separate source module. However, most compilers including our version of g++ on the instructional unix machines don't support this.