CS 215 – Spring 2017 Project 4 Due: April 23 checked into Canvas

CS 215 – Spring 2017
Project 4
Due: April 23 checked into Canvas by midnight
Learning Objectives:
- Implementation of a dynamic Stack library
- Implementation of a dynamic Queue library
- Use of multiple source files.
General Description:
A test program has been written to test the functionality of standard Stack and Queue functions. Finish
the project by completing the unwritten functions, and adding anything else necessary to make the
multiple source files and header files work.
Given: Proj4.zip contains the VS C++ source and project files for the part of the program already written.
Download this from the website to your computer. The source files contained are:
- main.cpp – the main program and test functions. This is complete and you shouldn’t change any
of the code. You will need to add #includes at the top to use the libraries you are writing:
#include “stack.h”
#include “queue.h”
- node.h – a header file that defines a node structure. It is complete.
- queue.h - a header file for the queue library. It is complete. It contains the declaration of the
queue structure and a list of queue library function prototypes.
Use this as an example to add stack.h to the project.
- queue.cpp – incomplete functions used to manipulate a queue data structure.
- stack.cpp – incomplete functions used to manipulate a stack data structure.
Specifications:
1. Add the stack.h header file to the project:
- Right click on Source Files on the Solution Explorer window (usually upper-right)
- Add / New Item
- Click the header file item and give it the name stack.h
Use the queue.h header file as an example. Declare the following stack structure, and list the
prototypes (signatures) for the four files in the stack.cpp file. Add any #includes needed.
struct stack {
Node * head;
Node * trail;
};
2. In main.cpp, include the local header files. Note the use of double quotes instead of arrows. This
indicates the header files are part of this project, not in the C++ library.
#include “stack.h”
#include “queue.h”
Note we don’t need to include node.h … it is included by these two header files already.
3. The program should now compile and run, though it will not do anything.
4. Complete the coding in the stack.cpp file. You can compile each .cpp separately using the
Build/compile option on the menu. Ctrl-F7 will also compile the current.cpp
5. Complete the coding in the queue.cpp file.
6. Use the Local Windows Debugger as usual to compile all source files and execute. Test the
program to ensure it works.
Detailed Specifications:
initQ() – initialize the Q to empty. The head and the tail should be NULL.
printQ() – print the word QUEUE followed by the keys of all nodes in the queue. In addition, (re) print
the key of the node pointed to by the tail pointer.
enQ() – given a Q, and a key for a new node: allocate a new node and populate the key member with the
argument passed. Insert the new node at the tail of the current Q. Be sure to update the tail pointer… it
should point to the newly-allocated node. Be sure the code works for “empty Q”, a Q of 1 node, and a Q
of 2 or more nodes.
deQ() – given a Q, remove the head node (save its key) and deallocate it. Return the key. When the Q is
empty, return INT_MAX (a constant defined in <iostream>). Be sure this works for an empty Q, a Q of
length 1, and a queue of length 2 or more.
printStack() – identical to printQ(), except there is no extra print of the tail (there is no tail pointer)
pop() – almost identical to deQ(). Remove and deallocate the top/head node, and return its key. Return
INT_MAX when the stack is empty.
push() – similar to enQ, except it places the new node as the now head (front) of the list. Again, be sure
to think about empty stack and stack of 1 or more nodes situations.