L4a_-_Stack_ADT_(1_on_1)

L4a_-_Stack_ADT_(1_on_1) - Lecture 4a Stack ADT Lecture...

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

View Full Document Right Arrow Icon
Lecture 4a Stack ADT
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full Document Right Arrow Icon
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 4
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
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full Document Right Arrow Icon
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 8
Stack ADT using Array
Background image of page 9

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

View Full Document Right Arrow Icon
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 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 54

L4a_-_Stack_ADT_(1_on_1) - Lecture 4a Stack ADT Lecture...

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

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