Title: COSC 2006 E: Data Structure I
1COSC 2006 E Data Structure I
- Instructor Dr. Haibin Zhu
- Tel 705-474-3450 ext. 4434
- Fax 705-474-1947
- Email haibinz_at_nipissingu.ca
- URL http//www.nipissingu.ca/faculty/haibinz
- Office Hours Contact me by emails.
- TEXT BOOK
- Data Abstraction Problem Solving with C, 5/E,
Frank M. Carrano, ISBN-10 0321433327, ISBN-13
9780321433329, Addison-Wesley, 992 pp, 2006.
2- GOAL OF THE COURSE
- Good Familiarity with C
- Ability to design and write programs
- Good knowledge of the basics of Computer
Science - PREREQUISITES
- C Programming language
- Program Design
- Running a program
- Minimal knowledge of computer hardware
- Basic Algorithms
3- C LANGUAGE
- Good knowledge of basic features
- Some familiarity with intermediate features
- BASIC FEATURES
- Data Structures
- Control Structures
- DATA STRUCTURES
- Elementary data types (int, char, float)
- One dimensional arrays
- CONTROL STRUCTURES
- Branching
- Looping
- Abstraction
4-
- BRANCHING
- if
- if else
- switch
- LOOPING
- while
- do while
- for
- ABSTRACTION
- Function
5- INTERMEDIATE FEATURES
- Structures
- Pointers
- Simple Recursion
- PROGRAM DESIGN
- Top Down Design
- Modular Design
- Hiding module implementation
- RUNNING A PROGRAM
- Editing a program
- Compiling a program
- Running a program
- Debugging a program
- Documenting a program
6- HARDWARE
- Random Access Memory
- Disks and disk files
- Central Processing Unit
- Bits, Bytes, and Words
7General Aspects in Programming
8What is a Good Solution?
- A solution is good if
- The total cost it incurs over all phases of its
life cycle is minimal - The cost of a solution includes
- Computer resources that the program consumes
- Difficulties encountered by users
- Consequences of a program that does not behave
correctly - Programs must be well structured and documented
- Efficiency is one aspect of a solutions cost
9Key Issues in Programming
- Modularity
- Style
- Modifiability
- Ease of Use
- Fail-safe programming
- Debugging
- Testing
10Key Issues in Programming Modularity
- Modularity has a favorable impact on
- Constructing programs
- Debugging programs
- Reading programs
- Modifying programs
- Eliminating redundant code
11Key Issues in Programming Style
- Use of private data members
- Proper use of reference arguments
- Proper use of methods
- Avoidance of global variables in modules
- Error handling
- Readability
- Documentation
12Key Issues in Programming Modifiability
- Modifiability is easier through the use of
- Named constants
- The typedef statement
13Key Issues in Programming Ease of Use
- In an interactive environment, the program should
prompt the user for input in a clear manner - A program should always echo its input
- The output should be well labeled and easy to
read
14Key Issues in Programming Fail-Safe Programming
- Fail-safe programs will perform reasonably no
matter how anyone uses it - Test for invalid input data and program logic
errors - Check invariants
- Enforce preconditions
- Check argument values
15Key Issues in Programming Debugging
- Programmer must systematically check a programs
logic to find where an error occurs - Tools to use while debugging
- Single-stepping
- Watches
- Breakpoints
- cout statements
- Dump functions
16Key Issues in Programming Testing
- Levels
- Unit testing Test methods, then classes
- Integration testing Test interactions among
modules - System testing Test entire program
- Acceptance testing Show that system complies
with requirements
17Key Issues in Programming Testing
- Types
- Open-box (white-box or glass-box) testing
- Test knowing the implementation
- Test all lines of code (decision branches, etc.)
- Closed-box (black-box or functional) testing
- Test knowing only the specifications
18Key Issues in Programming Testing
- Developing test data
- Include boundary values
- Need to know expected results
19Key Issues in Programming Testing
- Techniques
- assert statements to check invariants
- Disable, but do not remove, code used for testing
- / and /
- Booleans
- Macros
20Key Issues in Programming Testing
- Stubs
- An incompletely implemented method that simply
acknowledges that it was called - Drivers
- A module that tests another module
- For example, a main function
21ALGORITHMS
- What is an Algorithm?
- A step-by-step specification of a method to solve
a problem within a finite amount of time. - Search
- Binary Search
- Sort
- Selection Sort
- Bubble Sort
22- MATERIAL COVERED
- Quick Review and Advanced Features of C
- Abstract Data Types
- Sorting Algorithms
- Complexity Theory
- C (APPENDIX A)
- Basics
- I/O using iostream
- One-dimensional Arrays
- Multidimensional Arrays
- Structures
- Files
23- C (CHAPTER 4)
- Pointers
- Linked Lists
- Circular Linked Lists
- Other kinds of Linked Lists
- ABSTRACT DATA TYPES
- Basics (Chapter 3)
- Stacks (Chapter 6)
- Queues (Chapter 7)
- Trees (Chapter 10)
- Graphs (Chapter 13)
- Priority Queues (Chapter 11)
24- SORTING ALGORITHMS
- Quick Review of Selection Sort and Bubble Sort
(Chapter 9) - Insertion Sort
- Quicksort and Mergesort
- Radix Sort
- Heap Sort (Chapter 11)
- COMPLEXITY THEORY
- How fast are search algorithms?
- How fast are sort algorithms?
25- COMPUTER USE
- Visual C on PC
- COMPUTER USE IS MANDATORY
- INITIAL PREPARATION
- Read Chapter 1 and Appendix A till next class
- WE ADVANCE TO APPENDIX A.
26Review of C Fundamentals
27 Comments
- // Are line comments.
- They apply to the end of the line.
- / and / are bracket comments.
- Everything in between is a comment.
- Such comments may not be nested.
28Identifiers Keywords
- Identifiers consist of letters, number, and the
underscore character(_), - The first letter may not be a number
- Uppercase and lowercase are distinct
- Keywords are predefined identifiers.
- They may only be used as defined by the language.
29Fundamental Data Types
- Character, Signed Integer, Unsigned, Integer,
Floating Point. - char, signed char, unsigned char
- short, int, long
- unsigned short, unsigned, unsigned long
- float, double, long double
- all the above are integral types
30Characters
- Characters are enclosed in (singlequotes)
- Special characters are marked with \(backslash)
\n \t \ \ \0 \\ - Literal Constants
- Integer constants are by default int, or if
necessary long. - Floating point constants are by default of size
double. They - may be written in scientific notation, e.g. 4.5e10
31Variables
- Declaration Informs the compiler that something
exists. - Definition Informs the compiler that something
exists and instructs the compiler to reserve
space for it. Optionally, a definition of a
variable may contain an initializer. - Examples
- int x, y, z
- float volume
- int i 0
- float test 4.0
32Named Constants
- A constant cannot be changed at run time.
- C style constants
- define zero 0
- C style constants
- const int zero 0
- const float PI 3.14159
33Enumerations
- enum SU, MO, TU, WE, TH, FR, SA
- enum Boolean FALSE, TRUE
- enum plus , minus -
- enum winter 1, spring, summer, fall
- enum on, off switch
34TYPE Definitions
- typedef double real
- Now you can use the new term real.
- If you need to deal with longer numbers you
need to replace only one single statement.
Otherwise you would need to replace every single
place where you declared a variable to be double. - Example typedef long double Real
35Arithmetic Expressions
- You can form an expression by counting
parentheses variables, constants, operators and - , -, , /, (binary)
- , - (unary)
- Usual priority rules apply
- Most operators are left associative
- A / B C means (A / B) C
36Relational and Logical Expressions
- relational operators lt, lt, gt, gt
- equality operators , !
- logical operators ! (unary), ,
- (and) and (or) use short circuit means that
C evaluates logical expressions from left to
right and stops as soon as the value of the
entire expression is apparent - Evaluation Example
- (5 4) (A lt B)
- (5 5) (A lt B)
37Conditional Expressions
- Like an IF ELSE but it returns a value.
- LargerAB (A gt B) ? A B
- Expression1?expression2expression3 has the
value of either expression12 or expression3
according to whether expreesion1 is true or
false. - Conditional Expressions may be nested (if you
can keep them straight...) - Implicit Type Conversion
- A complicated topic, but important
- Integral Promotion char and short are converted
to int. - Then the following order is used for finding a
common type - int - unsigned - long - unsigned long - float
-double- long double
38Explicit Type Conversion
- Functional Format type(expression)
- float x 4.5 int y y int(x)
- Cast Format (type) expression
- float x 4.5 int y y (int) x
39Assignment
- A B 5
- A B C means B C A B (right
associative) - A B means A A B
- , -, , /, , .....
- A, A means A A 1 A--, --A same
- B A means that the is done FIRST
- B A means that the is done FIRST
40Elementary I/O
- include ltiostreamgt
- cin gtgt X gtgt Y
- Read first input into X, and second into Y.
- What is read depends on the type of X, Y.
- By default, white space is skipped.
- Variant for char ch cin.get()
41Elementary I/O (2)
- cout ltlt A text ltlt X ltlt \n
- For char you can also use cout.put(\n)
- Flags and manipulators control formatting see
Figure A-5 Page A-15 . - cout ltlt setprecision(2) 2 positions after.
- cout.setf(iosright) right justified
- cout ltlt setw(6) ltlt ABC field width
- include ltiomanipgt
42Functions (value and reference)
- int max(int X, int Y)
- return (X gt Y) ? X Y
-
- void max2 (int X, int Y, int Larger)
- Larger ((X gt Y) ? X Y)
-
43The IF Statement
- if (A gt B)
- // do something
-
- else
- // do something else
44Switch Statement
- switch (points)
- case 0 grade F break
- case 1 case 2 grade D break
- default cout ltlt error\n
-
45WHILE
- cin gtgt nextvalue
- sum 0
- while (nextvalue gt 0)
- sum nextvalue
- cin gtgt nextvalue
-
46- FOR LOOP
- for (int counter 1 counter lt N counter)
- cout ltlt counter
- Elements may be omitted, but not the
- Elements may contain several statements,
- separated by ,
- DO Statement
- do
- cin gtgt X
- // something or other
- while (X gt 0)
47 - Break and Continue
- Break gets you out of the loop. Completely out.
- Continue skips the rest of the loop and restarts
with the next iteration at the beginning. For
for loops the after action is still performed
before jumping out of the loop.
48Arrays
- One Dimensional Arrays
- Multidimensional Arrays
- Arrays of Arrays
- Strings
- One-Dimensional Arrays
- int ar130
- This defines ar10, ar11,..... ar129
- Better way of doing it
- const int size 30 // define size
- typedef int myarraysize //def type
- myarray bunch_of_numbers
49- Arrays over enumerations
- enum days SU, MO,...
- days Day
- for (Day SU DayltSA Daydays(Day1))
- cout ltlt array_of_numsDay ltlt endl
- Initialization and Assignment
- Array assignment is illegal!!!!!!(if a and b has
the same type then a b is illegal) - Initialization arrayType X 1, 3, 4
- int a 6 2, 2, 2, 5, 5, 0
- If you dont initialize all values, the rest
are set to 0. - Arrays in function headers may have open
- sizes . Arrays are never passed by value.
50CONST Array Parameters int
- compute_somethg(const int vals, int N)
- N stores the valid size which the function
- might want to know.
- CONST avoids side-effects in the array.
-
- We need this as arrays are not passed by value.
51Two-Dimensional Arrays
- int matrix num_of_rowsnum_of_cols
- matrix 00, ... matrix01, matrix02,
matrix 10,.... - Initialization is possible as before. Values
are assigned in the above order, that is
row-wise.
52Arrays of Arrays
- typedef int column10// def column
- typedef column columns20 //columns
- columns my_numbers
- my_numbers0 ... first column
- my_numbers01 ... second number in the
- first column.
- (Note that the order is inversed relative to a
two-D array.)
53C Strings
- A string is an array of characters (C-Strings).
- Literal strings are written as abcd
- The compiler adds a \0 to the end of a
- literal string. Thus the array has to be by one
larger than the largest text string. - Strings are are assigned with
strcpy(target,source) from string - strcmp(S1, S2) returns 0 if two strings are the
same. - It returns a negative number if S1is before S2,
and a positive number otherwise. - strcat(S1, S2) appends S2 to the end of S1
- and returns S1. In other words, it is
side-effecting.
54C String I/O
- Strings can be read with cin gtgt if no white
- space is contained in them.
- Strings can be written with cout ltlt
- cout.write(S1, N) writes N characters only.
- getline(S, count) reads in count-1 characters,
even if whitespace is contained. Then a \0 is
appended.
55C String (1)
- string str, str1, str2
- //read a word from an istream
- cin gtgt str cin gtgt str1 cin gtgt str2
- //find the length of the string
- coutltltstr.size()ltltendl
- //find if a string is empty
- coutltltstr.empty()ltltendl
- //access the char at index i
- int i 3 cout ltltstriltltendl
56C String (2)
- //concatenate two strings
- cout ltltstr1 str2ltltendl
- //access a substring of a string
- int Pos 3
- int NumChars3
- cout ltltstr.substr(Pos, NumChars)ltltendl
- //insert a substring into a string
- Pos 5
- string SubStr "OK"
- cout ltltstr.insert(Pos, SubStr)ltltendl
57C String (3)
- //remove a substring
- Pos 3
- NumChars 2
- coutltltstr.erase(Pos, NumChars)ltltendl
- //find a substring in a string
- string Pattern "OK"
- int StartPos 0
- cout ltltstr.find(Pattern, StartPos)ltltendl
58C String (4)
- //compare two strings
- cout ltlt(str1 str2)ltltendl
- cout ltlt(str1 ! str2)ltltendl
- cout ltlt(str1 lt str2)ltltendl
- cout ltlt(str1 gt str2)ltltendl
- cout ltlt(str1 lt str2)ltltendl
- cout ltlt(str1 gt str2)ltltendl
- ex1.cpp
59Struct
- struct person
- my_string Name
- int Age
- float GPA
-
- Initialization and Assignment
- person jamie Jamie Fay, 19, 3.1
- Structures may be copied by .
- Structures may be passed by value.
- Structures may be returned from functions.
- Structures may be nested.
- Arrays of Structures may be built.
60Structure Examples
- struct addr
- int number
- string street
-
- struct person
- string name
- int age
- addr address // and more stuff of course
-
- typedef person many_people size
- // def many_people
- many_people cosc2006 //cosc2006 is an array of
- // large_constant people
- cosc2006 2.age // a member of the 3rd element
of the array
61File Input/Output
- If you know how to use fprintf and fscanf from C
then you can use them. - Files may be text files (files of chars) or
binary files. - You still may read and write numbers from and
to text files. C does the necessary
translations for you. - include ltiostreamgt // cin, cout,
... - include ltfstreamgt // ifstream,
ofstream, ... - Character Output
- ofstream another_file_var(data.dat)
- another_file_var ltlt Character // or
- another_file_var.put(Character)
- ex1-2.cpp
62- Useful Functions for Input
- my_file_var.ignore(2) // skip 2 characters
- ch my_file_var.peek() // return but leave
- ch my_file_var.get() // variant of ltlt syntax
- while (my_file_var.peek() ! EOF) ...
- while (my_file_var gtgt Char) // TRUE
until // EOF - Numeric I/O from Text Files
- If the input is numeric, AND if we read into a
variable of a - numeric type, C will automatically do the
right thing. - E.g., if the input contains 234 and III is an
integer variable - my_file_var gtgt III // III will contain 234
63- Example Character Input
- include ltfstreamgt
- ifstream my_file_var
- my_file_var.open(disk_name)
- if (!my_file_var) process_error()
- my_file_var gtgt char_variable
- my_file_var.close()
- ex1-3.cpp
64Binary Files
- ofstream my_3_file(dsk.nme, iosbinary)
- Binary files permit more efficient storage of
- numeric and mixed data
- Binary files permit more efficient reading
- and writing of numeric and mixed data
- Binary files CANNOT be read by a human
- Binary files CANNOT be edited normally
65- File Organization
- All but the simplest programs are split into
several files. - Header files (.h) contain all declarations
needed for - compiling OTHER Implementation files (.cpp). They
are included. - Header files contain function Declarations
- but Constant Definitions and typedefs.
66Directives
- Avoiding Multiple Includes
- To avoid multiple inclusion of the same
- header file, it is a good idea to bracket it with
- the following lines
- ifndef some_name
- // header file text
- define some_name
- endif
67The Last