CS204-lec01 - Intro, Array, Array List
CS204-lec01 - Intro, Array, Array List
01
Data Structures
VU
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.
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;
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
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)