Chapter 1-Introduction Part 1
Chapter 1-Introduction Part 1
INTRODUCTION
It’s a way organizing data in such a way so that data can be easier to use.
More precisely, a data structure is a collection of data values, the relationship among them, and the
operation that can be applied to the data.
ADT is a type defined in terms of its data items and associated operations, not its implementation.
ADT is a type for objects whose behavior is defined by a set of value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not
how these operations will be implemented.
how data will be organized in memory and
what algorithms will be used for implementing the operations.
It is called “abstract” because it gives an implementation independent view.
Objects such as lists, sets, and graphs, along with their operations, can be viewed as ADTs, just as integers, reals,
and Booleans are data types.
We can think of ADT as a black box which hides the inner structure and design of the data type.
A Stack contains elements of same type arranged in sequential order. All operations takes place at a single
end that is top of the stack and following operations can be performed:
pop() – Remove and return the element at the top of the stack, if it is not empty.
peek() – Return the element at the top of the stack without removing it, if the stack is not empty.
A Queue contains elements of same type arranged in sequential order. Operations takes place at both ends,
insertion is done at end and deletion is done at front. Following operations can be performed:
dequeue() – Remove and return the first element of queue, if the queue is not empty.
peek() – Return the element of the queue without removing it, if the queue is not empty.
A list contains elements of same type arranged in sequential order and following operations can be performed on
the list.
get() – Return an element from the list at any given position.
insert() – Insert an element at any position of the list.
remove() – Remove the first occurrence of any element from a non-empty list.
removeAt() – Remove the element at a specified location from a non-empty list.
replace() – Replace an element at any position by another element.
size() – Return the number of elements in the list.
isEmpty() – Return true if the list is empty, otherwise return false.
isFull() – Return true if the list is full, otherwise return false.
Primitive data structure:- used to represent the standard data types of any one of computer language (int,
char, float…..etc.)
Built in type
Used to represent single value
Directly operated upon by machine level instructions
Non-Primitive data structure:- It is a more sophisticated data structure emphasizing on structuring of a
group of homogeneous (same type) or heterogeneous (different type) data items.
can be constructed with the help of primitive data structure.
Used to store group of values
Arrays, linked list, stack, queue…etc.
Linear data structure: A linear data structure traverses the data elements sequentially, in which only one data
element can directly be reached. Ex: Arrays, Linked Lists
Non-Linear data structure: Every data item is attached to several other data items in a way that is specific for
reflecting relationships. The data items are not arranged in a sequential structure. Ex: Trees, Graphs
Stores a fixed-size sequential collection of elements (stored at contiguous memory locations) of the same type.
a pointer is a data structure that stores the memory address of another value located in computer memory.
type *var-name;
The actual data type, is the same, a long
hexadecimal number that represents a memory address.
* that returns the value of the variable located at the address
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get
Algorithms are generally created independent of underlying languages, i.e. an algorithm can be implemented
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should
be clear and must lead to only one meaning.
Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output.
Independent − An algorithm should have step-by-step directions, which should be independent of any programming code.
Problem: Given a list of positive numbers, return the largest number on the list.
Algorithm:
1. Set max to 0.
2. For each number x in the list L, compare it to Max. If x is larger, set Max to x.
3. Max is now set to the largest number in the list.
Problem: Given two numbers from a user, return the division of the two numbers.
Algorithm:
1. Set Res to 0.
2. Get number one from the user.
3. Get number two from the user.
4. Res now set to the result of number one divided by number two.
5. Display Res to the user.
Problem: Given a name of person from user, Display the name five times.
Algorithm:
1. Set num to 5
2. Declare a string variable Name.
3. Ask the user to enter the name of person and store it into Name variable.
4. While num is greater than or equal to zero, display the person name to user.
5. Finished.
Algorithm writing is a process and is executed after the problem domain is well-defined.
Problem − Design an algorithm to add two numbers and display the result.
Computation:
Some means of performing arithmetic computations, comparisons, testing logical conditions, and so forth...
Selection:
Some means of choosing among two or more possible courses of action, based upon initial data, user input and/or computed results
Iteration:
Some means of repeatedly executing a collection of instructions, for a fixed number of times or until some logical condition holds
The flowchart shows the steps as boxes of various kinds, and their
order by connecting the boxes with arrows.
Multiply Hours by
Pay Rate. Store
END result in Gross
Pay.
Terminal
END
START
Read Hours
Input/Output Operations
represented by parallelograms Display message
“How much do you Input/Output
indicate an input or output operation get paid per
Operation
hour?”
Display Gross
Pay
END
START
Read Hours
Processes
Display message
represented by rectangles “How much do
you get paid per
hour?”
indicates a process such as a mathematical computation or variable
assignment
Read Pay Rate
Multiply Hours
Multiply Hours
by Pay Rate. by Pay Rate.
Process
Store result in Store result in
Gross Pay.
Gross Pay.
Display Gross
Pay
END
START
Read Hours
Decision
Display message
represented by Diamond “How much do
you get paid per
Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Display Gross
Pay
END
FOUR FLOWCHART STRUCTURES
Sequence
Decision
Repetition
Case
SEQUENCE STRUCTURE
the diamond symbol indicates a yes/no question. If the answer to the question is yes,
the flow follows one path. If the answer is no, the flow follows another path
NO YES
DECISION STRUCTURE
In the flowchart segment below, the question “is x < y?” is answered. If the answer is
no, then process A is performed. If the answer is yes, then process B is performed.
NO YES
x < y?
Process Process
A B
DECISION STRUCTURE
The flowchart segment below shows how a decision structure is expressed in C++
as an if/else statement.
NO YES if (x < y)
x < y? a = x * 2;
else
Calculate a Calculate a a = x + y;
as x plus y. as x times 2.
DECISION STRUCTURE
The flowchart segment below shows a decision structure with only one action to
perform. It is expressed as an if statement in C++ code.
NO YES if (x < y)
x < y? a = x * 2;
Calculate a
as x times 2.
REPETITION STRUCTURE
A repetition structure represents part of the program that repeats. This type of
structure is commonly known as a loop.
REPETITION STRUCTURE
Notice the use of the diamond symbol. A loop tests a condition, and if the condition
exists, it performs an action. Then it tests the condition again. If the condition still
exists, the action is repeated. This continues until the condition no longer exists.
REPETITION STRUCTURE
In the flowchart segment, the question “is x < y?” is answered. If the answer is yes,
then Process A is performed. The question “is x < y?” is answered again. Process A is
repeated as long as x is less than y. When x is no longer less than y, the repetition
stops and the structure is exited.
YES
x < y? Process A
REPETITION STRUCTURE
while (x < y)
YES x++;
x < y? Add 1 to x
CONTROLLING A REPETITION STRUCTURE
The action performed by a repetition structure must eventually cause the loop to
terminate. Otherwise, an infinite loop is created.
In this flowchart segment, x is never changed. Once the loop starts, it will never end.
QUESTION: How can this
flowchart be modified so
it is no longer an infinite YES
loop? x < y? Display x
CONTROLLING A REPETITION STRUCTURE
ANSWER: By adding an action within the repetition that changes the value of x.
YES
x < y? Display x Add 1 to x
A PRE-TEST REPETITION STRUCTURE
YES
x < y? Display x Add 1 to x
A POST-TEST REPETITION STRUCTURE
C++ Code
Display x
do
{
Flowchart cout << x << endl;
Add 1 to
x++;
x } while (x < y);
YES
x < y?
CASE STRUCTURE
The structure below indicates actions to perform depending on the value in years_employed.
CASE
years_employed
1 2 3 Other
If years_employed = If years_employed =
2, bonus is set to 200 3, bonus is set to 400
If years_employed = If years_employed is
CASE
1, bonus is set to 100 years_employed any other value,
bonus is set to 800
1 2 3 Other
YES
x < y? Display x Add 1 to x
COMBINING STRUCTURES
NO YES
x > min?
Display “x is Display “x is
outside the limits.” within limits.”
ALGORITHM FOR: SUM OF 2 NUMBERS