monday week 07
play

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


  1. 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 printList(list); For today's lecture ... 5/25 ... Dynamic Data Structures work through a series of programs dealing with linked lists, stacks and queues Creating a new list through iteration to ensure that you understand properly dynamic objects with links the right way memory management via malloc() and free() separating definitions of data structures and operations from their implementation NodeT *list, *head; head = list = makeNode(1); // both point to the first node int i; 2/25 Dynamic Data Structures for (i = 3; i < N; i += 2) { list->next = makeNode(i); // create and link new node Dynamic data structures are built at runtime. list = list->next; // move forward } Linked lists are an example. They are defined as: printList(head); typedef struct node { DataT data; // e.g. int data; 6/25 ... Dynamic Data Structures struct node *next } NodeT; 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 ... Dynamic Data Structures 3/25 undo sequence in a text editor checking for balanced braces Iterating over an existing linked list NodeT *list postfix calculator NodeT *p = list; They are examples of Last In, First Out (LIFO) while (p != NULL) { ... p->data ... // do something with the data p = p->next; // move forward ... Dynamic Data Structures 7/25 } Queues are a form of linked lists used to model: ... Dynamic Data Structures 4/25 the checkout at a supermarket people standing at a ticket window Creating a new list through iteration people queueing to go onto a bus cars queueing to go onto a ferry the wrong way objects flowing through a pipe (where they cannot overtake each other) messages, e.g. WhatsApp NodeT *list; web page requests arriving at a web server http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html Page 1 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html Page 2 of 9

  2. Monday Week 07 5/09/2014 6:33 pm Monday Week 07 5/09/2014 6:33 pm printing jobs arriving at a printer fprintf (stderr, "createQuack: no memory, aborting\n"); exit(1); } They are examples of First In, First Out (FIFO) head->data = HEADDATA; // should never be used head->next = NULL; return head; 8/25 Example: Quacks } Implement a data structure called Quack (QUeue and stACK): 11/25 ... A Linked List Implementation of a Quack define the data structure push(data,qs) makes another node in terms of C structures such as integers, arrays, structs and populates it with data implement the operations on the data structure the new node becomes the new top of quack qs (so the quack is a stack ) Define the operations without defining their implementation. void push(int data, Quack qs) { if (qs == NULL) { 9/25 ... Example: Quacks fprintf(stderr, "push: quack not initialised\n"); } else { Quack newnode = (Quack)malloc(sizeof(struct _node)); The operations are contained in a header file quack.h if (newnode == NULL) { fprintf(stderr, "push: no memory, aborting\n"); exit(1); typedef struct _node *Quack; } newnode->data = data; Quack createQuack(void); // create and return Quack newnode->next = qs->next; // old top void push(int, Quack); // put the given integer onto the quack qs->next = newnode; // new top int pop(Quack); // pop and return the top element on the quack } int isEmptyQuack(Quack); // return 1 if Quack is empty, else 0 return; 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 ... A Linked List Implementation of a Quack 12/25 we assume that the quack contains integers (could instead contain characters or structs) The type Quack is a pointer to a struct node pop(qs) checks if the quack qs is not empty but what struct node? if so, retrieves the data of the top node depends on how the quack is implemented and the node is freed which we cannot see from the header file int pop(Quack qs) { 10/25 A Linked List Implementation of a Quack int retval = 0; if (qs == NULL) { fprintf(stderr, "pop: quack not initialised\n"); createQuack() creates a 'head' linked-list node that points to the quack. } else if (isEmptyQuack(qs)) { fprintf(stderr, "pop: quack underflow\n"); } else { // quackLL.c: a linked-list-based implementation of a quack Quack topnode = qs->next; // top node must be there #include "quack.h" retval = topnode->data; #define HEADDATA -99999 // dummy data qs->next = topnode->next; // new top free(topnode); // free the old top struct _node { } int data; return retval; struct _node *next; } }; Quack createQuack(void) { ... A Linked List Implementation of a Quack 13/25 Quack head = (Quack)malloc(sizeof(struct _node)); if (head == NULL) { http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html Page 3 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html Page 4 of 9

  3. Monday Week 07 5/09/2014 6:33 pm Monday Week 07 5/09/2014 6:33 pm Effect of createQuack() followed by three push() calls: }; 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; } 16/25 Exercise: Re-implementing Quack Functions 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 14/25 Exercise: Implementing Quack Functions copies the element at top, decrements the value of top returns the stored top element 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 17/25 ... Exercise: Re-implementing Quack Functions 15/25 An Array Implementation of a Quack For the array implementation of quacks, re-implement the other functions We can implement quacks as an array instead of a linked list. int isEmptyQuack(Quack); // return 1 if Quack is empty, else 0 Both use exactly the same .h file. void makeEmptyQuack(Quack); // remove all the elements on Quack void showQuack(Quack); // print contents of Quack, from the top down Quack is now a pointer to a struct that contains an array and an integer: Which of these functions can be exactly the same in both quack implementations? // quackAR.c: an array-based implementation of a quack #include "quack.h" 18/25 Comparing the LL and AR Implementation #define HEIGHT 1000 struct _node { you can overflow the array-based quack, but not the linked-list based quack int array[HEIGHT]; int top; do you remember why? http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html Page 5 of 9 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week07a/notes.html Page 6 of 9

Recommend


More recommend