Download presentation

Presentation is loading. Please wait.

Published byBrody Duell Modified over 2 years ago

1
Sample PMT online… Browse http://www.cs.wmich.edu/gupta/teaching/cs 1120/sumII05/PMT/2004_1/ http://www.cs.wmich.edu/gupta/teaching/cs 1120/sumII05/PMT/2004_1/

2
Stacks and Queues

3
Checking for Balanced Braces Balanced: abc{dc{dc}pa}{csd+} Unbalanced: {asdf{df@#}}{dfa{df}

4
Pseudocode while (not end of line) { if (ch is ‘{‘) add ch to Stack else if (ch is ‘}’){ if (Stack is empty) return unbalanced; remove from Stack } if (Stack is empty) return balanced; return unbalanced;

5
Definition of Stack ADT level –A stack is an ordered group of homogeneous items, in which the removal and addition of stack items can take place only at the top of the stack A stack is a LIFO: last in, first out

6
Stack ADT 6 aStack.createStack(); while (not end of line) { if (ch is ‘{‘) aStack.push(ch); else if (ch is ‘}’){ if (!aStack.isEmpty()){ aStack.pop();} else{ aStack.destroyStack(); return unbalanced;} } //endelseif } //endwhile if (aStack.isEmpty()) return balanced; aStack.destroyStack(); return unbalanced; Using ADT stack for brace matching

7
A Stack -- Graphic

8
Array-Based Implementation class Stack { Public: Stack(); ~Stack(); bool isEmpty(); void push(Item item); void pop(); Item getTop(); Private: Item items[MAX]; int top; };

9
Array-Based Implementation Stack::Stack:top(-1){} Stack::~Stack(){} bool Stack::isEmpty(){ return top==-1; } void Stack::push(Item item){ if (top>=MAX-1) exit(0); items[++top] = item; } void Stack::pop(){ if (isEmpty()) exit(0); --top; } Itemp Stack::getTop(){ if (isEmpty()) exit(0); return items[top]; }

10
ADT List-Based Implementation class Stack { public: … private: List aList; };

11
ADT List-Based Implementation Stack::Stack{} Stack::~Stack(){} bool Stack::isEmpty(){ return aList.isEmpty(); } void Stack::push(Item item){ aList.insert(item, 1); }

12
ADT List-Based Implementation void Stack::pop(){ aList.remove(1); } Item Stack::getTop(){ return aList.retrieve(1); }

13
Compare Three Implementations Array v.s. LinkedList –Fixed size v.s. dynamic size LinkedList v.s. ADT List –Efficiency v.s. Simplicity

14
Application: Algebraic Expressions Infix expressions –Every binary operator appears between its operands: a+b*c, (a+b)*c Prefix expressions –Operator appears before its operands: +a*bc, *+abc Postfix expressions –Operator appears after its operands: abc*+, ab+c*

15
Evaluating Postfix Expressions

16
Pseudocode Stack aStack; for (each token t in the input){ if (t is an operand) aStack.push(t); else { operand2 = aStack.getTop(); aStack.pop(); operand1 = aStack.getTop(); aStack.pop(); result = operand1 t operand2; aStack.push(result); }

Similar presentations

OK

SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.

SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on cse related topics of psychology Ppt on atrial septal defect sound Ppt on isobars and isotopes of carbon Ppt on south america continent Ppt on history of olympics medals Ppt on pollution of air and water Ppt on water activity and microbial growth Ppt on automobile related topics Ppt on nokia company profile Ppt on diodes and transistors projects