The Stack ADT - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

The Stack ADT

Description:

A Stack is a type of collection where data is accessed in a FILO or LIFO manner. ... LIFO = Last In First Out. Some applications of a stack: Undo functionality ... – PowerPoint PPT presentation

Number of Views:138
Avg rating:3.0/5.0
Slides: 11
Provided by: anniegro
Category:
Tags: adt | filo | lifo | stack

less

Transcript and Presenter's Notes

Title: The Stack ADT


1
The Stack ADT
  • CS241
  • Western Washington University

2
What is a Stack?
A Stack is a type of collection where data is
accessed in a FILO or LIFO manner. FILO
First In Last Out LIFO Last In First Out Some
applications of a stack Undo functionality Parent
hesis balancing RPN calculations
3
Specifying Stack ADT Behavior
What operations are associated with a
stack? Create an empty stack Destroy a
stack Determine whether a stack is
empty Determine the number of items in a
stack Insert an item into the stack Retrieve an
item from the stack Remove an item from the
stack Display the contents of the stack These
operations specify an interface to a Stack ADT,
regardless of which implementation we will choose.
4
The Stack Interface
class Stack public // Create and Destroy
Stack() Stack() // Insert, Retrieve, and
Remove void push(const StackItemType item)
const StackItemType top() const const
StackItemType pop() // Size bool empty()
const int size() const // Print out
stack ostream operatorltlt(ostream os,
const Stack s)
5
Choosing an Implementation
We have quite a few choices for our Stack data
representation 1. A statically allocated
array typedef int StackItemType const int
MAX_STACK 100 class Stack private StackItem
Type _dataMAX_STACK int _size 2. A
dynamically allocated array typedef int
StackItemType class Stack private StackItemTy
pe _data int _size
6
Limitations???
Lets analyze these implementations and identify
their limitations. The static array
implementation requires us to set an upper bound
on the number of items we can hold in the
stack. The dynamic array implementation requires
us to know the number of items in the stack when
we are creating the list. How practical is the
Stack ADT with these limitations?
7
Other Options
When we studied the List ADT, we learned about a
linked representation. How can we apply that
representation to the stack? 1) Public
Inheritance - Is-A class Stack public List
private ???? 2) Aggregation -
Has-A class Stack private List _data
3) Private Inheritance - As-A class Stack
private List private ????
8
Pros and Cons
What are the advantages, if any, of these other
options? Same as the advantages of the linked
listthe data structure can grow as needed. No
longer need to try to guess how many items will
be in the stack. What are the disadvantages, if
any, of these other options? Not necessarily the
same as the linked list. We dont care about
efficient random access in a stack!
9
A Template Solution
Finally, we could use a template representation
template ltclass Tgt class Stack public //
Create and Destroy Stack() Stack() //
Insert, Retrieve, and Remove void push(const T
item) const T top() const const T pop()
// Size bool empty() const int size()
const // Print out stack template ltclass
Tgt ostream operatorltlt(ostream os, const
StackltTgt s)
10
Or the STL Template Stack
include ltstackgt template ltclass Tgt class
stack This stack class is part of the C
Standard Template Library.
Write a Comment
User Comments (0)
About PowerShow.com