MIT6 087IAP10 Lec07
MIT6 087IAP10 Lec07
Review
More about Pointers
Pointers to Pointers
Pointer Arrays
Multidimensional Arrays
Data Structures
Stacks
Queues
Application: Calculator
Review: Compound data types
};
Assuming a 32-bit x86 processor, evaluate
sizeof(struct foo)
Review: Compound data types
struct foo {
union {
int i;
char c;
} u;
unsigned i n t bar ;
short s;
unsigned i n t flag _s : 1;
unsigned i n t flag _u : 2;
};
sizeof(struct foo) = 12
Review: Linked lists and trees
Review
More about Pointers
Pointers to Pointers
Pointer Arrays
Multidimensional Arrays
Data Structures
Stacks
Queues
Application: Calculator
Pointer review
double pi = 3.14159;
double * ppi = π
printf ( "pi = %g\n" , * ppi );
int n= 3;
int *pn =&n; / * pointer to n * /
int **ppn =&pn; / * pointer to address of n * /
}
How does it compare to the familiar version of swap?
void swap( int *a, int *b) {
int temp = *a;
*a= *b;
*b = temp ;
}
Pointer arrays
arr
Can declare a pointer array int * sorted_array[100]; containing
pointers to elements of arr and sort the pointers instead
of the numbers themselves
Good approach for sorting arrays whose elements are very
large (like strings)
Pointer array example
Insertion sort:
/ * move previous elements down until
insertion point reached * /
void shift _element ( unsigned int i) {
int * pvalue ;
/ * guard against going outside array * /
for ( pvalue = sorted_array [ i ] ; i &&
* sorted_array [ i -1] > * pvalue ; i --){
/ * move pointer down * /
sorted_array [ i ] = sorted_array [ i -1];
}
sorted_array [ i ] = pvalue ; / * insert pointer * /
}
Pointer array example
*
Note that strArray contains only pointers, not the characters
themselves!
Multidimensional arrays
Review
More about Pointers
Pointers to Pointers
Pointer Arrays
Multidimensional Arrays
Data Structures
Stacks
Queues
Application: Calculator
More data structures
*
};
struct s_listnode * stack_buffer = NULL; start empty
Top is now at front of linked list (no need to track)
Stack as linked list
} else
return 0.; / * or other special value * /
}
Queue as array
of capacity 4:
a
c
b
front rear
Enqueue d to the rear of the queue:
a cb d
front rear
The queue is now full.
Queue as array
Dequeue a :
cb d
front rear
Enqueue e to the rear: where should it go?
*
};
struct s_listnode *queue_buffer = NULL; start empty
Let front be at beginning no need to track front
Rear is at end we should track it:
Infix
A+B
A*B-C
( A + B ) * ( C -D)
Prefix
+AB
-*ABC
*+AB-CD
Postfix
AB+
AB*CAB+
CD-*
left-associative
and push new operator onto stack
Infix expression: A + B * C -D
Token
A
+
B
*
C
-
D
(end)
Output queue
A
A
AB
AB
ABC
ABC*+
ABC*+D
ABC*+D-
Operator stack
+
+
+*
+*
-
-
Postfix expression: A B C * + D
What if expression includes parentheses?
Example with parentheses
Infix expression: ( A + B ) * ( C -D )
Token
(
A
+
B
)
*
(
C
-
D
)
(end)
Output queue
A
A
AB
AB+
AB+
AB+
AB+C
AB+C
AB+CD
AB+CDAB+
CD-*
Operator stack
(
(
(+
(+
*
*(
*(
*(
*(*
Postfix expression: A B + C D -*
Evaluating postfix
Postfix expression: 3 4 + 5 1 -*
Token
3
3
4
34
+
7
5
75
1
751
-
*
28
(end)
answer = 28
Stack
74
Extends to expressions with functions, unary operators
Performs evaluation in one pass, unlike with prefix notation
Summary
Topics covered:
Pointers to pointers
pointer and string arrays
multidimensional arrays
Data structures