Professional Documents
Culture Documents
Review
Data Structures
Stacks
Queues
Application: Calculator
1
Review: Compound data types
1
Review: Compound data types
struct foo {
short s ;
union {
int i ;
char c ;
} u ;
unsigned i n t f l a g _ s : 1 ;
unsigned i n t f l a g _ u : 2 ;
unsigned i n t bar ;
};
2
Review: Compound data types
struct foo {
short s ; ← 2 bytes
union { ← 4 bytes,
int i ; 4 byte-aligned
char c ;
} u;
unsigned i n t f l a g _ s : 1 ; ← bit fields
unsigned i n t f l a g _ u : 2 ;
unsigned i n t bar ; ← 4 bytes,
} ; 4 byte-aligned
2
Review: Compound data types
3
Review: Compound data types
i n t i ;
char c ;
} u ;
unsigned i n t bar ;
short s ;
unsigned i n t f l a g _ s : 1 ;
unsigned i n t f l a g _ u : 2 ;
};
sizeof(struct foo) = 12
3
Review: Linked lists and trees
4
6.087 Lecture 7 – January 20, 2010
Review
Data Structures
Stacks
Queues
Application: Calculator
5
Pointer review
double ∗ p p i = & p i ;
p r i n t f ( "pi = %g\n" , ∗ p p i ) ;
5
Pointers to pointers
i n t ∗pn = &n ; / ∗ p o i n t e r t o n ∗ /
6
Pointer pointers example
7
Pointer pointers example
i n t ∗temp = ∗a ;
∗a = ∗b ;
∗b = temp ;
i n t temp = ∗a ;
∗a = ∗b ;
∗b = temp ;
7
Pointer arrays
8
Pointer array example
9
Pointer array example
Insertion sort:
/ ∗ move p r e v i o u s elements down u n t i l
i n s e r t i o n p o i n t reached ∗ /
void s h i f t _ e l e m e n t ( unsigned i n t i ) {
i n t ∗ pvalue ;
/ ∗ guard a g a i n s t going o u t s i d e a r r a y ∗ /
f o r ( pvalue = s o r t e d _ a r r a y [ i ] ; i &&
∗ s o r t e d _ a r r a y [ i −1] > ∗ pvalue ; i −−) {
/ ∗ move p o i n t e r down ∗ /
s o r t e d _ a r r a y [ i ] = s o r t e d _ a r r a y [ i −1];
}
s o r t e d _ a r r a y [ i ] = pvalue ; / ∗ i n s e r t p o i n t e r ∗ /
}
10
Pointer array example
11
String arrays
char s t r 2 [ ] = "goodbye" ; / ∗ l e n g t h = 8 ∗ /
char s t r 3 [ ] = "ciao" ; / ∗ l e n g t h = 5 ∗ /
char ∗ s t r A r r a y [ ] = { s t r 1 , s t r 2 , s t r 3 } ;
12
Multidimensional arrays
13
6.087 Lecture 7 – January 20, 2010
Review
Data Structures
Stacks
Queues
Application: Calculator
14
More data structures
14
The stack
15
Stack as array
16
Stack as array
s t a c k _ b u f f e r [ i t o p ++] = elem ;
i f ( i t o p > 0 )
r e t u r n s t a c k _ b u f f e r [−− i t o p ] ;
else
r e t u r n 0 ; / ∗ o r o t h e r s p e c i a l v a l u e ∗ /
17
Stack as linked list
i n t element ;
s t r u c t s _ l i s t n o d e ∗ pnext ;
};
18
Stack as linked list
19
Stack as linked list
r e t u r n 0 ; / ∗ o r o t h e r s p e c i a l v a l u e ∗ /
20
The queue
21
Queue as array
22
Queue as array
i c o u n t −−;
r e t u r n q u e ue _ b uf f e r [ i f r o n t + + ] ;
} else
return 0 . ; / ∗ or other s p e c i a l value ∗ /
}
23
Queue as array
a b c
�
front rear
�
a b c d
�
front rear
24
Queue as array
• Dequeue ’a’:
b c d
�
front rear
�
25
Queue as array
26
Queue as array
27
Queue as linked list
f l o a t element ;
s t r u c t s _ l i s t n o d e ∗ pnext ;
};
28
Queue as linked list
p r e a r = new_node ;
29
Queue as linked list
r e t u r n 0 . ; / ∗ o r o t h e r s p e c i a l v a l u e ∗ /
30
A simple calculator
Infix
Prefix
Postfix
A + B
+AB A B +
A * B - C
-*ABC A B * C
( A + B ) * ( C - D)
* + A B - C D A B + C D - *
31
Infix to postfix
32
Infix to postfix example
• Infix expression: A + B * C - D
Token
Output queue
Operator stack
A A
+ A +
B AB +
* AB +*
C ABC +*
- ABC*+ -
D ABC*+D -
(end) A B C * + D -
• Postfix expression: A B C * + D
• What if expression includes parentheses?
33
Example with parentheses
• Infix expression: ( A + B ) * ( C - D )
Token
Output queue
Operator stack
( (
A A (
+ A (+
B AB (+
) AB+
* AB+ *
( AB+ *(
C AB+C *(
- AB+C *(
D AB+CD *(
) AB+CD *
(end) A B + C D - *
• Postfix expression: A B + C D - *
34
Evaluating postfix
35
Postfix evaluation example
• Postfix expression: 3 4 + 5 1 - *
Token
Stack
3
3
4
34
+
7
5
75
1
751
- 74
*
28
(end)
answer = 28
• Extends to expressions with functions, unary operators
• Performs evaluation in one pass, unlike with prefix notation
36
Summary
Topics covered:
• Pointers to pointers
• pointer and string arrays
• multidimensional arrays
• Data structures
37
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.