Stack ADT Abstract Data Type N 3 2
Stack ADT (Abstract Data Type) N … 3 2 1 Professor John Carelli Kutztown University Computer Science Department
What is a Stack? • A stack is an ordered collection, or list, of items that can only be modified by adding and removing items at the “top” of the list • Items are processed on a last come/first serve basis • There is no access to the middle of the list (no “cutting in line”) • Real life examples: • A stack of dinner plates • The “undo” command in MS Word or a text editor • The program “stack” in a computer (subroutine calls) Professor John Carelli Kutztown University Computer Science Department
Stacks in the abstract • Sometimes referred to as a “LIFO” Top • Stands for “last-in/first-out” • A queue would be a “FIFO” Push Top • Some definitions: Pop • Push: add an item to the top • Pop: remove the item atthe top Professor John Carelli Kutztown University Computer Science Department
Abstract Data Type (ADT) for a Stack • Stack ADT requires that a program implementing a stack contain certain minimum core capabilities: • Elements must be maintained in an ordered list • Facilities must be provided to both add new items to the top of the list (push) and remove the last item entered in list (pop) • Individual elements in the stack can be any valid data type (or collection of data types) • Other possible capabilities: • Query the stack for contents, length, … • Clear or empty the stack • Limit or expand the size of the stack Professor John Carelli Kutztown University Computer Science Department
Stack Representations • Using an Array (top) N-1 • Using a Linked List top 1 … 2 … 4 … 3 2 N 1 0 Professor John Carelli null Kutztown University Computer Science Department
Array Implementation of Stack Use an array to store data elements • Advantages: • Easy to access • Natural ordering • No “wrap-around” worries 0 1 N-1 top • Size Limitation • Array is fixed size • Number of entries is limited unless special measures are taken Professor John Carelli 2 Kutztown University Example: Stack. Array Computer Science Department
Linked List Implementation of Stack Use a linked list to store data elements • Advantages: • Virtually unlimited number of elements 1 2 … • Potential issues • More complex coding • Need to manage pointers and data containers • On-the-fly memory allocation/deallocation • More memory required (data plus pointers) • Most likely slower than array implementation Professor John Carelli top Kutztown University N null Example: Stack. LL Computer Science Department
Final Considerations • What about more complex data (not just integers)? • The integers can be pointers to other data containers (classes, structs …) • Generalize by making a template • Which implementation is better? Answer is: “it depends…” • If a fixed size stack is acceptable, probably an array • For example: “undo” stack in word processing • If managing random, or unpredictable, data, a linked list might be better • Particularly if speed or memory is less of an issue • Lists of names, addresses … Professor John Carelli Kutztown University Computer Science Department
- Slides: 8