Download presentation
Presentation is loading. Please wait.
Published byMarvin Wilcox Modified over 8 years ago
1
CSC 172 DATA STRUCTURES
2
A TALE OF TWO STRUCTURES
3
STACK – LIFO QUEUE - FIFO
4
#define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double f) { if (sp 0) return val[--sp]; else { printf(“error: stack empty\n”); return 0.0; } }
5
#define MAXVAL 100 double val[MAXVAL]; void enqueue(double f) { } double dequeue (void) { }
6
#define MAXVAL 100 int head = 0 ; tail = 0; double val[MAXVAL]; void enqueue(double f) { if (((head + 1) % MAXVAL) != tail) { val[head] = f; head = (head + 1) % MAXVAL; } else printf(“error: queue full\n”); } double dequeue (void) { if (tail != head) { int temp = tail; tail = (tail + 1 ) % MAXVAL; return val[temp]; } else printf(“error: queue empty\n”); return 0.0 ; }
7
Stack & Queue with Linked List? Stack What does “push” look like? What does “pop” look like? Queue What kind of linked list? What does enqueue look like? What does dequeus look like?
8
A useful stack algorithm Postfix evaluation We can rewrite the infix expression 1+2 As the postfix expression 1 2 + “Think” like a computer “load value ‘1’ into accumulator “load value ‘2’ into register A Add value in register A to value in accumulator How about 1+2+3+4 ? How about 2*3+4? How about 2+3*4?
9
How to implement? Can you write method that evaluates postfix expressions? double postfixeval(Object[] items) Where objects in items[] are either Double Character
10
Postfix evaluation using a stack 1. Make an empty stack 2. Read tokens until EOF a. If operand push onto stack b. If operator i. Pop two stack values ii. Perform binary operation iii. Push result 3. At EOF, pop final result
11
Trace by hand 1 2 3 4 + + + 2 3 * 4 + 2 3 4 * +
12
Infix to postfix 1 + 2 * 3 == 7 (because multiplication has higher precedence) 10 – 4 – 3 == 3 (because subtraction proceeds left to right)
13
Infix to postfix 4 ^ 3 ^ 2 == 262144 != 4096 Generally, Rather than:
14
Precedence A few simple rules: ( ) > ^ > * / > + - Subtraction associates left-to-right Exponentiation associates right to left
15
Infix Evaluation 1 – 2 – 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 2 == -8 (1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) ) Could you write a program to evaluate stuff like this?
16
Postfix If we expressed (1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) ) As 1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - Then, we could use the postfix stack evaluator
17
Postfix evaluation using a stack 1. Make an empty stack 2. Read tokens until EOF a. If operand push onto stack b. If operator i. Pop two stack values ii. Perform binary operation iii. Push result 3. At EOF, pop final result
18
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -
19
1
20
2121
22
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 4
23
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 5 4
24
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 1024
25
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 3 1024
26
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 3072
27
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 6 3072
28
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 18432
29
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 7 18432
30
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 7 18432
31
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2 7 18432
32
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 4 7 18432
33
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 2041 18432
34
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - 7
35
1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / - -8
36
But how to go from infix to postfix? Could you write a program to do it? What data structures would you use Stack Queue How about a simple case just using “+” 1+ 2 + 7 + 4 1 2 7 4 + + + Operands send on to output? Operator push on stack? Pop ‘em all at the end?
37
More complex 2 ^ 5 – 1 == 2 5 ^ 1 – Modify the simple rule? If you are an operator, pop first, then push yourself? 1 + 2 + 7 + 4 1 2 + 7 + 4 + ok
38
Even more complex 3 * 2 ^ 5 - 1 3 2 5 ^ * 1 – If you are an operator: Pop if the top of the stack is higher precedence than
39
Infix to postfix Stack Algorithm Operands : Send to queue Close parenthesis: Pop stack & send to queue until you find an open parenthesis Operators: 1. Pop all stack symbols and send to queue until a symbol of lower precedence (or a right-associative symbol of equal precedence) appears. 2. Push operator EOF: pop all remaining stack symbols and send to queue
40
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7
41
1
42
- 1
43
- 1 2
44
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^- ^- 1 2
45
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^- ^- 1 2 3
46
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^^- ^^- 1 2 3
47
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 ^^- ^^- 1 2 3 3
48
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 - 1 2 3 3 ^ ^ -
49
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 (- (- 1 2 3 3 ^ ^ -
50
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 (- (- 1 2 3 3 ^ ^ - 4
51
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 +(- +(- 1 2 3 3 ^ ^ - 4
52
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 +(- +(- 1 2 3 3 ^ ^ - 4 5
53
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 *+(- *+(- 1 2 3 3 ^ ^ - 4 5
54
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7 *+(- *+(- 1 2 3 3 ^ ^ - 4 5 6
55
1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7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