Fundamentals of Python: From First Programs Through Data Structures PowerPoint PPT Presentation

presentation player overlay
1 / 6
About This Presentation
Transcript and Presenter's Notes

Title: Fundamentals of Python: From First Programs Through Data Structures


1
Fundamentals of PythonFrom First Programs
Through Data Structures
  • Chapter 14
  • Linear Collections Stacks

2
Overview of Stacks
  • Stack LIFO structure in which access is
    completely restricted to just one end, called the
    top
  • Basic operations push and pop

3
Using a Stack
  • A stack type is not built into Python
  • Can use a Python list to emulate a stack
  • Use append to push and pop to pop
  • Drawback stack can be manipulated by all of the
    other list operations as well
  • Extra operations violate the spirit of a stack as
    an ADT
  • We define a more restricted interface or set of
    operations for any authentic stack implementation
    and show how these operations are used in a brief
    example

4
The Stack Interface
5
So what would it take to write a Stack class
using the Python List?
6
Instantiating a Stack
  • We assume that any stack class that implements
    this interface will also have a constructor that
    allows its user to create a new stack instance
  • Later, well consider two different
    implementations
  • ArrayStack and LinkedStack
  • With different performance trade-offs
  • For now, assume that someone has coded these so
    we can use them
  • s1 ArrayStack()
  • s2 LinkedStack()
Write a Comment
User Comments (0)
About PowerShow.com