34
1 Data Structures A Data Structure is an arrangement A Data Structure is an arrangement of of data data in memory. in memory. The purpose is to map real world The purpose is to map real world data organization to the logical data organization to the logical world in such a way that makes world in such a way that makes problems easy to understand and problems easy to understand and solve. solve. To make the link between program and To make the link between program and real world clear real world clear To make programs process data as To make programs process data as efficiently as possible. efficiently as possible.

1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory. The purpose is to map real

Embed Size (px)

Citation preview

Page 1: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

1

Data Structures A Data Structure is an arrangement A Data Structure is an arrangement of dataof data in in

memory.memory. The purpose is to map real world data The purpose is to map real world data

organization to the logical world in such a way organization to the logical world in such a way that makes problems easy to understand and that makes problems easy to understand and solve. solve.

To make the link between program and real To make the link between program and real world clearworld clear

To make programs process data as efficiently as To make programs process data as efficiently as possible.possible.

Page 2: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

2

Algorithms

The step by step sequence of instructions The step by step sequence of instructions that operate on data structures for a specific that operate on data structures for a specific purpose.purpose.

Some data structures are better than others Some data structures are better than others for data processing.for data processing.

Page 3: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

3

Silly example Suppose we want a program that manipulates Suppose we want a program that manipulates

peoples names.peoples names. We could use the basic char data type as our We could use the basic char data type as our

data structure.data structure.That is we could define 30 separate That is we could define 30 separate

variables, char chr1, chr2, chr3 … chr30;variables, char chr1, chr2, chr3 … chr30;Then assign each one a character in a name.Then assign each one a character in a name.This would be very clumsy and hard work This would be very clumsy and hard work

and inflexible.and inflexible. A better data structure would be to create a string.A better data structure would be to create a string.

that enables manipulation of a string as a whole that enables manipulation of a string as a whole andand as individual characters as individual characters

Page 4: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

4

String Data Structure Basic input and outputBasic input and output Basic string processingBasic string processing Character by character inputCharacter by character input Library functionsLibrary functions Using Strings with functionsUsing Strings with functions Common errorsCommon errors

Page 5: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

5

Fundamentals

String literal – a sequence of characters in double String literal – a sequence of characters in double quotes.quotes. ““This is a string”, “Hello world”, “mydata.dat”This is a string”, “Hello world”, “mydata.dat”

A string is an A string is an arrayarray of characters. of characters. What is an array?What is an array?

It is a data structure, a container, a way of It is a data structure, a container, a way of organising data.organising data.

It is a chunk of memory, a series of adjacent It is a chunk of memory, a series of adjacent bytes. bytes.

Page 6: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

6

String Arrays

A sequence of characters A sequence of characters plusplus one special one special character.character. The end of string character. The end of string character.

A named constant NULLA named constant NULLNULL takes the value of ‘\0’NULL takes the value of ‘\0’

E.g. the string “Good Morning!” has one E.g. the string “Good Morning!” has one extra character the NULL character extra character the NULL character which is not displayed.which is not displayed.

Page 7: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

7

“Good Morning!”

G o do M o r n i n g ! \O

Each box is one byte of memorythe string takes up one continuouschunk of memory

the sting takes up 14 bytes not 13

the end of string character acts as a sentinel (a

marker for many string processing problems)

Page 8: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

8

String Variables

Think of a name for your variableThink of a name for your variable e.g. FirstName, StreetName, Town, Country etc.e.g. FirstName, StreetName, Town, Country etc. Think about the maximum characters needed to Think about the maximum characters needed to

store. 5, 10, 80, 100, etc.store. 5, 10, 80, 100, etc. Then add one to that number for the NULL end of Then add one to that number for the NULL end of

string character.string character. Declare string variable as an array of characters. Declare string variable as an array of characters.

using special array notation.using special array notation.

Page 9: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

9

Example

char FirstName[20]; //able to store a string of char FirstName[20]; //able to store a string of 1919 chars chars

char LastName[20]; //able to store a string of char LastName[20]; //able to store a string of 1919 chars chars

