VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
Lecture4a dynamic data_structure
1. Lecture 4
Dynamic Data Structure Part 1
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 1
2. Content Lecture 4 Dynamic Data
Part 1
Pointer
Linear Lists
Linked List
Circular List
Doubly-Linked List
Stack
Queues
Sorted List
Linked Lists vs. Dynamic Arrays
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 2
3. Pointers
Pointers are an essential instrument in dynamical data
structures
But the use of pointers is not trivial
That is because of:
//important
Technics working with pointer are hard to read
The chances for errors are higher (especially for
beginners)
Undefined pointers are causing the most program
crashes
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 3
4. Pointers
Address space
When a program is executed the data are placed in the
memory
You can see the memory as an array which elements are
memory cells
The index area of this array is called address space
The single memory cell is in general one byte (8 bits)
Each active program has its own virtual address space
This space consists of a heap and a stack
Local variable or constants are stored in the stack
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 4
5. Pointers
Dynamical Data/Heap
If you need more memory during run time the memory has to
be allocated in the free parts of the address space (heap)
In opposite to global variables the addresses of the memory
locations are not constant
Therefore to address this memory you need so called
pointers
Pointers are variables whose values refers directly to (point
to) another value by using its address
This address refers to a memory location in which the data are
written
Therefore a pointer points to a memory address
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 5
6. Pointers
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 6
Storage for dynamical data
created or initialized at runtime
Storage for local variables
and parameters
declared and initialized
before runtime
8. Pointers
More than one pointer can refer to the same memory
location
In program language pointers are often used to realize a
call-by-reference function call, also for lists, strings, lookup
tables, control tables and trees
You can still change the data behind the pointer
If a pointer references to a location in memory to obtain
the value at this location this is called dereferencing
To release the memory you have to deallocate the memory
In some program language the deallocation of the memory
is done by a so called Garbage Collector (for example Java)
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 8
9. Pointers
In other language the programmer has to take care of the
deallocation by himself (for example C/C++)
That leads to following problems:
The deallocation can be forgotten (memory leaks)
It is not always trivial to calculate what and when
something has to be deallocated
Pointers are still used even when the deallocation has
taken place (dangling reference)
All this problems can lead to a mostly unexpected behavior
of the program execution
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 9
10. Linear Lists
To one of the simplest data structure belongs the Linear
List
A number of elements ai are represented in an order form:
a1 a2 … ai-1 ai ai+1 … an
Typical operations are:
Putting a new element at the beginning or at the end of
the list
Deleting an element from the list
Getting an element at position i (especially when i=1 or
i=n)
Getting the next or previous element of an element ai
(therefore ai+1 or ai-1)
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 10
11. Linear Lists
Examples for lists
Orders in a shop: Each element is equal to an order. The
order tells which article has to be send to a costumer. Each
new order is put at the end of the list. After finishing the
order the element is deleted in the list.
Moves: Each element is equal to a move in a game. A new
move is put at the end of the list. You can restore an old score
by removing the last element.
Timetable: Each element is equal to a new appointment or
short information. All elements are sorted by time. New
entries are therefore put in the list according to their time.
Expired entries are deleted from the list.
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 11
12. Linear Lists
An implementation of a list data structure may require some
of the following operations:
An operation to creating an empty list (init)
An operation to test whether or not a list is empty
(isempty)
An operation for adding an entity to a list at the beginning
An operation for appending an entity to a list
An operation for receiving the first component element
(head) of a list or the last element
An operation for referring to the list consisting of all the
components of a list except for its first (this is called the
"tail" of the list)
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 12
13. Linked List
A Linked List is a data structure that consists of a sequence
of data records such that in each record there is a field that
contains a reference (a link) to the next record in the
sequence
Each record of a Linked List is often called an element or
node
The field of each node that contains the address of the next
node is usually called the next link or next pointer
The remaining fields are known as the data, information
or value
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 13
14. Linked List
struct Node
{
data; //The data/value being stored in the node
Node next; //reference to the next node, null for last
//node
};
struct List
{
Node first; //Points to first node of list; null for empty
//list
};
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 14
15. Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 15
5 14 44 67 X
node/element next link/pointer
data/value null/end of the list
list
first link/pointer
16. Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 16
5 14 44 67 X
31
5 14 44 67 X
31
Insert a new node
5 14 44 67 X
31
newNode.next
newNode.next = node.next
node.next = newNode
newNode
node
node.next
17. Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 17
5 14 44
3list newNode
5 14 44
3list
newNode.next = list.first;
Special case if you insert a new
node at the beginning
5 14 44
3list list.first = newNode;
18. Linked List
Removing a node
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 18
5 14 44 67
5 14 44 67
node
obsoleteNode
node.next
5 14 44 67
node.next node.next.next
destroy obsoleteNode
node.next = node.next.next;
19. Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1
19
5 14 44
list
5 14 44
list
5 14 44
list
obsoleteNode = list.first
list.first = list.first.next
list.first.next
destroy obsoleteNode
Removing a node in front of the list
20. Circular List
In the most cases the last node of a list contains a null
value
Means that there is no next node in the list
In some cases a pointer to the first node of the list is made
This is called a Circular List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 20
5 14 44 67 struct List
{
Node first;
//Points to first node of list;
Node last;
//Points to first node of list;
}
21. Circular List
Insert an element at the
beginning
newNode.next = list.first
list.first = newNode
list.last = newNode;
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 21
5 14 44 67
1
5 14 44 67
1
22. Doubly-Linked List
A Doubly-Linked List is a Linked List that contains a
number of elements, each having two special fields
referencing to the next and previous element in the list
You can view it as two Linked Lists formed from the same
data items, in two opposite directions
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 22
5 14 44 67 XX
23. Doubly-Linked List
struct Node {
data; // The data being stored in the node
Node next; // A reference to the next node; null for last node
Node prev; // A reference to the previous node; null for first
// node
};
struct List {
Node firstNode; // points to first node of list;
Node lastNode; // points to last node of list;
};
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 23
24. Doubly-Linked List
Iterating through a Doubly Linked List can be done in
either direction
In fact, direction can change many times, if desired
Forwards
node = list.firstNode
while (node != null) {
//do something with the
//data
node = node.next;
}
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 24
Backwards
node = list.lastNode
while (node != null) {
//do something with the
//data
node = node.prev;
}
25. Doubly-Linked List
Inserting a node
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 25
5 14 44 67 XX
31
5 14 44 67 XX
31
newNode
newNode.prev = node;
newNode.next = node.next;
node
26. Doubly-Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 26
5 14 44 67 XX
31
5 14 44 67 XX
31
node.next = newNode;
node.next.prev = newNode
27. Doubly-Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 27
Removing a node
5 14 44 67 XX
5 14 44 67 XX
5 14 67 XX
destroy node;
obsoleteNode
obsoleteNode.prev.next=obsoleteNode.next;
44
obsoleteNode.next.prev=obsoleteNode.prev;
28. Stack
A Stack is also a Linear List with the difference that only at
one end elements could be added or removed
In a Stack you are mostly interested in the element on the
top
Elements below are only appearing again if all elements on
the top of it are removed
In computer science, a stack is a Last In, First Out (LIFO)
abstract data type and data structure
Examples for Stacks: Game Tic-Tac-Toe
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 28
29. Stack
In the most program language the implementation of Stack
can be done with arrays
The C++ Standard Template Library provides a Stack
Template Class which is restricted to only push/pop
operations
Java's library contains a Stack class that is a specialization
of the class Vector
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 29
30. Stack
An implementation of a Stack may require some of the
following operations:
An operation to creating an empty Stack (init)
An operation to add an element on the top of the Stack
(push)
An operation to remove an element on the top of the
Stack (pop)
An operation to receive the current element on the top
of the Stack (top)
An operation to receive the length of the stack; the
number of elements in the Stack (length)
An operation showing that the maximal capacity of the
Stack is reached (full)
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 30
32. Stack
Example
X = {b, d, f, h, j, l, n, p} a Stack
What is:
pop()
length()
top()
push(r)
length()
top()
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 32
33. Stack
Solution
X = {b, d, f, h, j, l, n, p} a Stack
What is:
pop() X = {b, d, f, h, j, l, n}
length() = 7
top() = n
push(r) X = {b, d, f, h, j, l, n, r}
length() = 8
top() = r
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 33
p
n
l
j
h
f
d
b
r
n
l
j
h
f
d
b
n
l
j
h
f
d
b
34. Queues
A Queue or FIFO (First-In-First-Out) is a Linear List
where at one end elements are added and on the other end
elements are removed
The inner elements are not considered
The elements will be processed in exactly the same order in
which they are original placed
The C++ Standard Template Library provides a Queue
Template Class.
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 34
35. Queues
The following operations exists in general for queues
init() initialize an empty queue
isempty() true if the queue is empty, otherwise false
pop() removes the item at the front of the queue
push() insert an item at the back of the queue
size() return the number of elements in the
queue
front() returns a reference to the value at the
front of a non-empty queue
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 35
36. Queues
Example
Y = {1, 3, 5, 7, 11, 13} a Queue
isempty() = false
pop() = 13 Y = {1, 3, 5, 7, 11}
push(17) Y = {17, 1, 3, 5, 7, 11}
size() = 6
front() = 11
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 36
1 3 5 7 11 13
17 1 3 5 7 11
1 3 5 7 11
37. Queues
Example
Y = {g, f, e, d, c, b, a} a Queue
What is
pop()
size()
front()
push(h)
isempty()
size()
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 37
g f e d c b a
38. Queues
Solution
Y = {g, f, e, d, c, b, a} a Queue
What is
pop() = a Y = {g, f, e, d, c, b}
size() = 6
front() = b
push(h) Y = {h, g, f, e, d, c, b}
isempty() = false
size() = 7
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 38
g f e d c b a
g f e d c b
h g f e d c b
39. Sorted List
In a Sorted List each element has a key
For this key a complete order relation ≤ exists with:
a ≤ a (reflexivity)
a ≤ b and b ≤ a a = b (anti symmetry)
a ≤ b and b ≤ c a ≤ c (transitivity)
With this order relation you can decided if an element is
smaller or bigger than another one
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 39
40. Sorted List
The following operations exist for Sorted List:
init() initialize an empty Sorted List
insert() insert an element in the list so that the
is still sorted
removefirst() removes the first element in the list (the
element with the lowest key)
getfirst() get the first element from the list
search(key) search for an element with given key
delete(key) delete an element with given key
length() the size of the Sorted List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 40
41. Sorted List
Insert in a Sorted List
Different from other list variants the value of the new element
decided in which place of the list it will be insert
If the list is empty or if the value of the first element is greater
than the value of the new element, the new element is to be
inserted at beginning of the list
Otherwise you have to pass through the list until you find a
value greater than the value of the new element
For this you use a pointer passing from element to element
and comparing the values
Traversing is also necessary if you print out an element or if
you remove an element of the list
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 41