1 / 15

Stacks and Queues

Stacks and Queues. Eric Roberts CS 106B April 10, 2009. Here is my proposal: The weight of the final will be. 15% + 5% for each Honor Code case filed this quarter.

Télécharger la présentation

Stacks and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and Queues Eric Roberts CS 106B April 10, 2009

  2. Here is my proposal: The weight of the final will be 15% + 5% for each Honor Code case filed this quarter The weight assigned to the homework will be whatever is left after the announced weights are assigned to the various other components, subject to a minimum of 15%. Decision Time: Exam Weighting Proposal • For many years, I’ve been thinking about the possibility of reducing the weight of the final ifwe can do something collectively about the Honor Code problem. • Thus, if no Honor Code cases come up this quarter, the final will count for 15%, which is exactly the same as the midterm. The homework would count for 60%. • If we file three Honor Code cases (as we did last quarter), the final will count for 30% and the homework for 45%. And so on . . .

  3. ADTs as Software Tools • Over the relatively short history of software development, one of the clear trends is the increasing power of the tools available to you as a programmer. • One of the best explanations of the importance of tools is the book Software Tools by Brian Kernighan and P. J. Plauger. Even though it was published in 1976, its value and relevance have not diminished over time. • The primary theme of the book is that the best way to extend your reach in programming is to build on the tools of others.

  4. Documentation of the Libraries

  5. Documentation for the Stack Class

  6. The Stack Metaphor • A stack is a data structure in which the elements are accessible only in a last-in/first-out order. • The fundamental operations on a stack are push, which adds a new value to the top of the stack, and pop, which removes and returns the top value. • One of the most common metaphors for the stack concept is a spring-loaded storage tray for dishes. Adding a new dish to the stack pushes any previous dishes downward. Taking the top dish away allows the dishes to pop back up.

  7. int Fact(int n) { if (n == 0) { return 1; } else { return n * Fact(n - 1); } } int Fact(int n) { if (n == 0) { return 1; } else { return n * Fact(n - 1); } } int Fact(int n) { if (n == 0) { return 1; } else { return n * Fact(n - 1); } } int Fact(int n) { if (n == 0) { return 1; } else { return n * Fact(n - 1); } } n n n n 3 2 0 3 1 Fact Function Calls and Stack Frames Local variables and return addresses are stored in a stack. int main() { cout << "Enter n: "; int n = GetInteger(); cout << n << "! = " << Fact(n) << endl; return 0; } n 6 2 6 1 Enter n: 3 3! = 6 skip simulation

  8. stack.size() stack.push(value) Returns the number of values pushed onto the stack. Pushes a new value onto the stack. stack.isEmpty() stack.pop() Returns true if the stack is empty. Removes and returns the top value from the stack. stack.peek() stack.clear() Returns the top value from the stack without removing it. Removes all values from the stack. Methods in the Stack<x> Class

  9. OperatorMatching Exercise: Stack Processing Write a C++ program that checks whether the bracketing operators (parentheses, brackets, and curly braces) in a string are properly matched. As an example of proper matching, consider the string { s = 2 * (a[2] + 3); x = (1 + (2)); } If you go through the string carefully, you discover that all the bracketing operators are correctly nested, with each open parenthesis matched by a close parenthesis, each open bracket matched by a close bracket, and so on. Enter string: { s = 2 * (a[2] + 3); x = (1 + (2)); } Brackets are properly nested Enter string: (a[2] + b[3) Brackets are incorrect Enter string:

  10. The LaIR Queue

  11. queue.size() queue.enqueue(value) Returns the number of values in the queue. Adds a new value to the end of the queue (which is often called its tail). queue.isEmpty() Returns true if the queue is empty. queue.dequeue() Removes and returns the value at the front of the queue (which is called its head). queue.peek() queue.clear() Returns the value at the head of the queue without removing it. Removes all values from the queue. Methods in the Queue<x> Class

  12. QueueTestProgram Exercise: Queue Test Program Write a simple simulation of a queue application in which the user can either enter a name that is entered into a queue or the command serve, which dequeues and displays the first name. Extend the program to support quit and list commands. > Fred Fred has been added to the queue > Dave Dave has been added to the queue > list The queue contains: Fred Dave > serve Fred has been helped > list The queue contains: Dave > serve Dave has been helped

  13. map.size() map.put(key, value) or map[key] = value; Returns the number of key/value pairs in the map. Makes an association between key and value, discarding any existing one. map.isEmpty() Returns true if the map is empty. map.get(key) or map[key] map.containsKey(key) Returns the most recent value associated with key. Returns true if there is a value associated with key. map.remove(key) map.clear() Removes key from the map along with its associated value, if any. Removes all key/value pairs from the map. Methods in the Map<x> Class

  14. lexicon.size() lexicon.add(word) Returns the number of key/value pairs in the lexicon. Adds true to the lexicon, always in lowercase. lexicon.isEmpty() Returns true if the lexicon is empty. lexicon.addWordsFromFile(filename) lexicon.containsWord(word) Adds all the words in the specified file to the lexicon. Returns true if the lexicon contains the specified word. lexicon.containsPrefix(prefix) lexicon.clear() Returns true if the lexicon contains any word beginning with prefix. Removes all words from the lexicon. Methods in the Lexicon Class

  15. The End

More Related