Monday Week 07 for (i = 1; i < N; i += 2) { list = makeNode(i); - - PowerPoint PPT Presentation

monday week 07
SMART_READER_LITE
LIVE PREVIEW

Monday Week 07 for (i = 1; i < N; i += 2) { list = makeNode(i); - - PowerPoint PPT Presentation

Monday Week 07 5/09/2014 6:33 pm Monday Week 07 5/09/2014 6:33 pm int i; Monday Week 07 for (i = 1; i < N; i += 2) { list = makeNode(i); // create new node list = list->next; // move forward 1/25 } Data and Memory


slide-1
SLIDE 1

5/09/2014 6:33 pm Monday Week 07 Page 1 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

Monday Week 07 Data and Memory

1/25

For today's lecture ... work through a series of programs dealing with linked lists, stacks and queues to ensure that you understand properly dynamic objects with links memory management via malloc() and free() separating definitions of data structures and operations from their implementation

Dynamic Data Structures

2/25

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;

... Dynamic Data Structures

3/25

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 Structures

4/25

Creating a new list through iteration the wrong way NodeT *list;

5/09/2014 6:33 pm Monday Week 07 Page 2 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

int i; for (i = 1; i < N; i += 2) { list = makeNode(i); // create new node list = list->next; // move forward } printList(list);

... Dynamic Data Structures

5/25

Creating a new list through iteration the right way NodeT *list, *head; head = list = makeNode(1); // both point to the first node int i; for (i = 3; i < N; i += 2) { list->next = makeNode(i); // create and link new node list = list->next; // move forward } printList(head);

... Dynamic Data Structures

6/25

Stacks are a form of linked lists used to model: moving boxes placing and removing many rings on a (single) finger putting plates away in the cupboard and then setting the table many cars going down a one-way street that is blocked and having to back out again calling functions in C page-visited history in a Web browser undo sequence in a text editor checking for balanced braces postfix calculator They are examples of Last In, First Out (LIFO)

... Dynamic Data Structures

7/25

Queues are a form of linked lists used to model: the checkout at a supermarket people standing at a ticket window people queueing to go onto a bus cars queueing to go onto a ferry

  • bjects flowing through a pipe (where they cannot overtake each other)

messages, e.g. WhatsApp web page requests arriving at a web server

slide-2
SLIDE 2

5/09/2014 6:33 pm Monday Week 07 Page 3 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

printing jobs arriving at a printer They are examples of First In, First Out (FIFO)

Example: Quacks

8/25

Implement a data structure called Quack (QUeue and stACK): define the data structure in terms of C structures such as integers, arrays, structs implement the operations on the data structure Define the operations without defining their implementation.

... Example: Quacks

9/25

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

push() and pop() are the core functions making a quack what it is we assume that the quack contains integers (could instead contain characters or structs) The type Quack is a pointer to a struct node but what struct node? depends on how the quack is implemented which we cannot see from the header file

A Linked List Implementation of a Quack

10/25

createQuack() creates a 'head' linked-list node that points to the quack.

// 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)); if (head == NULL) {

5/09/2014 6:33 pm Monday Week 07 Page 4 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

fprintf (stderr, "createQuack: no memory, aborting\n"); exit(1); } head->data = HEADDATA; // should never be used head->next = NULL; return head; }

... A Linked List Implementation of a Quack

11/25

push(data,qs) makes another node and populates it with data the new node becomes the new top of quack qs (so the quack is a stack)

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

... A Linked List Implementation of a Quack

12/25

pop(qs) checks if the quack qs is not empty if so, retrieves the data of the top node and the node is freed

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 Quack

13/25

slide-3
SLIDE 3

5/09/2014 6:33 pm Monday Week 07 Page 5 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

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

Exercise: Implementing Quack Functions

14/25

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: all functions should check whether incoming quack qs has been initialised if qs->next == NULL the quack has been initialisd but is empty

An Array Implementation of a Quack

15/25

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;

5/09/2014 6:33 pm Monday Week 07 Page 6 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

}; Quack createQuack(void) { Quack qs = (Quack)malloc(sizeof(struct _node)); if (qs == NULL) { fprintf (stderr, "createQuack: no memory, aborting\n"); exit(1); } qs->top = -1; // note the array in struct _node does not get initialised return qs; }

Exercise: Re-implementing Quack Functions

16/25

Re-implement push() and pop() for the array implementation of quacks. push() checks that there is a quack checks that the array is not full (this is called quack overflow)

(unlike the linked-list implementation, where there is no maximum number of elements)

increments the index top and places the new data at this position in the array pop() checks that there is a quack checks there is no underflow copies the element at top, decrements the value of top returns the stored top element

... Exercise: Re-implementing Quack Functions

17/25

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 Implementation

18/25

you can overflow the array-based quack, but not the linked-list based quack do you remember why?

slide-4
SLIDE 4

5/09/2014 6:33 pm Monday Week 07 Page 7 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

(may of course create an 'artificial' maximum number of nodes for the linked-list version)

a struct _node in a linked list implementation stores a quack element a struct _node in an array-based implementation stores the whole array the head node of the linked list version points to the top of the quack node

Exercise: Testing the LL and AR Implementations of Quacks

19/25

Write a tester to call functions in the interface quack.h Build the executable using gcc -Wall -Werror -O quackLL.c tester.c to test the LL implementation Build the executable using gcc -Wall -Werror -O quackAR.c tester.c to test the AR implementation

Implementing Quack Functions for Queues

20/25

The quack implementations can be extended to queues still dealing with quacks

  • nly difference between stacks and queues:

in a stack, new nodes added at the top in a queue, new nodes added at the bottom we only need one additional function, qush(), for queues

same prototype as push()

... Implementing Quack Functions for Queues

21/25

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 Queues

22/25

qush(data,qs) in the Linked List Implementation

5/09/2014 6:33 pm Monday Week 07 Page 8 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

makes another node and populates it with data the new node becomes the new bottom of quack qs (so the quack is a queue)

void qush(int data, Quack que) { if (que == NULL) { fprintf(stderr, "qush: quack not initialised\n"); } else { Quack newnode = (Quack)malloc(sizeof(struct _node)); if (newnode == NULL) { fprintf(stderr, "push: no memory, aborting\n"); exit(1); } 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 } return; }

... Implementing Quack Functions for Queues

23/25

qush(data,qs) in the Array Implementation checks that there is a quack checks that the array is not full (this is called quack overflow) increments the index top and moves all elements up places the new data at the bottom

Exercise: Josephus' Ring

24/25

1st century Jewish historian/philosopher/mathematician The legend: Flavius Josephus was in a group of 41 people surrounded by the Romans group decided to rather die than surrender Flavius suggested forming a ring and going around clockwise

slide-5
SLIDE 5

5/09/2014 6:33 pm Monday Week 07 Page 9 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html

killing every 3rd person until just one remains who would commit suicide He placed himself at position 31

... Exercise: Josephus' Ring

25/25

For example, if you have a ring of 12 people:

2 3 1 4 12 5 11 6 10 7 9 8

and you start counting at 1, people would be removed in the order:

3 6 9 12 4 8 1 7 2 11 5

Implement Josephus' Scheme using a quack as a queue

Produced: 5 Sep 2014