CSC161 2011S Imperative Problem Solving

Reading: Stacks and Queues

Readings:

Please submit a question by 9 p.m. on Monday, 2 May 2011.


General

Why don't we have a version of stacks/queues that allows popping from the top or the bottom? It seems like that would be more generally useful so that the user doesn't have to implement both... Is that what a deque is? If so, why have all 3 types?

Yes. A Dequeue (doubly-ended queue) is a hybrid of a stack and a queue. You can add to the front. You can add to the back. You can remove from the front. You can remove from the back.

Dequeues are more complex than stacks or queues. And, the more operations you add, the more you constrain your implementation. (E.g., you can implement fixed-sized stacks with an array with almost no effort. You can implement dynamic-sized stacks with a cons-based list with almost no effort (push == car, pop == cdr). You can implement dynamic-sized queues with a list with a bit more effort. Getting dequeues right is much harder.)


Stacks

Does peek in Stacks just grab the top value without doing anything to the stack, such as removing? What use would this have, as I'm not sure what the point of having that certain value being put on the stack if it's just going to be returned.

You are correct that peek looks at the top of the stack without removing it. It's sometimes useful to make a decision based on what's there w/o removing it.

I'm confused about the pop operation... The wikipedia article says it removes the top element of a stack, but I thought it returned it—much like the stack top operation. The code provided isn't clear to me; it looks like it is returning a pointer to the next element of the stack, but what does it do to the value popped?

Pop both removes and returns the top element. ps->items is an array of integers. --ps->size decrements the size by 1. So, ps->items[--ps->size decrements the size by 1 and returns the item (integer) at that index.


Queues

I understood the reading, but let's go through the Wikipedia C implementation of queues in class.

Maybe. Maybe we'll just do a lab on them.


Tangential Questions

One probably tangential question: The reading mentioned dynamic arrays (arrays that could vary in size). Are we going to cover dynamic arrays in this course?

No. If you'd like to know how to do them, come talk to me.


Unanswered Questions

Could you explain (if it is worth knowing) the section on Hardware Support in the Stack article?

I do not understand the difference between Stacks and Queues. I can see the graphical difference but how can they be implemented differently except that you put from the top and remove from the bottom for queues?

I am lost as to why in enqueue if/else statement it sets both q->last->next and q->last to node.

the queues reading, the author made reference to circular buffers. What are circular buffers?

What other implementation of stacks and queues are use for besides the ones mention in the reading?

What problems are queues good at solving?

The reading says queues are often used as a buffer for information to be processed later... is there any reason to use a queue over an array? (especially in C, where I think arrays are more efficient. Or would you argue that an array that you only iterate through once is effectively a queue?)

How are queues different from our new version of lists?

Anyhow, why are there not that much protection in a stack in case of buffer overload and attack?

Next, can you explain how the push and pop calls of stacks in that diagram work a little more clearly, I felt lost in a few parts.

Can you give a little more detail on ""enqueue"" and ""dequeue""?

Can you show us more C variants of the C++ calls listed on the page?

What are the advantages to using a linked-list or an array for making a stack? do they have the same advantages that the two data-holding types have independently?

We've talked about stacks in class before. These are the same concept as the stacks in the reading, but on a different level, right? The stacks we talked about before were concerning functions and variable assignment whereas the stacks we're reading about now are created to store information while main is called, after all the variable assignment... right? I just want to clarify that these are two different types of stacks.

In the stacks reading, it had mentioned stack top. What does that procedure do? Push everything on top?

Reading the code we have in the reading, I understand why it may need pointers, but, is it necessary?

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon May 2 22:03:34 2011.
The source to the document was last modified on Mon May 2 22:03:11 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/stacks-queues.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu