Вы находитесь на странице: 1из 68

Data Structures

Engr. Gil R. delas Alas Jr. CPE-Instructor

a data structure is a particular way of storing and organizing data in a computer. Common data structures include: array, linked list, hash-table, heap, B-tree, red-black tree, trie.
(http://en.wikipedia.org/wiki/Data_structure#Basic_principles) (http://en.wikipedia.org/wiki/Data_structure#Basic_principles)

the term data structure refers to a scheme for organizing related pieces of information.
http://www.webopedia.com

A data structure is a specialized format for organizing and storing data http://searchsqlserver.techtarget.com a data structure may be selected or designed to store data for the purpose of working on it with various algorithms. http://searchsqlserver.techtarget.com

The curiosity of our surroundings is the constant force that drives humanity forward. In an effort to better ourselves and our future we study the world we live in. Our own inventions define the way in which we lead our lives, and what people we become. Computers have now become the center of many cultures around the world, as technology unites people in a way that has never been seen before, changing the way we view each other. In the last decade, the Internet has become a medium that lets people share their ideas and opinions with millions of others. We are very close to a world where ignorance is no longer an excuse. Computers are, and will be even more, in the center of our lives. -- "World Around Us" by Alec Solway

Common Types of Data Structure


Array Lists Trees Heaps Tries

Data Types


is a classification identifying one of various types of data that determines the possible values for that type; the operations that can be done on that type; and the way the values of that type are stored. Basic Data types: Integer,Float,Double,Char,String,Boolean.

Integer


int height = 100; int balance = -67; char menuSelection = 'q'; char userInput = '3'; signed char myChar = 100; signed char newChar = -43

Char


Float/Double


float celsius = 37.623; double fahrenheit = 98.415; char strvar2[]="string can also be initialized like this for the C++ Tutorial";

String


ALGORITHM
an algorithm ( i / l m/) is an effective method expressed as a finite list[1] of well-defined instructions[2] for calculating a function[3]. Algorithms are used for calculation, data processing, and automated reasoning.
http://en.wikipedia.org/wiki/Algorithm

an algorithm is any set of detailed instructions which results in a predictable end-state from a known beginning. An algorithm is a finite set of instructions which, if followed, accomplish a particular task.

HISTORY
Originally named after the mathematician in 9th century by Abu Abdullah Muhammad bin Musa alKhwarizmi.

Algorithm as Algorism
The word algorism originally referred only to the rules of performing arithmetic using Arabic numerals but evolved into algorithm by the 18th century.

Criteria
Analyze the problem
Is required to ensure that the problem is clearly defined and understood.

Develop a solution
Selecting an exact set of steps.

Code the Selection


Sequence It is the order in which instructions are executed by the program. manner. Selection It provides the capability to make choice between different operations, depending on the results of the condition. Iteration Looping or a repetition. Referred to as a It is a Straight forward

It provides the ability for the same operation to be repeated based on the value of the condition. Invocation as it is needed. It is a set of statements

Test and correct the program.


-Used to verify that the program works correctly and fulfills its requirements or desired output.

PSEUDOCODE
Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool. Pseudocode is a kind of structured english for describing algorithms

FLOWCHARTING
>is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. >graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step.

Basic Flowchart Symbols


The Process Symbol represents any process, function, or action and is the most frequently used symbol in flowcharting. The Document Symbol is used to represent any type of hard copy input or output (i.e. reports).

Offpage Connector Symbols are used to indicate the flowchart continues on another page. Often, the page number is placed in the shape for easy reference. The Input/Output Symbol represents data that is available for input or resulting from processing (i.e. customer database records)

Comment Symbols are used when additional explanation or comment is required. This symbol is usually connected to the symbol it is explaining by a dashed line.

The Decision Symbol is a junction where a decision must be made. A single entry may have any number of alternative solutions, but only one can be chosen.

The Connector Symbol represents the exit to, or entry from, another part of the same flowchart. It is usually used to break a flow line that will be continued elsewhere. It's a good idea to reference page numbers for easy location of connectors. Flow lines indicate the flow of operation, that is, the exact sequence in which the instruction are executed.

EXERCISE. 1. Write an algorithm that will debug a program. 2. Create an algorithm that will check a nonfunctioning lamp. 3. Create a flow chart For this pseudocode:
If student's grade is greater than or equal to 60 Print "passed Else Print "failed"

4. Create a flowchart and pseudocode that will display the highest of the 3 input numbers. 5. Create a flowchart and pseudocode that will display if the input number is positive or negative.

FUNDAMENTAL OF C++
BASIC STRUCTURE OF C++ PROGRAMMING.

Preprocessor directive it tells the C++ compiler where to find the information of the instruction on the program body.

#include it instruct the compiler to take account of the file

Header a built in file where particular objects are define. It denoted by .h extension file.

2 types of directive #include #define -defining a constant Ex. #define PI=3.1416 -defining a macro Ex. #define cout c #define NEWLINE '\n'

Local and global declaration of variable Local variable Is a variable declared inside a block. Ex.

Global variable A variable is declared outside the block. Ex. Int x,y=5; Main() { Z=x+y; Cout<<Z; }

main function : main()


It is also called a driver function since it is the first function that will be executed once the program is compiled. There must have one and only one main() function.

Program body
Input and Output operator Output operator Is represented as << It is also called as put operator or Stream insertion operator. Displays the value of each variable or constant in order in which it appears in the output list. Ex. cout<< hello world ;

Input operator It represented as a symbol of >> Also known as get operator or extraction operator. Reads data from the keyboard into the memory during program execution.

Open and Close Brace


Open brace Is symbolizes as { Determines the beginning of the function body and may also denote the start of a certain block Close brace Is symbolizes as } Determines the end of the function body and may also signify the end of a certain block

Statement
Basic component of a function and usually terminated by the terminator operator, the semicolon(;). Ex. Z=x+y; cout<< enter a number <<endl; cin>>x;

OPERATORS
Arithmetic Operators If x=5 and y=2

Addition Multiplication Division Subtraction Modulus

+ * / %

x+y; x*y; x/y; x-y; x%y;

7 10 2.5 3 1

OPERATORS
Relational Operators If x=5 and y=2

Greater than

>

If(x>y)

True True False False False True

Greater than >= If(x>=y) or equal to Less than < If(x<y) Less than or <= If(x<=y) equal to Equal to == If(x==y) Not equal to != If(x!=y)

OPERATORS
If x=5 and y=2 Logical Operator

Logical OR Logic AND

|| If(x==y || x==5) True && If(x==y && x==5) False

OPERATORS
Combinational operator If x=5 and y=2

Add to Subtract to Multiply to Divide to Mode to

+= -= *= /= %=

x+=y; x-=y; x*=y; x/=y; x%=y;

x=x+y; x=x-y; x=x*y; x=x/y; x=x%y;

7 3 10 2.5 1

OPERATORS
Increment / Decrement If x=5 and y=2
Pre version performs the operation(either by adding 1 or subtracting by 1) on the subject before the resulting value is used in its surrounding context. Post version performs the operation after the object current value has been used.

Pre-increment Post-increment Pre-decrement Post-decrement

++m m++ --m m--

Program sample.
1.#include<iostream.h> #include<conio.h> main() { int x,y;

x=44; y=++x; cout<<"x = "<<x<<"\ty = "<<y; x=44; y=x++; cout<<"\nx = "<<x<<"\ty = "<<y; getch(); }

#include<iostream.h> #include<conio.h> main() {//combine operator int n=22; cout<<"n = "<<n<<endl; n+=9; cout<<"after n+=9, n = "<<n<<endl; n-=5; cout<<"after n-=5, n = "<<n<<endl; n*=2;cout<<"after n*=2, n = "<<n<<endl; n/=3; cout<<"after n/=3, n = "<<n<<endl; n%=7;cout<<"after n%=7, n = "<<n<<endl; getch(); return 0; }

Selection/ conditional statement

1. If Statement
#include<iostream.h> #include<conio.h> int x,y; main() { cout<<"enter 2 positive integers: "; cin>>x>>y; if(x%y) cout<<x<<" is not divisible by "<<y; getch(); return 0; }

2. If
int x,y; main() {

else Statement

#include<iostream.h> #include<conio.h>

cout<<"enter 2 positive integers: "; cin>>x>>y; if(x%y) cout<<x<<" is not divisible by "<<y; else cout<<x<<" is divisible by "<<y; getch(); return 0; }

3. Nested if statement

4. Else if statement

5. Switch statement

Combinational expression operator

functions
syntax:

function is a group of statements that is executed when it is called from some point of the program.

type name ( parameter1, parameter2, ...) { statements } // function example #include <iostream> using namespace std; int addition (int a, int b) { int r; r=a+b; return (r); } int main () { int z;

Functions
1. Break(); Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. #include<conio.h>
#include <iostream> using namespace std; int main (){ int n; for (n=10; n>0; n--) { cout << n << ", "; if (n==3) { cout << "countdown aborted!"; }}getch();} break;

10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!

2. Continue statement
The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration. //continue loop example #include <iostream> #include<conio.h> using namespace std; int main (){ for (int n=10; n>0; n--) { if (n==5) continue; cout << n << ", ";} cout << "FIRE!\n"; getch(); return 0;}

10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!

3. Goto statement goto allows to make an absolute jump to another point in the program. You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations. The destination point is identified by a label, which is then used as an argument for the goto statement. A label is made of a valid identifier followed by a colon (:).

#include <iostream> using namespace std; int main () { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE! int n=10; loop: cout << n << ", "; n--; if (n>0) goto loop; cout << "FIRE!\n"; return 0; }

// factorial calculator #include <iostream> using namespace std; long factorial (long a) { if (a > 1) return (a * factorial (a-1)); else return (1); } int main () { long number; cout << "Please type a number: "; cin >> number; cout << number << "! = " << factorial (number); return 0; }

// declaring functions prototypes #include <iostream> using namespace std; void odd (int a); void even (int a); int main () { int i; do { cout << "Type a number (0 to exit): "; cin >> i; odd (i); } while (i!=0); return 0; } void odd (int a) { if ((a%2)!=0) cout << "Number is odd.\n";

Array
array is simply a number of memory locations, each of which can store an item of data of the same data type and which are all referenced through the same variable name. How to declare an Array? array_type variable_name[array_size];

Example:
int Age [5] = {30, 22, 33, 44, 25}; int Age [5]; Age [0]=30; Age [1]=22; Age [2]=33; Age [3]=44; Age [4]=25;

Вам также может понравиться