46
Data Structures

Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Embed Size (px)

Citation preview

Page 1: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Data Structures

Page 2: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Introduction to Data Structures

• What is Data ?– Any useful information – that can be stored or

memorized for future reference• In an organization it can be

– Employee’s name, sex, age, department, address so on• In Railway reservation office

– Passenger name, traveling date, traveling time, seat number so on• In Computers

– Set of integers, Structure(s) or any other user defined data type

Page 3: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Abstract Data Types (ADT)

• This is a higher level, domain and operations independent specification of data– While working on an “integer” type we don’t worry

about its low level implementation (can be 32 bit, or 64 bit number)

– Distance = rate * time (rate and time can be of type integer or real numbers)

– Employee record means same to the bosses of two different organizations, no matter how differently they are stored by the admin officers

Page 4: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Data Structures• Collection of data in an ordered fashion

– Ages of the students in a class are all numbers (data), but once are grouped in one line, one after the other becomes structured data (arrays)

– Age, class, sex, height, weight of a student is all data, but if we place them in one line per student it would be structured data (structures/struct)

– Arrays, list, queues, stack, doubly linked lists, structures etc are all data structures

• Different operations are defined for each type of data structure to add, delete or modify its content

Page 5: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Algorithms

• The operations defined on the data structures are generally knows as algorithms

• For each data structure there has to be defined algorithms

• Algorithms are normally used to add, delete, modify, sort items of a data structure

• All programming languages are generally equipped with conventional data structures and algorithms.

• New data structures and the algorithms for manipulation can be defined by the user

Page 6: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Why Study?

• Designed to develop students understanding the impact of structuring data to achieve efficiency of a solution to a problem

• After completion you will be familiar with important and most often used data structuring techniques.

• It will enable you to understand the manner in which data is organized and presented later.

Page 7: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Objectives of the course

• Present in a systematic fashion the most commonly used data structures, emphasizing their abstract properties.

• Discuss typical algorithms that operate each kind of data structure, and analyze their performance.

• Compare different Data Structures for solving the same problem, and choose the best

Page 8: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Abstract Data Type ADT

• A data type whose properties (domain and operations) are specified independently of any particular implementation

• ADT is a mathematical model which gives the a set of utilities available to the user but never states the details of its implementation.

• In OO-Programming a Class is an ADT

Page 9: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Data Structures: Definition

• A collection of data elements whose organization is characterized by accessing operations that are used to store and retrieve the individual data elements;

• The logical arrangement of data as used by a system for data management; a representation of a data model in computer form

Page 10: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Data Structures: More specifically– A data structure is a way of grouping fundamental

types (like integers, floating point numbers, and arrays) into a bundle that represents some identifiable thing.

• For example, a matrix may be thought of as the bundle of the number of rows and columns, and the array of values of the elements of the matrix. This information must be known in order to manipulate the matrix.

– C introduced the struct for declaring and manipulating data structures. C++ extended the struct to a class

Page 11: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Goals of Data structures

• Identify and develop useful mathematical entities and operations and to determine what classes of problems can be solved by using these entities and operations.

• Determine representation of those abstract entities and to implement the abstract operation on these concrete representations.

Page 12: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

A Real Life Example• Lisa• Michele• John• 110• 622-9823• 112-4433• 75• Bronson• Paola

Electronic Phone BookContains different DATA:

- names- phone number- addresses

Need to perform certain OPERATIONS:- add- delete- look for a phone number- look for an address

How to organize the data so to optimize the efficiency of the operations

Page 13: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Features of c++ you need to know

• Variables• Parameter Passing• Pointers• Classes and Objects• Inheritance• Others

Page 14: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Variables

• You must be very comfortable with the notion of a variable as an abstraction for a region of a memory. A variable has attributes such as name, type, value, address size, lifetime and scope.

Page 15: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Parameter Passing

• There are two parameter passing mechanisms in C++: pass-by-value and pass-by-reference. It is essential that you understand the behavioral difference between the two methods as well as the performance implications of using each of them.

Page 16: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Pointers

• Mastering the use of pointers is essential when programming in C++. The key to understanding pointers is to :– recognize that a pointer variable has exactly the

same set of attributes as any other C++ variable. – It is crucial that you keep straight the distinctions

between the :• value of a pointer, • the address of a pointer • the object to which a pointer points.

Page 17: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Classes and Objects

• A C++ class encapsulates a set of – Values

