Lecture10

Lecture10 - 5/3/10 CMPSC24:Lecture10...

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

View Full Document Right Arrow Icon
5/3/10 1 CMPSC 24: Lecture 10 Recursion Introduc;on & Abstract Data Type: Stacks Divyakant Agrawal Department of Computer Science UC Santa Barbara 5/3/10 1 Lecture Plan Binary Search on Sorted Lists Stack ADT 2 Stack ADT Describe a stack and its opera;ons at a logical level Demonstrate the eFect of stack opera;ons using a par;cular implementa;on of a stack Implement the Stack ADT, in both an a rray‐based implementa;on and a linked implementa;on Applica;ons Matching parentheses Efficient way to save current state Call stack 3
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/3/10 2 4 Stacks of Coins and Bills 5 Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK 6 Stacks What do these composite objects all have in common?
Background image of page 2
5/3/10 3 7 Stacks Stack An abstract data type in which elements are added and removed from only one end (LIFO) Stacks What opera5ons would be appropriate for a stack? 8 Stack Methods Transformers Push Pop Observers IsEmpty IsFull Top change state observe state 9
Background image of page 3

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

View Full DocumentRight Arrow Icon
5/3/10 4 10 Stacks class StackType { public: StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop(); ItemType Top() const; Logical Level 11 Array‐Based Implementa;on private: int top; ItemType items[MAX_ITEMS]; }; [0] [1] [2] …. [ . ] stack .items .top Physical Level 12 Array‐Based Implementa;on Give a series of opera5ons that could produce this situa5on
Background image of page 4
5/3/10 5 13 Array‐Based Implementa;on Before we code, we must consider error condiAons Stack overflow The condi;on that results from trying to push an element on to a full stack Stack underflow The condi;on that results from trying to pop an empty stack 14 Array‐Based Implementa;on StackType::StackType() { top = -1; } bool StackType::IsEmpty() const { return ( top = -1); } bool StackType::IsFull() const { return ( top == MAX_ITEMS); } What does const mean? 15 Array‐Based Implementa;on void StackType::Push(ItemType newItem) { if (IsFull()) throw FullStack(); top++; items[top] = newItem; } What is FullStack() ? Code for Top/ Pop ?
Background image of page 5

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

View Full DocumentRight Arrow Icon
5/3/10 6 16 Array‐Based Implementa;on void StackType::Pop() { if (IsEmpty()) throw EmptyStack(); top- -; } ItemType StackType::Top() const { if (IsEmpty()) throw EmptyStack(); return (items[top]); } What is EmptyStack ? 17 Class Interface Diagram StackType class StackType Pop Push IsFull IsEmpty Private data: top [MAX_ITEMS-1] [ 2 ] [ 1 ] items [ 0 ] Top 18 char leEer = ‘V’; StackType charStack; charStack.Push(leEer); charStack.Push(‘C’); charStack.Push(‘S’); if ( !charStack.Is±mpty( )) charStack.Pop( ); charStack.Push(‘K’);
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

Lecture10 - 5/3/10 CMPSC24:Lecture10...

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

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