Step 5 If Step 3 is false return false and stop Step 6 If the end of the

Step 5 if step 3 is false return false and stop step

This preview shows page 11 - 18 out of 20 pages.

Step 5 : If Step 3 is false, return false and stop. Step 6 : If the end of the expression is reached, check if the stack is empty. Step 7 : If Step 6 is true, return true otherwise false. { kccecia, achung, lixin } COMP2011 (Spring 2019) p.11
Image of page 11
Example: Balanced Parentheses #include "char-stack/char-stack.h" /* File: balanced-paren.cpp */ const char L_PAREN = '(' ; const char R_PAREN = ')' ; const char L_BRACE = '{' ; const char R_BRACE = '}' ; const char L_BRACKET = '[' ; const char R_BRACKET = ']' ; bool balanced_paren( const char * expr); int main() // To check if a string has balanced parantheses { char expr[1024]; cout << "Input an expression containing parentheses: " ; cin >> expr; cout << boolalpha << balanced_paren(expr) << endl; return 0; } /* Compile: g++ -Lchar-stack -o balanced-parenstack balanced-paren.cpp -lcharst bool check_char_stack(char_stack& a, char c) { if (a.empty()) return false; if ( != c) return false; a.pop(); return true; } { kccecia, achung, lixin } COMP2011 (Spring 2019) p.12
Image of page 12
Example: Balanced Parentheses .. bool balanced_paren( const char * expr) { char_stack a; for ( const char * s = expr; *s != '\0' ; ++s) switch (*s) { case L_PAREN: case L_BRACE: case L_BRACKET: a.push(*s); break ; case R_PAREN: if (!check_char_stack(a, L_PAREN)) return false; break ; case R_BRACE: if (!check_char_stack(a, L_BRACE)) return false; break ; case R_BRACKET: if (!check_char_stack(a, L_BRACKET)) return false; break ; default : break ; } return a.empty(); } { kccecia, achung, lixin } COMP2011 (Spring 2019) p.13
Image of page 13
Part II Queue { kccecia, achung, lixin } COMP2011 (Spring 2019) p.14
Image of page 14
Queue: How it Works Consider the case when people line up for tickets. more people: new customers join the back of a queue , one at a time. fewer people: the customer at the front buys a ticket and leaves the queue . As an ADT , insertions and removals of items on a queue are based on a first-in first-out (FIFO) policy. It supports: Data : an ordered list of data/items. Operations (major ones): front : get the value of the front item enqueue : add a new item to the back dequeue : remove an item from the front { kccecia, achung, lixin } COMP2011 (Spring 2019) p.15
Image of page 15
Circular Queue of int Data — Illustration { kccecia, achung, lixin } COMP2011 (Spring 2019) p.16
Image of page 16
Circular Queue of int Data — queue.h #include <iostream> /* File: int-queue.h */ #include <cstdlib> using namespace std; const int BUFFER_SIZE = 5; class int_queue // Circular queue { private : int data[BUFFER_SIZE]; // Use an array to store data int num_items; // Number of items on the queue int
Image of page 17
Image of page 18

You've reached the end of your free preview.

Want to read all 20 pages?

  • Fall '14
  • Method, Subroutine, Binary numeral system

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes