Week 06 (A)
Week 06 (A) | 1/13 |
Nerds You Should Know #5 | 2/13 |
Who's the guy standing in the bus door?
... Nerds You Should Know #5 | 3/13 |
Alan Turing
|
|
Biography: "Alan Turing: The Enigma" by Andrew Hodges
Varieties of Linked List | 4/13 |
By varying insertion/deletion strategies
Stacks | 5/13 |
Insert and delete are called push and pop
push(S,V)
pop(S)
Stack s = emptyStack(); push(s,10); push(s,15); push(s,7); x = pop(s); push(s,12); y = pop(s);// x == 7 && y == 12
... Stacks | 6/13 |
Applications for stacks:
Exercise: Reverse Polish Notation | 7/13 |
Normal arithmetic uses infix notation, e.g. 1 + 2
Infix places operator between operands.
An alternative notation puts operator last, e.g. 1 2 +
This postfix style is called reverse polish notation (RPN).
Advantages: no parentheses needed to disambiguate.
... Exercise: Reverse Polish Notation | 8/13 |
Implement a reverse polish calculator.
Example:
1 2 +(1 + 2) = 3 3 2 *(3 * 2) = 6 4 3 + 6 *((4 + 3) * 6) = 42 1 2 3 4 + * +((3 + 4) * 2) + 1) = 15 1 2 + 5 * 7 - 4 /((((1 + 2) * 5) - 7) / 4) = 2
... Exercise: Reverse Polish Notation | 9/13 |
Main loop:
splitLine()
Exercise: Stack Data Type | 10/13 |
Implement a Stack
int
typedef struct stackNode { ... } StackNode; typedef struct stackRep { ... } StackRep; typedef StackRep *Stack; Stack emptyStack();// make an empty Stack int height(Stack s);// # values in Stack int isEmpty(Stack s);// is Stack empty? push(Stack s, int v);// add value on top int pop(Stack s);// remove top value
Queues | 11/13 |
Insert and delete are called enqueue and dequeue
enqueue(Q,V)
dequeue(Q)
Queue q = emptyQueue(); enqueue(q,10); enqueue(q,15); enqueue(q,7); x = dequeue(q); enqueue(q,12); y = dequeue(q);// x == 10 && y == 15
... Queues | 12/13 |
Applications for queues:
Exercise: Queue Data Type | 13/13 |
Implement a Queue
typedef struct queueNode { ... } QueueNode; typedef struct queueRep { ... } QueueRep; typedef QueueRep *Queue; Queue emptyQueue();// make an empty Queue int length(Queue q);// # values in Queue int isEmpty(Queue q);// is Queue empty? enqueue(Queue q, int v);// add value at tail int dequeue(Queue s);// remove head value
Produced: 24 Aug 2016