Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains.

Similar presentations


Presentation on theme: "Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains."— Presentation transcript:

1 Class 4: Queues

2 cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains some data (‘soap’ or ‘garlic’ or 5) n Access is FIFO - first in, first out.

3 cis 335 Fall 2001 Barry Cohen First in, first out (FIFO) n This restriction on access is sometimes called FIFO - ‘first in, first out’ n Example: a movie line. n A computer example: a buffer of characters read from the keyboard.

4 cis 335 Fall 2001 Barry Cohen Queue operations n createQueue() n destroyQueue() n bool isEmpty() n enqueue(in newItem:NewItemType) Put an item onto the stack n dequeue() Remove the earliest item. No value returned n getFront(out queueFront:QueueItemType) Return earliest item, but don’t remove it.

5 cis 335 Fall 2001 Barry Cohen Palindrome test n A palindrome reads the same backward and forward. (‘Madam, I’m Adam.’) n Use stack and queue to test. n Put the string in each, and then compare.

6 cis 335 Fall 2001 Barry Cohen STL class queue n #include n It this a legal expression? ((a + (b / (c - d ) / 2) n bool empty() const; n size_type size() const; n T &front(); n T &back(); n void pop(); n void push(const T& x);

7 cis 335 Fall 2001 Barry Cohen Use the ‘counting test’ n Get an intuition: open paren: +1 close paren: -1 n Start with 0 n Value must always be nonnegative n Must end with 0

8 cis 335 Fall 2001 Barry Cohen Test expression using a stack n Start with empty stack n Open paren: push n Close paren: pop n End with empty stack

9 cis 335 Fall 2001 Barry Cohen Problems with stack solutions n Evaluating algebraic equations n Parsing a computer language (like C) n Parsing human language (at least some of it) n Executing computer programs (including recursive ones)

10 cis 335 Fall 2001 Barry Cohen Evaluate a postfix expression n An expression like you find on HP calculator (also called ‘reverse Polish notation’ - RPN) n Evaluating infix is complicated n Evaluating postfix is easy n Solution: infix -> postfix evaluate postfix

11 cis 335 Fall 2001 Barry Cohen Keep it simple n Assume correct expression n Only binary operators (+, -, *, /). (No unary ‘-’, for example.) n Only left to right order (no ‘**’, for example). n Each number is a single digit (0-9)

12 cis 335 Fall 2001 Barry Cohen Evaluate postfix n Evaluate postfix: 2 3 5 + * n Algorithm: while (there’s still input) * see a number * push it * see an op * pop two numbers from stack * apply op * push result


Download ppt "Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains."

Similar presentations


Ads by Google