L4a_-_Stack_ADT_(4_on_1)

L4a_-_Stack_ADT_(4_on_1) - Lecture Overview Stack Lecture...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 4a Stack ADT 2 --- 54 [CS1102C AY0809S2 Lecture 4a] Lecture Overview ± Stack ² Introduction ² Specification ² Implementations ± Array Based ± List ADT ± STL vector ² Applications ± Tower of Hanoi ± Bracket Matching ± Maze Exploration 3 --- 54 [CS1102C AY0809S2 Lecture 4a] Stack : A specialized list ± List ADT (Lecture 3) allows user to manipulate (insert/retrieve/remove) item at any position within the sequence of items ± There are cases where we only want to consider a few specific positions only ² E.g. only the first/last position ² Can be considered as special cases of list ± Stack is one such example: ² Only manipulation at the last position is allowed ± Queue (Lecture 4b) is another example 4 --- 54 [CS1102C AY0809S2 Lecture 4a] What is a stack ± Real life example: ² A stack of books, A stack of plates, Etc ± It is easier to add/remove item to/from the top of the stack ± The latest item added is the first item you can get out from the stack ² Known as L ast I n F irst O ut (LIFO) order ± Major Operations: ² Push : Place item on top of the stack ² Pop : Remove item from the top of the stack ± It is also common to provide: ² Top : Take a look at the topmost item without removing it
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 --- 54 [CS1102C AY0809S2 Lecture 4a] Stack : Illustration A stack of four books Bottom of stack (inaccessible) Top of stack (accessible) Push a new book on top Pop a book from top 6 --- 54 [CS1102C AY0809S2 Lecture 4a] Stack ADT: C++ Specification #include “StackException.h” template<typename T> class Stack { public: Stack(); bool isEmpty() const; int size() const; void push(const T& newItem) throw (StackException); void pop() throw (StackException); void pop(T& stackTop) throw (StackException); void getTop(T& stackTop) const throw (StackException); private: //Implementation dependant //See subsequent implementation slides }; Make use of template to make the stack ADT flexible 7 --- 54 [CS1102C AY0809S2 Lecture 4a] Stack ADT: C++ Specification ± StackException is a user defined exception class ² Similar to ListException class in Lecture 3 Slide 7 ² Constructor take in a string for error description ± E.g. StackException(“Something is wrong!”); ² Use what() method to retrieve the description ± E.g. stackExceptionObject.what( ) ; ± There are two versions of pop() method ² pop( ) : just remove the topmost item ² pop( T& stackTop) : retrieve and remove the topmost item 8 --- 54 [CS1102C AY0809S2 Lecture 4a] Stack ADT : Implementations ± There are many ways to implement Stack ADT, we will cover: ² Array-base implementation: ± Show the underlying details ± Very close to the array implementation of List ADT ² List ADT implementation: ± Stack is a special case of list, can be built on List ADT ± Make use of the linked list implementation of List ADT ² STL vector implementation: ± Make use of STL container vector ± Learn how to weight the pros and cons for each implementations
Background image of page 2
Stack ADT using Array 10 --- 54 [CS1102C AY0809S2 Lecture 4a] Stack ADT (Array) : C++ Specification const int MAX_STACK = 50; template<typename T> class Stack { public: //No change to methods from slide 6 private: T items[MAX_STACK]; int top; }; 0 1 MAX_STACK - 1 top
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

L4a_-_Stack_ADT_(4_on_1) - Lecture Overview Stack Lecture...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online