• The values are represented by the member variables of the class

– operations. • The operations by the member functions of the class.

– In C++ a class definition introduces a new type. – The instances of a class type are called objects. – Special role of the constructor and the destructor

member functions of a class and when the C++ compiler invokes each of them.

Page 18: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Variables

• Used to store data during program execution– Results of calculations– Temporary values– Control information

• Can hold many different types of data– Numbers, characters, strings of characters and

objects ( collections of data and programs )

Page 19: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

What is a program variable?

• A named location to store data– a container for data

• It can hold only one type of data– for example only integers, only floating point

(real) numbers, or only characters

Page 20: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Creating variables

• All program variables must be declared before using them

• A variable declaration associates a name with a storage location in memory and specifies the type of data it will store:

Type Variable_1, Variable_2, …;

• For example, to create three integer variables to store the number of baskets, number of eggs per basket, and total number of eggs:

int numberOfBaskets, eggsPerBasket, totalEggs;

Page 21: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Variables

• Variables store data in a program• Variables must be declared (or defined) before they

are used– Variable declarations

• Variable type• Variable name (identifier)

– Example• int nPosition, nHeight, nWidth;• char cAnswer;

Page 22: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Variable Names

• Naming rules– Identifiers must start with a letter, may contain

letters or numbers or _– There is essentially no limit to the length of an

identifier• Variable name should match its use

– Use dName rather than a– Use nHeight rather than x

Page 23: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Variable Types

• Primitive types– Basic building blocks of the C++ language. Match intrinsic

hardware capabilities.• Class types

– Contain:• One or more primitive types• Plus associated C++ statements to manage the

primitive types• Possibly even other class types

Page 24: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Primitive Types

short integer 1 byte

int integer 2 bytes

unsigned integer 4 bytes

long integer 8 bytes

float real 4 bytes

double real 8 bytes

char character 2 bytes

bool logical 1 bit/1 byte

Use the sizeof() operator for a particular system

Page 25: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Primitive Data Types

Type Name Kind of Value Memory Used Size Range

short integer 1 byte -128 to 127

int integer 2 bytes -32768 to 32767

unsigned Integer 2 bytes 0 to 65,535

long integer 4 bytes -2,147,483,648 to 2,147,483,647

float floating point 4 bytes +/- 3.4028… x 10+38 to+/- 1.4023… x 0-45

double floating point 8 bytes +/- 1.767… x 10+308 to+/- 4.940… x 0-324

char single character 2 bytes all ASCII characters

bool true or false 1 bit not applicable

Page 26: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Primitive Types

• Integer– Signed whole numbers only (no decimal)

• Real (Scientific Notation)– Signed numbers with two portions

• An exponent• A mantissa with a fixed precision

• Char– ASCII character

Page 27: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Integer vs Real Numbers

• Why two different systems?– Clearly the range of integers fit into the range of real

numbers• Integers are computationally very efficient

– If you can use integers, do so your program will run much faster

• Integers are precise– By their nature, real numbers often have very small errors

that creep into their representation. Integers do not have this issue.

Page 28: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Variable names: Identifiers

Rules- these must be obeyed

• all C++ identifiers must follow the same rules

• must not start with a digit• must contain only numbers,

letters, underscore (_) and $ (but avoid using $, it is reserved for special purposes)

• names are case-sensitive (ThisName and thisName are two different variable names)

Good Programming Practice - these should be obeyed

• always use meaningful names from the problem domain (for example, eggsPerBasket instead of n, which is meaningless, or count, which is not meaningful enough)

• start variable names with lower case• capitalize interior words (use

eggsPerBasket instead of eggsperbasket)

• avoid using $ since it is reserved for special purposes

Page 29: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Two main kinds of types in C++

primitive data types• the simplest types• cannot decompose into other

types• values only, no methods• Examples:

int - integerdouble - floating point (real)char - character

class types• more complex• composed of other types

(primitive or class types)• both data and methods

Page 30: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Syntax Templates

• Efficient method to communicate the grammar of a computer language– Similar to a sentence diagram– Indicates the required and optional portions of a

statement.

Page 31: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Assignment Statements

• Syntax templateVariable = Expression ;

• Operation– The expression on the right hand side is evaluated

and assigned to the memory location named by the variable on the left hand side.

Page 32: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Expressions

• Simple expression– Variable

• Arithmetic expressions– Binary operators (require two values)

