


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of stacks and queues, simple sequence containers used in computer science. Stacks follow the last in first out (lifo) principle, while queues follow the first in first out (fifo) principle. The fundamental properties of these containers, their differences from other containers, and their applications in memory management and resource allocation. It also provides an example of using a stack to check for balanced parentheses in an expression.
Typology: Lab Reports
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Stacks and queues are very simple sequence containers in which items are only added and removed from the end. In a stack, all work is done on just one end, called the top. Hence, when an item is removed, it will be the item most recently added. As a result, a stack is called a LIFO structure, for “Last In First Out”. In a queue, items are added to the end, usually called the rear or back, and removed from the other end, usually called the front. Hence, when an item is removed it will be the item that has been in the queue longer than any other item currently in the queue. As a result, a queue is called a FIFO structure, for “First In First Out”. A fundamental property distinguishing stacks and queues from other containers is that items in the middle of the sequence may not be accessed or removed. One effect of this is than neither stacks nor queues have iterators. Stacks and queues are interesting because their simple functionality pro- vides an appropriate model for many common operations. Here are some examples:
A simple example will help illustrate further the use of a stack. The goal is to determine whether or not an expression has a balanced set of parentheses. Here the term parentheses is meant to include the characters ’(’, ’[’, ’{’, ’)’, ’]’, ’}’. We use a stack of chars. Each time an “open” parenthesis char — ’(’, ’[’, ’{’ — is read in the input, the char is pushed
onto the stack. Every time a “close” parenthesis — ’)’, ’]’, ’}’ — is read in the input, the top char of the stacked is checked:
This process of reading chars and doing the outlined push / comparison / pop operations continues until an error is found or until there is no more input associated with the expression. If the stack is not empty at the end of the input, a mismatch error has also occurred — there aren’t enough closing parentheses. To fully understand the foregoing, try a few examples yourself.
Stacks and queues are implemented in the standard library as templated containers. The include files are just called stack and queue, as in
#include
The definition of stack and queue objects is pretty much what you might guess, e.g.
std::stack
Figures 1 and 2 summarize the public interfaces to stack and queue objects. These summaries are taken from
http://www.sgi.com/tech/stl/stack.html
and
http://www.sgi.com/tech/stl/queue.html
Interestingly, these classes are implemented in terms of other standard li- brary containers rather than being implemented “from scratch”. We’ll ex- plore this issue in the checkpoints below.
Figure 2: Operations on a std::queue.
must empty the container(s) used, determine if the line is a palindrome while doing so, and output an appropriate message.