char Sentence[80]; //able to store a string of 79 charschar Sentence[80]; //able to store a string of 79 chars

char word[10]; //able to store a string of char word[10]; //able to store a string of 99 chars chars

Unititialised string variables. Only

memory allocated. They do not contain

any useful information.

NO this is not an error! Take the string word for instance. It appears that 10 spaces have been allocated, enough for 10 characters? however strings of this sort require one extra space for the end of string character. Which means you only have 9 “usable” spaces.

Page 10: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

10

Example – declaration plus initialisationchar S[14] = “Chris”;char S[14] = “Chris”;initialising this way initialising this way automaticallyautomatically places a NULL character at the end of places a NULL character at the end of the string.the string.

Compare with unititialised versionCompare with unititialised version

char S[14];char S[14];

C h ir s \0

? ? ?? ? ? ? ? ? ? ? ? ? ?

s[0] s[1] s[3]s[2] s[4] s[5] s[6] s[7] s[8] s[9] s[10]

s[11]

s[12]

s[13]

note max subscript 1 less than length of string

Page 11: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

11

String Indexes A string has a certain length.A string has a certain length. That is, the number of characters not including NULL. That is, the number of characters not including NULL.

This is not the same as the size of the array.This is not the same as the size of the array. The name of the string is a reference to the address of the The name of the string is a reference to the address of the

first character.first character. We can access any character we wish by using the square We can access any character we wish by using the square

bracket notation.bracket notation. e.g. FirstName[0] allows us to access the first character e.g. FirstName[0] allows us to access the first character

in the string FirstName. FirstName[1] accesses the in the string FirstName. FirstName[1] accesses the second character, FirstName[2] the third and so on.second character, FirstName[2] the third and so on.

Problem this Problem this array indexarray index or or array subscriptarray subscript is is confusing.confusing.

One off problemsOne off problems

Page 12: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

12

Demo 1

Accessing elements of a stringAccessing elements of a string char message[14] = “Good Morning!”;char message[14] = “Good Morning!”;

Illustrate access of unititialised string.Illustrate access of unititialised string. Illustrate going past the end of string Illustrate going past the end of string

character but still in bounds of array.character but still in bounds of array. Illustrate going off the end of the arrayIllustrate going off the end of the array Illustrate difference between strlen and sizofIllustrate difference between strlen and sizof

Page 13: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

13

Basic input of strings

Using extraction operator (with cin or a file Using extraction operator (with cin or a file stream).stream). cin >> FirstName;cin >> FirstName; fin >> FirstName;fin >> FirstName;

ProblemProblem Extraction operator uses a space as Extraction operator uses a space as

terminatorterminator

Note no square brackets [ ]

Page 14: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

14

More string input tools

cin.get() or fin.get()cin.get() or fin.get() extracts a single character inlcuding extracts a single character inlcuding

white space!white space! cin.getline() or fin.getline()cin.getline() or fin.getline()

extracts a whole line of characters extracts a whole line of characters including white space.including white space.

Page 15: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

15

cin.get()cin.get() returns a character entered at the cin.get() returns a character entered at the

keyboard.keyboard.e.g. to enter a number of characters at the e.g. to enter a number of characters at the

keyboard including spaces and assign then to keyboard including spaces and assign then to individual string elements.individual string elements.const int MAXCHARS = 80;char line[MAXCHARS+1];int i=0;do {

line[i] = cin.get();i++;

}while (line[i-1] != ‘\n’ && i < MAXCHARS);line[i] = ‘\0’;

DO DEMO 2

Page 16: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

16

Using getline() basic use of getline needs three arguments. cin.getline(stringvar,lengthvar, stop_char)

stringvar is the name of a string variable. lengthvar is the name of an integer variable that

denotes the maximum number of characters to be read. A literal int can be used. extraction stops when stop_char encountered.

cin.getline(LastName,14,’\n’);

extracts up to 14 characters from the keyboard, stops extraction

when ‘\n’ encountered

Page 17: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

17

Demo 3

Creating string variablesCreating string variables Inputting with extraction operatorInputting with extraction operator

problems with spacesproblems with spaces Using getline()Using getline()

Page 18: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

18

#include <iostream.h>int main() {const int MAXCHARS 80;char message[MAXCHARS+1];

cout << “Please enter a message : “;cin.getline(message,MAXCHARS,’\n’);cout << “Your message is : “ << message << endl;return 0;

}

Page 19: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

19

Using = and == with strings

You cannot use these operators in the same You cannot use these operators in the same way as you would with basic data types.way as you would with basic data types.

E.g.E.g.

char Name[10];char Name[10];

Name = “Thomas”;Name = “Thomas”;

Assignment Illegal!

Only allowable in declaration

Page 20: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

20

Assignment of strings

Use strcpy() library functionUse strcpy() library function Needs Needs #include <string.h>

strcpy(destination, source);

e.g.e.g. strcpy(FirstName,”Chris”);

““Assigns” the string “Chris” to FirstNameAssigns” the string “Chris” to FirstName

Page 21: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

21

Checking for equality The statementThe statement

if (FirstName1 == FirstName2) {if (FirstName1 == FirstName2) {do something ..do something ..

}}is not illegal. It does is not illegal. It does NOTNOT test whether test whether FirstName1 is the same as FirstName2FirstName1 is the same as FirstName2

Use Use strcmp() library function which library function which again needs again needs # include <string.h>

Page 22: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

22

Comparisons of Strings

strcmp(string1, string2) returns an integer, strcmp(string1, string2) returns an integer, > 0 if string1 is “bigger” than string2> 0 if string1 is “bigger” than string2 < 0 if string1 is “smaller” than string2< 0 if string1 is “smaller” than string2 0 (False) if string1 is the same as string20 (False) if string1 is the same as string2

It compares each string a character at a time.It compares each string a character at a time.

typical usetypical useif (strcmp(FirstName, “Chris”) == 0)

cout << “My Name” << endl;

True

Page 23: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

23

Finding the length of a string

A very important task in string processing is to A very important task in string processing is to find the number of characters in a string.find the number of characters in a string.

To set bounds of loops to process the string.To set bounds of loops to process the string. Remember the declaration gives the maximum Remember the declaration gives the maximum

number of characters.number of characters. The NULL character \0 marks the end of a The NULL character \0 marks the end of a

string.string. We want the count up to the NULL character.We want the count up to the NULL character.

Page 24: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

24

Library function strlen()Needs Needs #inlcude <string.h>This function returns the length of a stringThis function returns the length of a string

char FirstName[40];int size;strcpy(FirstName, “Christopher”);size = strlen(FirstName);for (int i=0;i<size; i++) FirstName[i] = “*”;

cout << FirstName << endl;

Page 25: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

25

String Processing Loops, Loops and LoopsLoops, Loops and Loops

for loops, while loops, do while loopsfor loops, while loops, do while loops E.g. To fill a string my_string, with ‘*’ charactersE.g. To fill a string my_string, with ‘*’ characters

while loop version

int i = 0;

while (my_string[i] != ‘\0’) {

my_string[i] = ‘*’;

i++;

}

for loop version

for (i=0; my_string[i] != ‘\0’; i++){

my_string[i] = ‘*’;

i++;

}

NOTE THIS IS DANGEROUS since no check is made on whether there is a NULL character

Page 26: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

26

Safer Versionwhile loop version

int i = 0;

while (my_string[i] != ‘\0’ && i < MAXCHARS) {

my_string[i] = ‘*’;

i++;

}

I leave it as an exercise to adapt the for loop version to a safe version.

Page 27: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

27

CAUTION

When manipulating strings make sure you When manipulating strings make sure you do not lose or replace the NULL character.do not lose or replace the NULL character.

Make sure that the NULL character is in the Make sure that the NULL character is in the correct place. That is, one position after the correct place. That is, one position after the last character in string.last character in string.

Page 28: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

28

Strings as function argumentsPassing a string to a function.Passing a string to a function.

e.g. size = strlen(mystring);e.g. size = strlen(mystring);

