Stacks and Queues by David Guichard

Stacks and Queues by David Guichard - 3 Stacks and Queues...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3 Stacks and Queues Queues and stacks are two special list types of particular importance. They can be imple- mented using any list implementation, but arrays are a more practical solution for these structures than for general purpose lists. Øak × A stack is a list with the operations insert and remove restricted to one end of the list, called the top; insert and remove are usually called push and pop respectively. The natural analogy is to a stack of books: it is easy to add or retrieve a book from the top of a pile, but more difficult in the middle of the pile. Another good model is the spring loaded stack of dishes found in many dining halls and cafeterias; ‘push’ and ‘pop’ seem particularly appropriate in this context. The properties of a stack are often referred to as LIFO (Last In First Out), which is sometimes also used as a noun, that is, a stack is a lifo. Stacks are often useful in systems programming and organization. A simple example of a use for a stack is as a text buffer in a line editor. As a line of text is typed in, it is stored in a stack. When a mistake is noticed and the delete key is pressed the editor merely pops the stack, thus eliminating the most recently typed character. Stacks are usually implemented as arrays. In any given application there will typically be only a few stacks in use, so the problem of wasted space is not important. If stacks are expected to be highly variable in size, it may be appropriate to use a pointer based approach. 27 28 Chapter 3 Stacks and Queues For the array implementation we can borrow directly the naive array based imple- mentation of lists. In the record declaration the lastentry field would usually be called top , and the push and pop routines would of course not allow for a parameter indicating position in the stack. Typically the pop function will not only remove the top item from the stack but return the value of the item. Sometimes this is the only way to examine a value, and there is no separate retrieve function. On the other hand, in some mod- ified implementations, it may be possible to examine any item on the stack, but only to insert and remove at the top. In this case, the get_top function might be declared as get_top(int&,int) , and the call get_top(m,-2) would retrieve the value stored in the second item below the top of the stack. class int_stack { public: int_stack(); // initialize a stack with default capacity int_stack(int); // initialize a stack with capacity at least n // The next 4 functions return true on success, false on failure bool push(int n); // push n on the stack bool pop(int& n); // put the top value in n and pop the stack bool get_top(int& n); // put the top value in n bool clear(); // reinitialize the stack so it is empty bool empty(); // return true if the stack is empty, false otherwise bool full(); // return true if the stack is full, false otherwise int size(); // return number of items currently on the stack void dump(); // dump the stack to cerr...
View Full Document

This note was uploaded on 05/25/2010 for the course CPE CPE 360 taught by Professor Jenniferchen during the Spring '10 term at Stevens.

Page1 / 14

Stacks and Queues by David Guichard - 3 Stacks and Queues...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online