• + - * / %

– Unary operators (only one value needed)• + - ++ --

Page 33: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Expression Examples

int nPosition, nHeight, nWidth;nPosition = 0;nHeight = 5;nWidth = nHeight;

nHeight = nHeight * 2;nPosition = nHeight + nWidth * 3 + 5;

float nXPos, nYPos;nXPos = 2.3;nYPos = nXPos / 0.3 ;

nHeight = -nWidth;nHeight = nWidth * -nPosition;

Page 34: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Additional Arithmetic Operators

• Increment and Decrement – ++ will increment a variable by 1– -- will decrement a variable by 1– A unary operator that can be prefix or postfix

• Modulus operator %– Integer division yields an integer result– % Returns the remainder of an integer division

Page 35: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Samples

int nSomeNumber, nQuotient, nRemainder;nSomeNumber = 17;nQuotient = nSomeNumber / 3;nRemainder = nSomeNumber % 3;float nAnswer;nAnswer = nSomeNumber / 3.0;nSomeNumber++;nQuotient = 7;nSomeNumber = nQuotient ++;nSomeNumber = ++nQuotient ;

Page 36: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Samples

void main() { cout<<"Hello World”<<endl; int nSomeNumber, nQuotient, nRemainder; nSomeNumber = 17; nQuotient = 7; nSomeNumber = nQuotient++; cout<<"nQuotient postfix is " << nQuotient<<endl; cout<<"nSomeNumber postfix is " << nSomeNumber<<endl; nSomeNumber = ++nQuotient ; cout<<"nQuotient prefix is " +<<nQuotient<<endl; cout<<"nSomeNumber prefix is " << nSomeNumber<<endl; }

Page 37: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Examples to run

int nCount = 0;int nValue = 0;

nValue = nCount++;cout<<"NCount is"+nCount+"nValue is ”<< nValue<<endl;nValue = ++nCount;cout<<"NCount is "+nCount+" nValue is ”<< nValue<<endl;nCount++;++nCount;

nValue = nValue + 1;

nCount = nValue--;nCount = nValue - 1;

Page 38: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Prefix and Postfix

• A prefix ++ or -- is evaluated (executed) first and the new value is available in the current expression

• A postfix ++ or -- is evaluated (executed) after the expression has been evaluated therefore the new value IS NOT available in the current expression

Page 39: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Precedence Rules

• How is the following expression evaluated?– nPosition = nHeigth / nWidth + 5 * 3;

• Why?• Precedence rules (introduction)

– First unary operators + , -, ++, -- – Next binary operators *, /, %– Then binary operators +, -– Unless there are parenthesis ( )

Page 40: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

© 2000 Scott S Albert

Precedence Exercise

int nCount = 0;int nValue = 0;

nValue = 1 + 2 – 3 – 4;nValue = 16 * 4 / 2 * 3;nValue = 7 – 3 * 4 + 2;

nValue = 6 * -nCount + 5;

nValue = (6 * (-nCount)) + 5;

Page 41: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Conversion between types• Implicit conversion

– C++ will automatically convert some types. This conversion is often call promotion since the conversion always moves to a more expansive type.

– shortintlongfloatdouble– Any type can be promoted to a type on its right

• byte can be promoted to long• float can not be promoted to short• int foo;

• double foobar;• foobar = 2.5;• foo = foobar; • cout<<"foobar is " << foobar;

Page 42: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Conversion between types

• Explicit conversion• C++ allows the programmer to explicitly

specify a conversion though a cast– This allows a conversion that would not be done

through promotion, but should be used with caution.

• Values are truncated (not rounded) without warning.• Special issues when casting characters to numbers

Page 43: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Console I/O

• Input from the keyboard, output to a “console” window.– Not Windows programming per se– Rather a rudimentary input and output

mechanism useful for simple programs where we don’t want to spend a great deal of time on the user interface

• No mouse, graphics, multiple windows etc.

Page 44: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Output Methods• Cout<<

– Outputs to the console window

• Use endl or \n for a new line

Input Methods

• Cin>>

Page 45: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Comments

• Two types of comments– // -- everything after a // is ignored by the

compiler to the end of the line– /* -- everything after a /* is ignored by the

compiler until a */ is reached

Page 46: Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an

Named Constants

• Named constants are of type– const

• Normally there names are all upper case• Examples

const int MAXNUMBER = 100;

const float MINTEMP = -40.0;