



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German)Martensstraße 3,
91058 Erlangen
Data Structures
and Algorithms
Overview on simple data structuresfor representing dynamic sets of data records ^ Main operations on these data structures are^ ^
Insertion
and^ deletion
of an element
^ searching
for an element ^ finding the
minimum
or^ maximum
element
^ finding the
successor
or the^
predecessor
of an element
^ And similar operations … These data structures are often implementedusing^ dynamically allocated objects
and^ pointers
Data Structures
and Algorithms
Stack ^ A^ stack
implements the LIFO (last-in, first-out) policy ^ like a stack of plates, where you can either placean extra plate at the top or remove the topmost plate For a stack, ^ the^ insert^ operation is called
Push
^ and the
delete
operation is called
Pop
Data Structures
and Algorithms
Where are Stacks used? ^ A^ call stack
that is used for the proper execution of a computer program with subroutine or function calls Analysis of
context free languages
(e.g. properly nested brackets)
^ Properly nested: (()(()())), Wrongly nested: (()((()) Reversed Polish notation of terms ^ Compute 2 + 3*
⇨^ 2 Push 3 Push 5 * +
Data Structures
and Algorithms
Typical Implementation of a Stack ^ A typical implementation of a stack of size nis based on an
array^ S[1…n]
Top
Push^
Pop^4321
Data Structures
and Algorithms
Stack ^ If top(S) = 0 the stack is empty
⇨ no element can be popped
^ If top(S) = n the stack is full
⇨ no further element can be pushed
Data Structures
and Algorithms
7 6 5 4 3 2 1
7 6 5 4 3 2 1
7 6 5 4 3 2 1
7 6 5 4 3 2 1
Top[S]=
Top=
Top=
Top=
3 3
3 23 3 23
(^53) 17 push(S,17)
pop (S)^
17 pop (S)^
3 pop (S)^
23 push(S,5)
pop (S)^
5 pop (S)^
3 pop (S)
Error:underflow
Data Structures
and Algorithms
NumElements
return
top[S]
Data Structures
and Algorithms
Push(S,x)if^ Stack_Full(S)then^ error "overflow" else^ top[S]
:=^ top[S]+ S[top[S]]
:=^ x
Pop(S)if^ Stack_Empty(S)then^ error "underflow" else^ top[S]
:=^ top[S]- return
S[top[S]+1]
This pseudo code containserror handling functionality
Data Structures
and Algorithms
Pseudo Code for Stack Operations ^ (Asymptotic) Runtime^ ^
NumElements
: number of operations independent of size n of stack ⇨ constant ⇨ O(1) Stack_Empty
and^ Stack_Full
:
number of operations independent of size n of stack ⇨ constant ⇨ O(1) Push^ and
Pop : number of operations independent of size n of stack ⇨ constant
⇨ O(1)
Data Structures
and Algorithms
Data Structures
and Algorithms
Typical Implementation of a Queue ^ A typical implementation of a queue consisting of at most n-1elements is based on an
array^ Q[1 … n]
^ Its attribute
head(Q)
points to the head of the queue.
^ Its attribute
tail(Q)
points to the position
Data Structures
and Algorithms
Elementary Data Structures^ Q
1 2
3 4
5 6
7 8
9 10 head(Q)^
tail(Q)
Example (2) ^ Insert one more element (5.) ^ And again: Insert one more element (6.)
Q
1 2
3 4
5 6
7 8
9 10 head(Q)
tail(Q)
Q
1 2
3 4
5 6
7 8
9 10 head(Q) tail(Q)
Data Structures
and Algorithms
If tail > head:
NumElements(Q) = tail - head ^ If tail < head:
NumElements(Q) = tail – head + n ^ If tail = head:
NumElements(Q) = 0 ^ Initially: head[Q] = tail[Q] = 1 Position of elements in queue ^ The x. element of a queue Q (
≤^ x^ ≤^ NumElements(Q)
is mapped to array positionhead(Q) + (x - 1)
if x
≤^ n – head +1 (no wrap around)
head(Q) + (x - 1) - n
if x > n – head +1 (wrap around)