Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Elementary Data Structures: Stacks and Linked Lists, Study notes of Data Structures and Algorithms

An introduction to elementary data structures, focusing on stacks and linked lists. Topics covered include the LIFO policy of stacks, their implementation and usage, where they are used, and their typical implementation. For linked lists, the document discusses their unsorted, doubly linked nature, typical examples of their use, and their implementation, including insertion, deletion, and searching.

What you will learn

  • Where are stacks typically used?
  • What is a linked list and how is it implemented?
  • What are the typical examples of using linked lists?
  • How is an element inserted or deleted from a linked list?
  • What is a stack and how does it implement the LIFO policy?

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

jacksonhh
jacksonhh 🇬🇧

4.2

(23)

251 documents

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German)
Martensstraße 3, 91058 Erlangen
Introduction to
Data Structures and Algorithms
Chapter: Elementary Data Structures(1)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Elementary Data Structures: Stacks and Linked Lists and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German)Martensstraße 3,

91058 Erlangen

Introduction toData Structures and Algorithms Chapter:

Elementary Data Structures(1)

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

Elementary Data Structures

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

Elementary Data Structures

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 * +

Elementary Data Structures

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]

Elementary Data Structures^ ⇨^ so it can hold at most n elements^ ^ top(S) is the index of the most recentlyinserted element^ ^ The stack consists of elementsS[1 … top(S)], where^ ^ S[1] is the element at the bottom of the stack,^ ^ and S[top(S)] is the element at the top.^ ^ The unused elements S[top(S)+1 … n]are not in the stack

S

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

Elementary Data Structures

Data Structures

and Algorithms

Elementary Data Structures

S:^

S:^

S:^

S:

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

Elementary Data Structures^ Pseudo Code for Stack Operations^ ^ Number of elements

NumElements

(S)

return

top[S]

Data Structures

and Algorithms

Elementary Data Structures^ Pseudo Code for Stack Operations^ ^ Pushing and Popping

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)

Elementary Data Structures

Data Structures

and Algorithms

Elementary Data Structures^ Where are Queues used?^ ^ In multi-tasking systems (communication, synchronization)^ ^ In communication systems (store-and-forward networks)^ ^ In servicing systems (queue in front of the servicing unit)^ ^ Queuing networks (performance evaluation of computer andcommunication networks)

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

Elementary Data Structures where a new element will be inserted into the queue(i.e. one position behind the last element of the queue).^ ^ The elements in the queue are in positionshead(Q), head(Q)+1, …, tail(Q)-1, where we wrap around the arrayboundary in the sense that Q[1] immediately follows Q[n]

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.)

1.^ 2.^

3.^ 4.

Q

1 2

3 4

5 6

7 8

9 10 head(Q)

tail(Q)

1.^ 2.^

3.^ 4.^

Q

1 2

3 4

5 6

7 8

9 10 head(Q) tail(Q)

1.^ 2.^

3.^ 4.^

Data Structures

and Algorithms

Elementary Data StructuresTypical Implementation of a Queue^ ^ Number of elements in queue^ ^

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)