22
C Programming - Lecture 7 • In this lecture we learn about: – Linked Lists – Binary Trees – How to test your code – What happens if you don’t

C Programming - Lecture 7

  • Upload
    nerina

  • View
    27

  • Download
    1

Embed Size (px)

DESCRIPTION

C Programming - Lecture 7. In this lecture we learn about: Linked Lists Binary Trees How to test your code What happens if you don’t. Structs which contain themselves. Sometimes programmers want structs in C to contain themselves. - PowerPoint PPT Presentation

Citation preview

Page 1: C Programming - Lecture 7

C Programming - Lecture 7

• In this lecture we learn about:– Linked Lists– Binary Trees– How to test your code– What happens if you don’t

Page 2: C Programming - Lecture 7

Structs which contain themselves• Sometimes programmers want structs in C

to contain themselves.

• For example, we might design an electronic dictionary which has a struct for each word and we might want to refer to synonyms which are also word structures.

word1:runsynonym1synonym2

word2: sprintsynonym1synonym2

word3: jogsynonym1synonym2

Page 3: C Programming - Lecture 7

How structs can contain themselves

• Clearly a struct cannot literally contain itself.

• But it can contain a pointer to the same type of struct

struct silly_struct { /* This doesn't work */ struct silly_struct s1;};

struct good_struct { /* This does work */ struct *good_struct s2;};

Page 4: C Programming - Lecture 7

The linked list - a common use of structs which contain themselves

• Imagine we are reading lines from a file but don't know how many lines will be read.

• We need a structure which can extend itself.head_of_list

information

nextptr

information

nextptr

information

nextptr= NULL

This is known as a linked list. By passing the value of thehead of the list to a function we can pass ALL the information.

Page 5: C Programming - Lecture 7

How to set up a linked listtypedef struct list_item { information in each item struct list_item *nextptr;} LIST_ITEM;

This structure (where information is what you want each"node" of your linked list to contain). It is important thatthe nextptr of the last bit of the list contains NULL so thatyou know when to stop.

NULLhead oflist

Page 6: C Programming - Lecture 7

Address book with linked lists

typedef struct list { char name[MAXLEN]; char address[MAXLEN]; char phone[MAXLEN]; struct list *next;} ADDRESS;

ADDRESS *hol= NULL; /* Set the head of the list */

Page 7: C Programming - Lecture 7

Linked list conceptsNULL

head oflist

Adding an item to the middle of the list

NULLhead oflist

new itempoints at next item

movethislink

Deleting an item from the middle of the list

NULLhead oflist

move this link

delete this node

Page 8: C Programming - Lecture 7

Adding to our address bookvoid add_to_list (void) /* Add a new name to our address book */{ ADDRESS *new_name; new_name= (ADDRESS *)malloc (sizeof (ADDRESS));

/* CHECK THE MEMORY! */ printf ("Name> "); fgets (new_name->name, MAXLEN, stdin); printf ("Address> "); fgets (new_name->address, MAXLEN, stdin); printf ("Tel> "); fgets (new_name->phone, MAXLEN, stdin); /* Chain the new item into the list */ new_name->next= hol; hol= new_name;}

Page 9: C Programming - Lecture 7

The Binary Tree• A binary tree is a method for storing

ordered data (for example a dictionary of words) where we wish to easily be able to add items and find items

• Each element in the tree can lead to two further elements – to the left and to the right, representing elements which are earlier in the alphabet and later in the alphabet respectively

Page 10: C Programming - Lecture 7

The binary tree

NULL NULL

NULL NULL NULL

NULLNULL

typedef struct tree { char word[100]; struct tree *left; struct tree *right;} TREE;

Page 11: C Programming - Lecture 7

Binary Tree Pros & Cons

• Finding an element is O(log n)

• Adding an element is O(log n) – O(1) if we already know where to add it.

• Deleting an element may be complex

• Programming complexity is higher than a linked list (just about)

Page 12: C Programming - Lecture 7

Deleting an entire binary tree

• I think this code is elegant and worth looking at: void delete_tree (TREE *ptr)

{ if (ptr == NULL) return; delete_tree(ptr->left); delete_tree(ptr->right); free (ptr);}

We can delete the whole tree with: delete_tree(root_of_tree);

Page 13: C Programming - Lecture 7

Testing your code really works• Just because a piece of code works once doesn't

mean it will work again.• Because we get a right answer for an input of 'n'

does not mean we will get the right answer for 'm'.• Working code should never "crash" it should

always exit with an error whatever its input.• You should know how your code will behave

when asked "the wrong question".

Page 14: C Programming - Lecture 7

Test boundary conditions• Consider what might happen if the input is very

large or very small.• If there is a possibility that your code will get such

input you should make sure it can deal with it.• Always beware of the divide by zero error.• In 1998 the guided-missile carrier USS Yorktown

was shut down for several hours when a crew-member mistakenly input zero to one of the computers. Don't let your code work like this.

Page 15: C Programming - Lecture 7

Boundary conditions example• What is wrong with this code which is

supposed to be like strlen

int my_strlen (char *string)/* What is wrong with this code to find the lengthof a string */{ int len= 1; while (string[len] != '\0') len++; return len;}

Page 16: C Programming - Lecture 7

Overflows of numbers• If you are going to work with large numbers

then be sure you know how large a number your variables can deal with.

• In most implementations of C an unsigned char can be from 0 to 255. How big an int can be varies from computer to computer.

• In July 1996 Ariane 5 exploded as a direct result of a programming error which tried to fit 64 bits of floating point into a 16 bit int.

Page 17: C Programming - Lecture 7

What if the user asks "the wrong question"

• This code finds the average of 'n' doubles - under what conditions does it fail. double avg (double a[], int n)/* a is an array of n doubles */{ int i; double sum= 0; for (i= 0; i < n; i++) { sum+= a[i]; } return sum/n;}

Page 18: C Programming - Lecture 7

Program defensively• In some cases (not all) you might add code

to weed out rogue values.

void class_of_degree (char degree[], double percent)/* Work out the approx. class of degree fromsomeone's percentage overall mark */{ if (percent < 0 || percent > 100) strcpy(degree,"Error in mark"); else if (percent >= 70) strcpy(degree,"First"); else if (percent >= 60) strcpy(degree,"Two-one"); . .}

These linesare just hereout of caution

Page 19: C Programming - Lecture 7

How to test your code while writing

• A good programmer doesn't sit down, write 10,000 lines of code and then run it.

• It will make your life easier if you test your program as you write it.

• Write the smallest possible part of the program you think will _do something_ and test it.

• Build the program up gradually - testing as you go.• I like to compile every dozen lines or so – as soon as

I've made a significant change. (I use a separate window to compile in).

Page 20: C Programming - Lecture 7

When and what to test• If your program takes no input but simply runs

and produces an answer then it may not need much testing. Most programs are not like this.

• If you are doing the cryptography project or Zipf's law projects, for example, your programs should be taking strings of input.

• What would happen if those strings of input were just rubbish instead of well behaved strings of words and letters.

Page 21: C Programming - Lecture 7

Document your testing• Documenting your testing is critical and it will

be important in your project.• If appropriate, you should include in your write

up, some evidence that you have tested your code with various inputs

• Failing to document testing can have important consequences

• One of the problems which beset the Pathfinder probe had actually been spotted in testing before launch - but forgotten about. It had to be solved while in flight.

Page 22: C Programming - Lecture 7