Data Structure Lecture 1
Data Structure Lecture 1
Data Structure Lecture 1
Assignments = 15%
Quizzes = 10%
Mid Term = 25%
Final Term = 50%
Evaluation Criteria
sequential structure.
The different non-linear data structures are tree,
and graphs.
Need for Data Structures
Data Structures organize data =>more efficient programs.
More powerful computers => more complex applications.
More complex applications demand more calculations.
Programming effort.
Efficiency
A solution is said to be efficient if it solves the
problem within its resource constraints.
Space
Time
The cost of a solution is the amount of resources
that the solution consumes.
Selecting a Data Structure
Select a data structure as follow:
Analyze the problem to determine the resource
X [1]
The memory can be
thought of as an array. X [2]
X [3]
X [4]
X [5]
array
What is Array Name?
‘x’ is an array name but there is no variable x.
‘x’ is not an lvalue.
For example, if we have the code
int a, b;
Then we can write
b= 2;
a = b;
a = 5;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Dynamic Arrays
You would like to use an array data structure but
you do not know the size of the array at compile
time.
You find out when the program executes that you
need an integer array of size n=20.
Allocate an array using the new operator:
Int* y =new int[20]; //or int* y=new int[n];
y[0] = 10;
y[1] = 15; //use is the same
Dynamic Arrays
‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cell in memory.
It can be assigned a value. The new operator
returns as address that is store in y.
We can write:
y = &x[0];
y = x; //x can appear on the right
//y get the address of the first cell of
the x array
Dynamic Arrays
We must free the memory we got using the new
operator once we are done with the y array.
delete[]y;