Notice no square brackets [ ] are used in the Notice no square brackets [ ] are used in the call.call.

What is passed?What is passed?

Page 29: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

29

A bit of technical stuff. Take a deep breath! The string variable name without square brackets The string variable name without square brackets

contains the address in memory of the first contains the address in memory of the first position of the string.position of the string.

All that is passed is an address!All that is passed is an address! a variable that can store addresses is called a a variable that can store addresses is called a

pointer.pointer. When an array is passed to a function all that is When an array is passed to a function all that is

passed is the address of the first element of the passed is the address of the first element of the array.array.

Page 30: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

30

How does the function know the size of the string?How does the function know the size of the string? The standard functions use the NULL character as The standard functions use the NULL character as

a a sentinel.sentinel. They have a precondition that the strings passed They have a precondition that the strings passed

are properly formedare properly formed A dangerous assumption.A dangerous assumption.

It would be safer if we provided information about It would be safer if we provided information about the sizes of strings being manipulated.the sizes of strings being manipulated.

E.g. What happens if we attempt to copy a string E.g. What happens if we attempt to copy a string with 50 characters into a string that can only take with 50 characters into a string that can only take 5 characters max?5 characters max? memory is overwritten!memory is overwritten!

Page 31: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

31

Creating your own functions that operate on strings: e.g. A safe string copy function

PrototypePrototypevoid my_string_copy(char target[], char source[], int target_size);

Notice syntax of prototype. The size of target and source is not provided. Since all that is passed is the address of the first element. target and source receive addresses. This is similar to pass by reference.

Note also choice of generic names for the parameters, that do not conflict with identifiers else where in your program

Page 32: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

32

Function DefinitionFunction Definition

void my_string_copy(char target[], char source[], int target_size) {

int new_length; // temporarily hold length of source string

new_length = strlen(source);

// can target hold a string this long?

if (new_length > (target_size-1)) // allow for NULL

new_length = target_size; // fit in all we can

// copy character by character

for (int i = 0; i < new_length; i++)

target[i] = source[i];

target[i] = ‘\0’; //add the NULL to the end of target

}

Page 33: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

33

Putting it all together#include <iostream.h>#include <string.h>//prototypevoid my_string_copy(char target[], char source[], int target_size);

int main() {char shortSTR[5]; //holds up to 4 charschar longSTR[] = “This is a long string”;

my_string_copy(shortSTR,”Hello”,5);cout << shortSTR << “STRING ENDS HERE. \n”;

my_string_copy(shortSTR,longSTR,5);cout << shortSTR << “STRING ENDS HERE. \n”;

return 0;}

void my_string_copy(char target[], char source[], int target_size) {int new_length; // temporarily hold length of source string

new_length = strlen(source);// can target hold a string this long?if (new_length > (target_size-1)) // allow for NULL

new_length = target_size; // fit in all we can

// copy character by characterfor (int i = 0; i < new_length; i++)

target[i] = source[i];

target[i] = ‘\0’; //add the NULL to the end of target}

DO DEMO 4

Page 34: 1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real

34

Summary We have been discussing strings in particular the Cstring form. This We have been discussing strings in particular the Cstring form. This

sort of string uses a NULL sentinelsort of string uses a NULL sentinel The string is an The string is an arrayarray or characters or characters You cannot assign with = operator, use strcpy() or write your own.You cannot assign with = operator, use strcpy() or write your own. You cannot compare with == use strcmp() or write your own.You cannot compare with == use strcmp() or write your own. Be careful when operating on strings not to write past the end of the Be careful when operating on strings not to write past the end of the

string!string! protect with a check for length.protect with a check for length.

Be careful not to overwrite the NULL character, or forget to insert it.Be careful not to overwrite the NULL character, or forget to insert it. When strings are used with functions it is like passing by reference. When strings are used with functions it is like passing by reference.

There is no pass by value for arrays!There is no pass by value for arrays! Note there is another way of defining strings using the string class (not Note there is another way of defining strings using the string class (not

covered here)covered here)