Week 07


Nerds You Should Know #61/45

The next in a series on famous computer-related people ...
 

 
Both are called "The Father of X" ... who are they?
(only one of them is computer-related)


... Nerds You Should Know #62/45

John von Neumann

  • 1933-1953: Professor of Mathematics
    Princeton University
    Institute of Advanced Studies
  • developed idea of "stored program"
  • developed "memory/cpu" architecture
    (now called the "von Neumann architecture")
  • first computer called "MANIAC"
  • provided computational support
    for Manhattan Project (US atom bomb)
  • early champion of scientific computing
  • sceptical of "high-level programming languages"


Data and Memory3/45

For the next two lectures ...


Data4/45

Values of various types   (1-23.14'A''1'"abc")

The C language supports:

For program manipulation, values are stored in memory


... Data5/45

Data exists outside programs in files

A file is a sequence of bytes, accessed sequentially

The FILE * type represents a file stream in C

Every C program is connected to three streams:

The stdio.h library defines a range of file operations:


... Data6/45

Ways to input data into a program:


... Data7/45

Ways to input data into a program:


Memory8/45

Memory is a very large array of bytes

Access memory via C variables


... Memory9/45

Within a program, data objects have scope and lifetime

Scope: region of program in which an object is known

Lifetime: extent of execution during which an object exists


... Memory10/45

Local variables and parameters:

Global variables and string constants: Dynamic objects:


Pointers11/45

Pointer variables contain memory addresses

Pointers are typed   (know what kind of object they point to)

Using a pointer to access a simple memory object:

int  x, y, *xp;  xp = &x;  *xp = 5;  y = *xp+2;

Using a pointer to access a structured object:

typedef struct { int x; float y; char z; } Rec;

Rec r, *rp;  rp = &r;  (*rp).x = 5;  rp->z = 'm';


... Pointers12/45

For a function to be able to change the "actual" value of a variable, we need to pass a pointer as a parameter.


... Pointers13/45


Arrays14/45

Arrays contain a sequence of values of the same type.

Each array is defined as:

#define N NumOfElems

ElemType a[N];

Elements are accessed via a[0], a[1], ... a[N-1]

The name a is equivalent to &a[0]   (constant pointer)

Iteration over arrays:

// index-based
int i; 
for (i = 0; i < N; i ++) { ... a[i] ... }
// pointer-based
ElemType *p;
for (p = a; p < &a[N]; p++) { ... *p ... }


Exercise: Implementing string.h15/45

C strings are arrays of bytes, terminated by '\0'.

Implement the following operations from the string.h library:

typdef char *String;

int strlen(const String s);
String strcpy(String dest, const String src);

Use indexed-based iteration for strlen() and pointer-based iteration for strcpy().

The const indicates that we do not intend to change the input string.


Structs16/45

Structs contain a named collection of values of various types.

Structs are defined as:

struct StructTag {
	Type1 FieldName1;
	Type2 FieldName2;
	...
	Typen FieldNamen;
};

Often, we define structs via a typedef:

typedef struct StructTag {...} StructTypeName;


Dynamic Data Structures


... Dynamic Data Structures18/45

Dynamic data structures are built at runtime.

Linked lists are an example. They are defined as:

typedef struct node {
	DataT data;		// e.g. int data;
	struct node *next;
} NodeT;

[Diagram:Pic/linkedList2-small.png]


... Dynamic Data Structures19/45

Iterating over an existing linked list NodeT *list

NodeT *p = list;
while (p != NULL) {
	... p->data ...  // do something with the data    
	p = p->next;     // move forward
}


... Dynamic Data Structures20/45

Creating a new list through iteration


... Dynamic Data Structures21/45

Creating a new list through iteration


... Dynamic Data Structures22/45

Stacks are a form of linked lists used to model:

They are examples of Last In, First Out (LIFO)


... Dynamic Data Structures23/45

Queues are a form of linked lists used to model:

They are examples of First In, First Out (FIFO)


Abstract Data Types


Abstract Data Types25/45

Abstract data types (ADTs) are ...


Example: Quack ADT26/45

Implement a data structure called Quack (QUeue and stACK):

Define the operations (interface) without defining their implementation.


... Example: Quack ADT27/45

The operations are contained in a header file quack.h


typedef struct _node *Quack;

Quack createQuack(void);     // create and return Quack
void  push(int, Quack);      // put the given integer onto the quack
int   pop(Quack);            // pop and return the top element on the quack
int   isEmptyQuack(Quack);   // return 1 if Quack is empty, else 0
void  makeEmptyQuack(Quack); // remove all the elements on Quack
void  showQuack(Quack);      // print contents of Quack, from the top down


