Data Structures - CS301 Power Point Slides Lecture 01
Data Structures - CS301 Power Point Slides Lecture 01
Data Structures - CS301 Power Point Slides Lecture 01
01
Data Structures
Data Structures
Prepares the students for (and is a
prerequisite for) the more advanced
material students will encounter in later
courses.
Cover well-known data structures such as
dynamic arrays, linked lists, stacks,
queues, tree and graphs.
Implement data structures in C++
Data Structures
Prepares the students for (and is a
prerequisite for) the more advanced
material students will encounter in later
courses.
Cover well-known data structures such as
dynamic arrays, linked lists, stacks,
queues, tree and graphs.
Implement data structures in C++
Data Structures
Prepares the students for (and is a
prerequisite for) the more advanced
material students will encounter in later
courses.
Cover well-known data structures such as
dynamic arrays, linked lists, stacks,
queues, tree and graphs.
Implement data structures in C++
Need for Data Structures
Data structures organize data more
efficient programs.
More powerful computers more
complex applications.
More complex applications demand more
calculations.
Need for Data Structures
Data structures organize data more
efficient programs.
More powerful computers more
complex applications.
More complex applications demand more
calculations.
Need for Data Structures
Data structures organize data more
efficient programs.
More powerful computers more
complex applications.
More complex applications demand more
calculations.
Organizing Data
Any organization for a collection of records
that can be searched, processed in any
order, or modified.
The choice of data structure and algorithm
can make the difference between a
program running in a few seconds or many
days.
Organizing Data
Any organization for a collection of records
that can be searched, processed in any
order, or modified.
The choice of data structure and algorithm
can make the difference between a
program running in a few seconds or many
days.
Efficiency
A solution is said to be efficient if it solves
the problem within its resource constraints.
– Space
– Time
int a, b;
b = 2;
a = b;
a = 5;
2 = a;
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;
b = 2;
a = b;
a = 5;
2 = a;
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;
b = 2;
a = b;
a = 5;
2 = a;
Array Name
‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Array Name
‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
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:
y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
It can be assigned a value. The new operator
returns as address that is stored in y.
We can write:
y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
It can be assigned a value. The new operator
returns as address that is stored in y.
We can write:
y = &x[0];
y = x; // x can appear on the right
// y gets 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;
Real life:
a. shopping list,
b. groceries list,
c. list of people to invite to dinner
d. List of presents to get
Lists
A list is collection of items that are all of the
same type (grocery items, integers, names)