Professional Documents
Culture Documents
Luciano Bononi
<bononi@cs.unibo.it>
http://www.cs.unibo.it/~bononi/
(slide credits: these slides are a revised version of slides created by Dr. Gabriele DAngelo)
Introduction to algorithms
arrays
linked lists
Luciano Bononi
2010-2011
Luciano Bononi
2010-2011
Algorithm: etymology
(source: wikipedia)
Luciano Bononi
2010-2011
2010-2011
Luciano Bononi
2010-2011
OUTPUT
INPUT
a permutation of the
sequence of numbers
sequence of numbers
10
Sort
b1,b2,b3,'.,bn
Correctness
Running time
Depends on
10
algorithm
2010-2011
Strategy
empty handed
card
in
the
right
inserted/sorted
Luciano Bononi
2010-2011
(from wikipedia)
2010-2011
Data structures
The title of this course is Algorithms and Data Structures
DEFINITION:
A data structure is a way to store and organize data in order to facilitate
operations on them (e.g. data access and modification)
2010-2011
10
representation:
data can be stored variously according to their type (for
organization:
Luciano Bononi
2010-2011
11
creation
Luciano Bononi
2010-2011
12
Algorithm:
outline, the essence of a computational procedure, step-bystep instructions
Program:
an implementation of an algorithm in some programming
language
Data structure:
organization of data needed to solve the problem
2010-2011
13
Overall picture
Data structure and
Implementation goals
Robustness
Correctness
Adaptability
Efficiency
Reusability
Luciano Bononi
2010-2011
14
Data structures
{false, true}
Digit
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Letter
{A, B, , Z, a, b, , z}
NaturalNumber
{0, 1, 2, }
Integer
String
2010-2011
15
List is empty
Relations:
n=0
Luciano Bononi
2010-2011
16
Create a list
Create(L)
Delete a list
Destroy(L)
IsEmpty(L)
Length(L)
Find(L, k)
Search(L, x)
Delete(L, k)
Insert(L, x, k)
2010-2011
17
Usually arrays are fixed-size, that is: their size cannot change
once their storage has been allocated (i.e. it is not possible to
insert new elements in the middle of the array)
Luciano Bononi
2010-2011
18
Luciano Bononi
2010-2011
19
Create a list
Create(L)
Delete a list
Destroy(L)
IsEmpty(L)
Length(L)
Find(L, k)
Search(L, x)
Delete(L, k)
Insert(L, x, k)
Luciano Bononi
2010-2011
20
10
Search(L, x)
2.
Delete(L, k)
3.
Insert(L, x, k)
Luciano Bononi
2010-2011
21
MaxSize-1
EXAMPLE:
Luciano Bononi
2010-2011
22
11
use two additional arrays first and last to index into this one
5
first[1]
last[1]
first[2]
first[3]
last[2]
last[3]
2010-2011
23
PRO:
simple to use
CONS:
2010-2011
24
12
In this case, the size of the array depends on its load factor
(that is how many elements are in the array)
2010-2011
25
Insert() operation
MaxSize * 2
2.
3.
Delete() operation
2.
MaxSize / 2
2010-2011
26
13
What are the PRO and CONS of the dynamic array data
structure?
Luciano Bononi
2010-2011
27
Linked list
A
head: *node
node := NULL
}
Each
1.
2.
Data: integer
Head
Next: *node
node
}
Data
Next
Data
Luciano Bononi
Next
Data
Next
2010-2011
28
14
Integer x;
x)
Function Add_item_to_end(L,
Add_item_to_end
2.
{
new node n
n.Data = x
n.Next = NULL
If (L.Head == NULL) {
L.Head = *n
}
3.
4.
Else {
pointer = L.Head
while (pointer <> NULL) {
Prev_pointer = pointer
pointer = pointer.Next
Head
}
Prev_pointer.Next
Prev_pointer.Next = *n
Data
Next
NULL
}
}
Luciano Bononi
2010-2011
29
List L;
Integer x;
Function Add_item_to_front
Add_item_to_front (L, x)
1.
2.
3.
4.
{
new node n
n.Data = x
n.Next = L.Head
L.Head = *n
}
Data
Next
NULL
Luciano Bononi
2010-2011
30
15
2.
3.
4.
Head
Data
Data
Next
Next
Luciano Bononi
2010-2011
31
Head
27
Next
3
Luciano Bononi
Next
99
Next
NULL
2010-2011
32
16
L = list
x = value to find
pointer = L.Head
in the list
return value =
if (pointer.Data = x) then
return(pointer)
return
pointer = pointer.Next
}
return(NULL)
return
}
2010-2011
33
Head
Data
Next
Data
Next
Data
Next
NULL
IMPORTANT: the deletion of the first and the last element are
special cases that have to be managed very carefully
Luciano Bononi
2010-2011
34
17
Head
27
Next
Next
99
Next
2010-2011
35
2.
3.
Prev
Data
Luciano Bononi
Next
2010-2011
36
18
Head
Prev Data
Next
Prev Data
Next
Prev Data
Next
Tail
Luciano Bononi
2010-2011
37
References
http://www.cs.rutgers.edu/~vchinni/dsa/
http://www.cs.aau.dk/~luhua/courses/ad07/
http://www.cs.aau.dk/~simas/ad01/index.html
http://140.113.241.130/course/2006_introduction_to_algorithm
s/courseindex.htm
Luciano Bononi
2010-2011
38
19
Luciano Bononi
<bononi@cs.unibo.it>
http://www.cs.unibo.it/~bononi/
20