In class, we reviewed code for a singly-linked-list (which can implement a stack easily).
But queues are a bit more involved1.
In class I sketched a special header struct which keeps track of the front and end of
our singly-linked-list, as follows
(though in lecture I fumbled which field should be called "front" and which "rear"):
incremental steps
The following steps are suggested, good incremental steps to your hw.
(Don't try to write everything, and only then start to compile/run/test!)
And: be sure to draw pictures — if you don't have lots of scribbles
of memory diagrams, you're doing it wrong!
(The final exam will have questions that require understanding what pointers to update
in what order, w/o access to a textbook/internet.)
Starting:
Decide on the data types (records) you we'll use to represent a queue of (say) Integer,
and declare them.
Write a series of Ada statements to "hand construct" the queue shown above
(using as many helper-variables as desired, and setting each field "by hand").
Compile and run.
Also hand-make a queue with just 1 item, and another with 0 items.
Make them runnable code, since
they will be useful for testing/double-checking our later code with.
Warming up:
Write a procedure which prints out the contents of a queue.
Test this with your hand-constructed queue above.
enqueue:
Draw before & after memory-diagrams, for enqueuing one more item
to your hand-constructed queue.
Now write the code for enqueueing (on to longer queues);
run your code on the previous example and print the result to make sure it seems to work.
Enqueuing with 0 items is a special-case. Draw the before & after pictures.
Add to your code to handle the empty queue (inspired by the drawings), and run/print to make sure it seems to work.
Write code for freeing any garbage memory (if that is necessary), and make sure enqueue still seems to work.
dequeue:
Draw before & after memory-diagrams, for dequeueing one more item
to your hand-constructed queue.
Now write the code for dequeueing (on to longer queues);
run your code on the previous example and print the result to make sure it seems to work.
Dequeueing with one item is a special-case.2
Draw the before & after pictures.
Add to your code to handle the size-one queue (inspired by the drawings), and run/print to make sure it seems to work.
Write code for freeing any garbage memory (if that is necessary), and make sure dequeue still seems to work.
dequeue:
Draw before & after memory-diagrams, for enqueueing one more item
to your hand-constructed queue.
Write the code, and print the result to make sure it's what you expect.
Enqueueing into an empty-queue is a special-case. Draw the before & after pictures.
Now write the code for enqueueing, and compile/run/print-to-verify it.
Write code for freeing any garbage memory (if any is necessary),
and make sure enqueue still seems to work correctly.
Generic types: Add a type-parameter to your package,
to generalize it from queue-of-Integer.
This might entail moving your test-list-involving-integers to
outside the package, or (as in code for linked-list put,equal) pass in particular integers.