A Linked List Implementation of a Quack28/45


// quackLL.c: a linked-list-based implementation of a quack
#include "quack.h"
#define HEADDATA -99999   // dummy data

struct _node {
	int data;
	struct _node *next;
};

Quack createQuack(void) {
	Quack head = (Quack)malloc(sizeof(struct _node));
	assert(head != NULL);
	head->data = HEADDATA;   // should never be used
	head->next = NULL;
	return head;
}


... A Linked List Implementation of a Quack29/45


void push(int data, Quack qs) {
	if (qs == NULL) {
		fprintf(stderr, "push: quack not initialised\n");
	} else {
		Quack newnode = (Quack)malloc(sizeof(struct _node));
		assert(newnode != NULL);
		newnode->data = data;
		newnode->next = qs->next;   // old top
		qs->next = newnode;         // new top
	}
}


... A Linked List Implementation of a Quack30/45


int pop(Quack qs) {
	int retval = 0;
	if (qs == NULL) {
		fprintf(stderr, "pop: quack not initialised\n");
	} else if (isEmptyQuack(qs)) {
		fprintf(stderr, "pop: quack underflow\n");
	} else {
		Quack topnode = qs->next;   // top node must be there  
		retval = topnode->data;
		qs->next = topnode->next;   // new top
		free(topnode);              // free the old top
	}
	return retval;
}


... A Linked List Implementation of a Quack31/45

Effect of createQuack() followed by three push() calls:

[Diagram:Pic/quackLL-small.png]


Exercise: Implementing Quack Functions32/45

Implement the following functions for the Linked List implementation of quacks:

int   isEmptyQuack(Quack);   // return 1 if Quack is empty, else 0
void  makeEmptyQuack(Quack); // remove all the elements on Quack
void  showQuack(Quack);      // print contents of Quack, from the top down

Notes:


Implementing Quack Functions for Queues33/45

The quack implementations can be extended to queues


... Implementing Quack Functions for Queues34/45

New interface quack.h


typedef struct _node *Quack;

Quack createQuack(void);     // create and return Quack
void  push(int, Quack);      // put the given integer onto the quack
void  qush(int, Quack);      // put the given integer onto the quack
int   pop(Quack);            // pop and return the top element on the quack
int   isEmptyQuack(Quack);   // return 1 if Quack is empty, else 0
void  makeEmptyQuack(Quack); // remove all the elements on Quack
void  showQuack(Quack);      // print contents of Quack, from the top down


... Implementing Quack Functions for Queues35/45

qush(data,qs) in the Linked List Implementation


void qush(int data, Quack que) {
	if (que == NULL) {
		fprintf(stderr, "qush: quack not initialised\n");
	} else {
		Quack newnode = (Quack)malloc(sizeof(struct _node));
		assert(newnode != NULL);
		newnode->data = data;
		newnode->next = NULL;    // will become the new bottom
		Quack endnode = que;
		while (endnode->next != NULL) {   // go to end of list
			endnode = endnode->next;
		}
		endnode->next = newnode;         // new bottom
	}
}


Array Implementation of Quacks


An Array Implementation of a Quack37/45

We can implement quacks as an array instead of a linked list.

Both use exactly the same .h file.

Quack is now a pointer to a struct that contains an array and an integer:


// quackAR.c: an array-based implementation of a quack
#include "quack.h"
#define HEIGHT 1000

struct _node {
   int array[HEIGHT];
   int top;
};

Quack createQuack(void) {
	Quack qs = (Quack)malloc(sizeof(struct _node));
	assert(qs != NULL);
	qs->top = -1;   // note the array in struct _node does not get initialised
	return qs;
}


Exercise: Re-implementing Quack Functions38/45

Re-implement push(), qush() and pop() for the array implementation of quacks.

[Diagram:Pic/quackAR-small.png]


... Exercise: Re-implementing Quack Functions39/45

For the array implementation of quacks, re-implement the other functions

int   isEmptyQuack(Quack);   // return 1 if Quack is empty, else 0
void  makeEmptyQuack(Quack); // remove all the elements on Quack
void  showQuack(Quack);      // print contents of Quack, from the top down

Which of these functions can be exactly the same in both quack implementations?


Comparing the LL and AR Implementation40/45


Exercise: Testing the LL and AR Implementations of Quacks41/45


Exercise: Josephus' Ring42/45

1st century Jewish historian/philosopher/mathematician

[Diagram:Pic/josephus-small.png]

The legend:


... Exercise: Josephus' Ring43/45


Tips for Next Quiz44/45


... Tips for Next Quiz45/45

Sample practice question


Produced: 4 Sep 2016