Chapter 7 Implementations of the ADT Stack CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides
An Array-Based Implementation Using an array to store a stack’s entries © 2017 Pearson Education, Hoboken, NJ. All rights reserved
An Array-Based Implementation The header file for an array-based stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
An Array-Based Implementation The header file for an array-based stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
An Array-Based Implementation The implementation file for an array-based stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
An Array-Based Implementation The implementation file for an array-based stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
An Array-Based Implementation The implementation file for an array-based stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
An Array-Based Implementation Protecting the ADT’s walls Implement stack as a class Declaring items and top as private Note push receives newEntry as constant reference argument push uses newEntry as an alias … no copy made © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation A link-based implementation of a stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation The header file for the class LinkedStack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation The header file for the class LinkedStack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation The implementation file for the class LinkedStack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation The implementation file for the class LinkedStack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation The implementation file for the class LinkedStack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation The implementation file for the class LinkedStack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation The implementation file for the class LinkedStack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
A Link-Based implementation The implementation file for the class LinkedStack © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Implementations That Use Exceptions Method peek does not expect client to look at top of an empty stack assert statement merely issues error message, and halts execution Consider having peek throw an exception Listings follow on next slides © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Implementations That Use Exceptions The header file for the class PrecondViolatedExcep © 2017 Pearson Education, Hoboken, NJ. All rights reserved
Implementations That Use Exceptions Implementation file for the class PrecondViolatedExcep © 2017 Pearson Education, Hoboken, NJ. All rights reserved