Data Structures and Algorithms Elementary Data Structures
Data Structures and Algorithms Elementary Data Structures
1
◼ Static and Dynamic Data Structures
◼ Static Arrays
◼ Pointers
◼ Run-Time Arrays
◼ The Linked List Structure
◼ Some Linked List Operations
◼ Variations on Linked Lists
2
◼ What is abstract data type?
What is Data Structures?
What is Data Structures?
◼ A data structure is defined
A data structure by
is defined
by
◼ (1) the logical arrangement
(1) the logical of data
elements, arrangement
combined with of data
elements, combined
◼ (2) the set of operations we need to
with
access the elements.
(2) the set of operations
we need to access the
elements.
What is Data Structures?
Example: library
• is composed of elements (books)
• Accessing a particular book
requires knowledge of the
arrangement of the books
• Users access books only
through the librarian
the logical arrangement of data elements,
combined with
the set of operations we need to access the
elements.
Basic Data Structures
Structures include
linked lists
Stack, Queue
binary trees
…and others
What is Algorithm?
Algorithm:
• A computable set of steps to achieve a desired result
• Relationship to Data Structure
Example: Find an element
2 4 6
1 3 5 7
1 2 3 4 5 6 7
What is Algorithm?
The Process of Programming
• Understand the problem statement
• Write the algorithm
• Write the code
• Compile your code
• Solve the compile time errors
• Test, Run and debug runtime / logical error
An Example Algorithm
◼ Problem: Given a list of positive numbers,
return the largest number on the list.
1. Set max to 0.
2. For each number x in the list L,
1. compare it to max.
2. If x is larger, set max to x.
3. max is now set to the largest number in
the list.
◼ Static data are allocated memory at
Compile Time, i.e. before the program
is executed.
◼ Static data are allocated their memory
space in a place called the Data
Segment
◼ Static data cannot change size during
Run Time, i.e. while the program is
running.
10
◼ The Heap ( free memory)
◼ a heap is an area of pre-reserved computer main
storage ( memory ) that a program process can use to
store data in some variable amount that won't be
known until the program is running.
11
◼ A Dynamic Data Structure is allocated memory
at run-time. Consists of nodes to store data
and pointers to these nodes to access the
data.
◼ Nodes are created (allocated) and destroyed
(de-allocated) at run-time.
◼ Using dynamic allocation allows your
programs to create data structures with sizes
that can be defined while the program is
running and to expand the sizes when
needed.
◼ Abstraction:
A homogeneous sequence of elements with a fixed size
that allows direct access to its elements.
◼ Elements (members):
Any type, but all elements must be of the same type
◼ Relationship:
Linear (One-To-one). Ordered storage with direct access.
◼ Fundamental Operations:
◼ create array (by declaring it) with a size fixed at
compile time
◼ store an element in the array at a given position (direct
access)
◼ retrieve an element from a given position (direct
access)
13
◼ Create Array:
Size is fixed (constant):
e.g. int x[20], string name[50];
◼ Retrieve an element: e.g. z = x[ i ] ;
◼ Store an element: e.g. name[ i ] = “Ann” ;
14
◼ Arrays are always passed by reference
e.g. int findmax ( int x [ ] , int size );
◼ Can use const if array elements are not to
be modified, e.g.
int findmax ( const int x [ ] , int size );
◼ Do not include the array size within the
brackets when defining an array parameter.
◼ This is because the array name is the base
address of the array, and the size is already
known.
15
◼ Useful in representing a variety of data,
e.g. Tables, Matrices, Graphs and Images
Day Fri Sat Sun j
City i
Cairo 35 32 33 5 2 3
Tanta 34 31 31 4 1 1
Alex
29 28 28 9 8 8
A Table of Temp. A Matrix of Integers
16
◼ Declaration:
<element-type> <arrayName> [size 1][size2];
e.g.
double table[NROWS] [NCOLS];
◼ Indexing:
table[2] [4];
Row# Column#
0 .. NROWS 0 .. NCOLS
17
ADDRESS
For every variable there are two attributes: address
and value
20
◼ A pointer variable must be declared before it is used. It must
be bound to the same type as the variable it will point to.
◼ The asterisk operator * must precede each pointer name in
the declaration
◼ For Example:
double x = 3.14; // a variable of type double
double *p; // a pointer to double
p = &x; // p now stores the address of x
x
p
0012FF78 3.14
Starts at location 0012FF78
21
◼ (*) is the dereferencing (or indirection)
operator. It can be used to access the
value stored in a location.
◼ For example, if (p) is a pointer, the
value of (*p) is not the address stored
in p but is instead the value stored in
memory at that address (i.e. 3.14)
22
An asterisk has two uses with regard to pointers
▪ In a definition, it indicates that the object is a pointer
char *s; // s is of type pointer to char
▪ In expressions, when applied to a pointer it evaluates to the
object to which the pointer points (indirection or
dereferencing)
int k = 1;
int *p = &k; // p points to k
*p = 2;
cout << k << endl; // display a 2
23
◼ A node is an anonymous variable (has no name)
◼ No name is needed because there is always a pointer
pointing to the node.
Heap
Pointer Node
24
◼ The new operator allocates memory from the heap to a node of
specified type at Run Time. It returns the address of that node.
◼ The statements:
int *p ;
…………………………………….
p = new int;
create a new node of type int and let a pointer p point to it. No
data is put into the node
◼ The node created has no name, it is called an Anonymous
Variabe. It can only be accessed via its pointer using the
indirection operator, i.e. by using (*p)
25
◼ * - indirection operator
*p = 15.5;
// *p reads as: contents of node pointed to by p
◼ Stores floating value 15.5 in the node pointed to by p
*p
p
15.5
26
◼ Operation:
delete <pointer variable>;
29
The operator new can be used in an expression of the form:
new <Type> [n]
...
}
31
Because run-time arrays can take a lot of memory
from the heap, we must de-allocate that space after
we finish with it.
To return memory allocated to array pointed to by A,
use the delete operator in the form:
delete [ ] A;
32
int n;
cout << “Enter size of array: ";
cin >> n; // size is entered at run-time
if (n > 0)
{ int *A = new int [n]; // A is now the base address
// process A
for (int i = 0; i < n; i++) cin >> A[i];
………..
delete [ ] A; // Release memory locations
}
33
{
Int si=9;
Int * myarray=new int [si];
……
Extendarray(myarray,si);
}
Void Extendarray(int * x,int &size)
{
Size = size*5;
Int * h= new int[size];