Homework Assignment 5

  1. Do not change any of the files that I have provided, other than as described in problem 3d for StackDriver.java. You must make your files work with my files.

  1. Write a generic queue in C using a "void *" implementation. The implementation should use an array to hold the queue elements. Use a "circular" implementation that wraps the end of the queue around to the front of the array once it reaches the end of the array. Your implementation will need to keep track of two indices--one that points to the front of the queue and one that points to the end of the queue. If you have the Scott text, you can look at Figure 8.4 on page 413 to get an idea of the implementation I want.

    The API that you will implement is:

    void *queue_new(int numItems);  // create a queue that can hold the indicated number of items
    void queue_enqueue(void *q, void *item); // add the element to the back of the queue
    void *queue_dequeue(void *q); // remove and return the element at the front of the queue
    int queue_isEmpty(void *q);   // 1 if empty and 0 otherwise
    
    I have included several files for this problem: If you have questions about what your output should look like, I have placed a C executable of queueDriver in /home/bvz/cs365/hw/hw5.

  2. The queue in question 1 requires that you write downcasts in order to extract values of a specific type, such as int, from the queue. This can be dangerous because the downcasts are not checked at run time to ensure that they are safe. Given an example of how this failure to check the downcasts could lead to a catastrophic result (i.e., a core dump or totally non-sensical output).

  3. In both Java and C++ write generic code (i.e., templates) to define a stack which is implemented as a vector. Use the vector class from the C++ STL library and the ArrayList class from the Java util library. The stack should support the following methods:

    1. Stack() constructor: A stack with a default maximum capacity of 100.
    2. Stack(int maxSize) constructor: The maximum capacity of the stack (in your C++ implementation you should only have a single constructor since you can use optional parameters--see section 8.4 of the Scott text or my notes if you're unsure how they work).
    3. bool push(T value): returns true if the push succeeds and false if the push would cause the stack to exceed its maximum capacity. In the false case, the value should not be pushed onto the stack.
    4. T pop()
    5. bool(ean) isEmpty()

    I have included the following sample programs/data in /home/bvz/cs365/hw/hw5 to help you test your program:

    Other information that may be useful:

    1. I have placed a C++ executable named StackDriver in the directory /home/bvz/cs365/hw/hw5 on the hydra machines. You can run this executable to see what the output from both your java and C++ implementations should look like.

    2. For the C++ version you must put the class declaration in a Stack.h file and the method definitions in a separate Stack.cpp file.

    3. My StackDriver.cpp program includes "Stack.h". You will need to include the Stack.cpp file at the end of your Stack.h file using the statement:
      #include "Stack.cpp"
      

    4. For the Java version place your Stack class in a package named stack. Note that StackDriver.java is not in the stack package and will need to import your Stack class from the stack package.

What To Submit

  1. queue.c: We will use a makefile to compile your queue.c into an executable with queueDriver.c.
  2. Stack.h/Stack.cpp: We will use a makefile to compile your template code into an executable with StackDriver.cpp.
  3. StackDriver.jar: StackDriver.jar should be executable and should contain Stack.java.
  4. A hw5.txt file for question 2.