Stacks - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Stacks

Description:

The stack abstract data type is essentially a list using the LIFO ... parenthesis ... expression, until a left parenthesis is popped (but not output) A left parenthesis ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 9
Provided by: william732
Learn more at: http://www.cs.siue.edu
Category:

less

Transcript and Presenter's Notes

Title: Stacks


1
Chapter 6 Stacks
The stack abstract data type is essentially a
list using the LIFO (last-in-first-out) policy
for adding and removing elements.
The principal stack operations
  • Create an empty stack.
  • Copy an existing stack.
  • Destroy a stack.
  • Determine whether a stack is empty.
  • Add a new element to a stack.
  • Remove the most recently added element from a
    stack.
  • Retrieve the most recently added element from a
    stack.

2
Example Stack Application 1 The Run-Time Stack
u 20
u 10
v 30
v 20
temp 30
temp 20
When running a program that uses functions, a
stack is used to keep track of the function
calls, including the status of the variables in
each function.
a 10
b 20
c 20
c 30
x 20
x 10
y 30
y 20
z 10
z 20
z 30
3
Example Stack Application 2 Converting Infix To
Postfix
Following these rules, then, the infix expression
7 6 - 3 ( 5 8 / 2 ) is converted into the
postfix expression 7 6 3 5 8 2 / -
4
Example Stack Application 3 Evaluating A
Postfix Expression
Following these rules, then, with the postfix
expression 7 6 3 5 8 2 / - yields
7
13
-14
5
Example Stack Application 4 Graphical
Transformations
translate
When graphically manipulating 2D and 3D objects,
its often convenient to use a stack to
manipulate them at the origin and then translate
them to their appropriate locations.
scale
rotate
By carefully applying the transformations in the
correct order (via the stack), the image is
altered in the desired fashion.
translate
6
Stack Implementation Alternatives
  • An Array Implementation
  • Positives
  • Avoids pointers (uses top index)
  • Trivial implementation
  • Negatives
  • Size must be declared in advance
  • A Linked List Implementation
  • Positives
  • Dynamically allocates exactly the right amount of
    memory
  • Straightforward (if not quite trivial)
    implementation
  • Negatives
  • Those wonderful pointers

7
Linked List Implementation of Stack
The stack class inherits from the LinkedList
class, so all LinkedList members are accessible
to any stack. This derived class has a
protected access specifier, indicating that the
public and protected members of LinkedList are
considered protected in the stack class. If the
access specifier were private, then the public
and protected members of LinkedList are
considered private in the derived class. If the
access specifier were public, then the public
and protected members of LinkedList are
considered public and protected (respectively) in
the derived class.
// Class declaration file stack.h // Linked List
implementation of the // stack ADT inherits
from LinkedList. ifndef STACK_H include
"linkedList.h" include ltiostreamgt using
namespace std class stack protected
LinkedList public // Class
constructors stack() stack(const
stack s) // Member functions bool
isEmpty() void push(const elementType
item) elementType pop()
elementType retrieve() define
STACK_H endif
Lets assume that the getNode and head members in
LinkedList were declared protected, not
private! Lets also assume that the elementType
typedef occurred in the LinkedList definition!
8
// Push function inserts item at // // the top
of the stack. // void stack push(const
elementType elt) nodePtr newHead
getNode(elt) assert(newHead ! NULL)
newHead-gtnext head head newHead
return // Pop function removes and returns
the // // top stack entry (if there is one).
// elementType stack pop() elementType
elt nodePtr oldHead assert(head !
NULL) oldHead head elt head-gtitem
head head-gtnext delete oldHead return
elt // On_top function returns (w/o removing)
// // the top stack entry (if there is one).
// elementType stack retrieve()
elementType elt assert(head ! NULL) elt
head-gtitem return elt
// Class implementation file stack.cpp // Linked
List implementation of the // stack ADT
inherits from LinkedList. include
"stack.h" include "linkedList.h" include
ltcassertgt include ltcstdlibgtusing namespace
std // Default constructor // //
Inherited from LinkedList. // stack stack()
LinkedList() // Copy constructor
// // Inherited from LinkedList. // stack
stack(const stack s) LinkedList(s) // Empty
function returns a boolean // // value that
indicates whether or // // not the stack is
empty. // bool stack isEmpty()
return head NULL
Write a Comment
User Comments (0)
About PowerShow.com