2-11 - Problem Solving with Computers-II CS 24 February 11,...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Problem Solving with Computers-II CS 24 February 11, 2010 1 Announcements Wednesdays Lab section Feedback Feedback from Quiz Pick up midterms No office hours on Feb 17 th Please speak to one of the TAs 2 Recap Exception handling File I/O 3 Lecture Plan Stack ADT Queue ADT 4 Stack ADT Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular implementation of a stack Implement the Stack ADT, in both an a rray-based implementation and a linked implementation Applications Matching parentheses Efficient way to save current state Call stack 5 6 Stacks of Coins and Bills 7 Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK 8 Stacks What do these composite objects all have in common? 9 Stacks Stack An abstract data type in which elements are added and removed from only one end (LIFO) Stacks What operations would be appropriate for a stack? 10 Stack Methods Transformers Push Pop Observers IsEmpty IsFull Top change state observe state 11 12 Stacks class StackType { public: StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop(); ItemType Top() const; Logical Level 13 Array-Based Implementation private: int top; ItemType items[MAX_ITEMS]; }; [0] [1] [2] . [ . ] stack .items .top Physical Level 14 Array-Based Implementation Give a series of operations that could produce this situation 15 Array-Based Implementation Before we code, we must consider error conditions Stack overflow The condition that results from trying to push an element on to a full stack Stack underflow The condition that results from trying to pop an empty stack 16 Array-Based Implementation StackType::StackType() { top = -1; } bool StackType::IsEmpty() const { return ( top = -1); } bool StackType::IsFull() const { return ( top == MAX_ITEMS); } What does const mean? 17 Array-Based Implementation void StackType::Push(ItemType newItem) { if (IsFull()) throw FullStack(); top++; items[top] = newItem; } What is FullStack() ? Code for Top/ Pop ? 18 Array-Based Implementation void StackType::Pop() { if (IsEmpty()) throw EmptyStack(); top- -; } ItemType StackType::Top() const { if (IsEmpty()) throw EmptyStack(); return (items[top]); } What is EmptyStack ? 19 Class Interface Diagram StackType class StackType Pop Push IsFull IsEmpty Private data: top [MAX_ITEMS-1] [ 2 ] [ 1 ] items [ 0 ] Top 20 char letter = V; StackType charStack; charStack.Push(letter); charStack.Push(C); charStack.Push(S); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(K); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop()} Tracing Client Code Private data: top [MAX_ITEMS-1] ....
View Full Document

Page1 / 64

2-11 - Problem Solving with Computers-II CS 24 February 11,...

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

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