8_Stacks.pdf - Stacks and Queues and a quick note on...

This preview shows page 1 - 12 out of 40 pages.

Stacks and Queues and a quick note on exceptions
Image of page 1

Subscribe to view the full document.

CS2134 Exception Handling • mechanism to handle exceptional (unusual behavior) –I/O problem –subscript out of range –violation of a precondition • Allow clean design of reliable code (avoids cluttering the “usual” code with lots of special cases • in C++ exceptions thrown in a “try” block are caught in a “catch” statement or propagated to block in which this is nested, or propagated to the caller
Image of page 2
CS2134 Exception Handling • Try blocks enclose a throw expression/call to a function that throws an exception inside a try block • Catch blocks follows a try block or call to a function that has a try block • Throw expressions flag an unusual situation is of type void can pass information “back”
Image of page 3

Subscribe to view the full document.

#include <iostream> using namespace std; int main () { try { throw 20; } catch ( int e) { cout << "An exception occurred. Exception Nr. " << e << endl; } return 0; } try { // code here } catch ( int param) { cout << "int exception" ; } catch ( char param) { cout << "char exception" ; } catch (...) { cout << "default exception" ; }
Image of page 4
void g() { throw std::exception(); } void f() { std::string str = "Hello"; // This string is newly allocated g(); } int main() { try { f(); } catch(...) { } }
Image of page 5

Subscribe to view the full document.

CS2134 try { ... v.pop_back(); ... // assuming pop_back was OK } catch (const UnderFlowException & e) { cout << e.what() << endl; .... }
Image of page 6
CS2134 Abstract Data Types –Abstract Data Types • Abstract description of the operations provided and the relationships among them Different implementations are possible for the same ADT • Separation of concerns between data type implementation and use • Were designed around common algorithmic constructs, rather than physical design –Classes in Object Oriented languages group data (member variables) with operations to manipulate the data (member functions) –OO languages developed to support ADTs
Image of page 7

Subscribe to view the full document.

CS2134 ADT’s stack, queue, priority queue 8 top() push(x) pop() queue front() enqueue/ push(x) dequeue/ pop() back() Priority queue insert(x)/ push(x) top() delete/ pop() VIP service FIFO LIFO stack
Image of page 8
Function Calls How would we implement these? Joe Bob Ann customersInLine clerk 1 clerk 2 30 100 25 40 Printer Jobs The ADT stack will be used when we design a simple calculator, do a simple balanced paren checking. The ADT’s queue, priority queue, and stack will be used as a subroutine in a graph algorithm fun 1 fun 2 fun 3
Image of page 9

Subscribe to view the full document.

yahoo.com How would we implement these? p1 p2 google.com nyu.ed Sam’s Sue’s Sue’s printer queue Sue’s printer Back Button for Web Pages computer 2 computer 1 p3 video buffering stored video file video player
Image of page 10
STL Container Adapters
Image of page 11

Subscribe to view the full document.

Simpler - the underlying implementation is not apparent to the user - less details for the user to understand Simpler - the underlying implementation is not Flexible - the implementation can vary for different compilers - the user works through the interface and does not need to be aware of differences Safer - the user can be prevented from some errors “A container adaptor provides a different (typically restricted) interface to a container.
Image of page 12
You've reached the end of this preview.

{[ snackBarMessage ]}

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