Week 06 (A)


Week 06 (A) 1/13

In This Lecture ...

Coming Up ...


Nerds You Should Know #52/13

Next in a series on famous computer scientists ...

Who's the guy standing in the bus door?


... Nerds You Should Know #53/13

Alan Turing

  
  • Founder of Computer Science
  • 1930's Maths/Physics at Cambridge
  • 1936 The Turing Machine
    (framework for computability theory)
  • 1940-45 Code breaker
    (cracked Enigma code at Bletchley Park)
  • 1946-50 Designed early computer
  • Papers on neural nets, programming, chess computers
  • 1950 Posed the "Turing Test" for AI
  • 1954 Suicide by poisoned apple

Biography:   "Alan Turing: The Enigma"   by   Andrew Hodges


Varieties of Linked List4/13

By varying insertion/deletion strategies

Examples:


Stacks5/13

Insert and delete are called push and pop

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

Example animation


... Stacks6/13

Applications for stacks:


Exercise: Reverse Polish Notation7/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 Notation8/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 Notation9/13

Main loop:

Evaluate as RPN, can be implemented via a Stack: Error conditions: insufficient numbers, ... ?


Exercise: Stack Data Type10/13

Implement a Stack (of ints) data type using linked lists:

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


Queues11/13

Insert and delete are called enqueue and dequeue

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

Example animation


... Queues12/13

Applications for queues:


Exercise: Queue Data Type13/13

Implement a Queue data type using linked lists:

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