Stacks and Queues in Computer Science: LIFO and FIFO Containers, Lab Reports of Data Structures and Algorithms

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

Pre 2010

Uploaded on 08/09/2009

koofers-user-6ru
koofers-user-6ru 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science II CSci 1200
Lab 10
Stacks and Queues
Introduction to Stacks and Queues
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:
The management of memory associated with function calls uses a
stack. Each function call causes the creation of a “frame” for the
new function, including space for parameters and local variables. This
frame is “pushed” onto the memory stack at the start of operations
for the function. When a function ends, this frame is “popped” off
the memory stack, and the previous function, whose memory is now
on the top of the stack, resumes its execution.
The allocation of computing resources, including the CPU, printers
and communication channels, is often based on the FIFO nature of
a queue. Certainly, these allocation methods are more sophisticated
than purely FIFO, but FIFO is the starting idea.
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
pf3
pf4

Partial preview of the text

Download Stacks and Queues in Computer Science: LIFO and FIFO Containers and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Computer Science II — CSci 1200

Lab 10

Stacks and Queues

Introduction to Stacks and Queues

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:

  • The management of memory associated with function calls uses a stack. Each function call causes the creation of a “frame” for the new function, including space for parameters and local variables. This frame is “pushed” onto the memory stack at the start of operations for the function. When a function ends, this frame is “popped” off the memory stack, and the previous function, whose memory is now on the top of the stack, resumes its execution.
  • The allocation of computing resources, including the CPU, printers and communication channels, is often based on the FIFO nature of a queue. Certainly, these allocation methods are more sophisticated than purely FIFO, but FIFO is the starting idea.

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:

  • If the stack is empty, the parentheses are unbalanced, so an error has occurred.
  • If the top char is the matching “open” parenthesis char — e.g. ’(’ on top of the stack when ’)’ is read, etc. — there is a correct match of parentheses. In this case, the top char is popped off the stack, and both it and the input char are discarded (no longer considered).
  • If the top char is not the matching “open” parenthesis — e.g. a ’(’ is on top of the stack when a ’}’ is read — then a error has been detected.

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 and the Standard Library

Stacks and queues are implemented in the standard library as templated containers. The include files are just called stack and queue, as in

#include #include

The definition of stack and queue objects is pretty much what you might guess, e.g.

std::stack s; std::queue q;

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.

  1. The downloaded file cs2stack.h contains a partial implementation of the stack in terms of a vector. Complete this implementation and write a short main program to test it. Be sure to test all member functions. You should keep the implementation entirely inside cs2stack.h and you are welcome to inline functions.