Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 172 DATA STRUCTURES.

Similar presentations


Presentation on theme: "CSC 172 DATA STRUCTURES."— Presentation transcript:

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 < MAXVAL) val[sp++] = f; else printf(“error: stack full \n”); } double pop (void) { 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) {
#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?
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 ? 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
Make an empty stack Read tokens until EOF If operand push onto stack If operator Pop two stack values Perform binary operation Push result At EOF, pop final result

11 Trace by hand 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 == != 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 * ^ ^ / - Then, we could use the postfix stack evaluator

17 Postfix evaluation using a stack
Make an empty stack Read tokens until EOF If operand push onto stack If operator Pop two stack values Perform binary operation Push result At EOF, pop final result

18 1 2 – 4 5 ^ 3 * 6 * ^ ^ / -

19 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 1

20 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 2 1

21 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - -1

22 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 4 -1

23 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 5 4 -1

24 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 1024 -1

25 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 3 1024 -1

26 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 3072 -1

27 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 6 3072 -1

28 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 18432 -1

29 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 7 18432 -1

30 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 2 7 18432 -1

31 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 2 7 18432 -1

32 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 4 7 18432 -1

33 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 2041 18432 -1

34 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 7 -1

35 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - -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 “+” 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? ok

38 Even more complex 3 * 2 ^ 5 - 1 3 2 5 ^ * 1 – If you are an operator:
3 * 2 ^ 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: Pop all stack symbols and send to queue until a symbol of lower precedence (or a right-associative symbol of equal precedence) appears. Push operator EOF: pop all remaining stack symbols and send to queue

40 1 – 2 ^ 3 ^ 3 – ( * 6) * 7

41 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 1

42 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 - 1

43 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 - 1 2

44 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^ - 1 2

45 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^ - 1 2 3

46 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^ - 1 2 3

47 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^ -

48 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 - ^ ^ -

49 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ( - ^ ^ -

50 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ( - ^ ^ - 4

51 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 + ( - ^ ^ - 4

52 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 + ( - ^ ^ - 4 5

53 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 * + ( - ^ ^ - 4 5

54 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 * + ( - ^ ^

55 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 - ^ ^ * +

56 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 * - ^ ^ * +

57 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 * - ^ ^ * + 7

58 1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^ ^ * + 7 * -

59 ((1 – (2 ^ (3 ^ 3))) – (( 4 + (5 * 6)) * 7))
1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ((1 – (2 ^ (3 ^ 3))) – (( 4 + (5 * 6)) * 7)) To evaluation stack Input ^ ^ * + 7 * -


Download ppt "CSC 172 DATA STRUCTURES."

Similar presentations


Ads by Google