Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pseudocode for Bracket matching Stack mystack; // declare & initialize stack Loop through all characters in program { if (symbol is an ‘opener’) // (,

Similar presentations


Presentation on theme: "Pseudocode for Bracket matching Stack mystack; // declare & initialize stack Loop through all characters in program { if (symbol is an ‘opener’) // (,"— Presentation transcript:

1 Pseudocode for Bracket matching Stack mystack; // declare & initialize stack Loop through all characters in program { if (symbol is an ‘opener’) // (, [, { mystack.push(symbol); if (symbol is a ‘closer’) { // }, ], } if (mystack.empty()) return no_match; else { // check if symbol matches top of stack char stacktop; mystack.top(stacktop); mystack.pop(); if (symbol and stacktop mismatch) return no_match; }

2 reverse.cpp #include “stack.h” int main() { Stack stack; stack.push(1); stack.push(2); stack.push(3); int num; while (!stack.empty()) { cout << stack.top() << endl; stack.pop(); }

3 reverse.cpp (with STL) #include int main() { Stack stack ; stack.push(1.6); stack.push(2.4); stack.push(3.9); float num; while (!stack.empty()) { cout << stack.top() << endl; stack.pop(); }

4 stack.h (with templates) template class Stack { public: Stack(); bool empty() const; bool push(Entry item); bool pop(); bool top(Entry &item) const; private: int num_items; Entry entries[maxstack]; }

5 part of stack.cpp (with templates) template bool Stack ::push(Entry item) { if (num_items >= maxstack) return false; else { entries[num_items++] = item; return true; }


Download ppt "Pseudocode for Bracket matching Stack mystack; // declare & initialize stack Loop through all characters in program { if (symbol is an ‘opener’) // (,"

Similar presentations


Ads by Google