data structures unit 04 -

66
Data Structures – Unit 04 Bucharest University of Economic Studies Stacks and Queues

Upload: others

Post on 19-Apr-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures Unit 04 -

Data Structures – Unit 04Bucharest University of Economic Studies

Stacks and Queues

Page 2: Data Structures Unit 04 -

Agenda

•Definition

•Graphical representation

•Internal interpretation

•Characteristics

•Operations

•Implementations

Page 3: Data Structures Unit 04 -

Stack

I t i s a l i n e a r s t r u c t u r e i n w h i c h t h e t w o b a s i c

o p e r a t i o n s , i n s e r t i o n a n d d e l e t i o n a r e m a d e

o n a L I F O b a s e d r u l e . T h e s e o p e r a t i o n s a r e

m a d e i n a s i n g l e p a r t o f t h e s t r u c t u r e c a l l e d

t h e h e a d o f t h e s t a c k , o r t h e t o p .

STACKpop

push

Page 4: Data Structures Unit 04 -

Stack•A logical structure implemented with different type of data structures:

linked lists or arrays;

•A homogenous structure with elements of the same type;

•Basic operations:

•Adding an element, called push operation

•Removing an element, called pop operation

•Looking at the top element, called peek operation

struct Stack{

int info;

Stack * next;

} ;

Page 5: Data Structures Unit 04 -

Stack as an ADT

2

4

1

3

6

top

The implementation rule is

LIFO – Last In First Out

The pop operation from a NULL stack

is considered an error

The push operation into a stack is

considered a limitation

The element from the top of a stack is

the only accessible element

Page 6: Data Structures Unit 04 -

Stack – graphical representation

Stack mechanism

K

C

A

T

S

push pop

Page 7: Data Structures Unit 04 -

Stacks with Linked Lists

2 1005

1002 1003

5 1328

1005 1006

8 2500

1328 1329

34 NULL

2500 2501

2 NULL

3241 3242

top of the stack

new node

Page 8: Data Structures Unit 04 -

Stacks with Linked Lists

2 1005

1002 1003

5 1328

1005 1006

8 2500

1328 1329

34 NULL

2500 2501

2 1002

3241 3242

top of the stack

Page 9: Data Structures Unit 04 -

Stacks with Linked Lists

2 1005

1002 1003

5 1328

1005 1006

8 2500

1328 1329

34 NULL

2500 2501

2 1002

3241 3242

temporary node

top of the stack

Page 10: Data Structures Unit 04 -

Function call simulation

int suma(int x, int y) {

int z = 0, r = 2;

z = x + y;

return z;

}

void main(){

int a = suma(3,8);

}

0x00f11464h

Page 11: Data Structures Unit 04 -

Function call simulation

initialization

.…

BP:0x0018FA1Ch

SP:0x0018FA18h

Page 12: Data Structures Unit 04 -

push

b: 8

.…

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

Function call simulation

Page 13: Data Structures Unit 04 -

push a

a: 3

b: 8

.…

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

Function call simulation

Page 14: Data Structures Unit 04 -

push IP

IP: 0x00f11464h

a: 3

b: 8

.…

SP:0x0018FA0Ch

Function call simulation

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

Page 15: Data Structures Unit 04 -

push BP

BP: 0x0018FA1Ch

IP: 0x00f11464h

a: 3

b: 8

.…

SP:0x0018FA08h

Function call simulation

SP:0x0018FA0Ch

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

Page 16: Data Structures Unit 04 -

mov SP, BP

BP: 0x0018FA1Ch

IP: 0x00f11464h

a: 3

b: 8

.…

BP:0x0018FA08h

Function call simulation

SP:0x0018FA08h

SP:0x0018FA0Ch

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

Page 17: Data Structures Unit 04 -

sub esp,8

r: 2

z: 0

BP: 0x0018FA1Ch

IP: 0x00f11464h

a: 3

b: 8

.…

BP:0x0018FA08h

Function call simulation

SP:0x0018FA08h

SP:0x0018FA0Ch

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

SP:0x0018FA00h

Page 18: Data Structures Unit 04 -

mov BP, SP

r: 2

z: 0

BP: 0x0018FA1Ch

IP: 0x00f11464h

a: 3

b: 8

.…

BP:0x0018FA08h

Function call simulation

SP:0x0018FA08h

SP:0x0018FA0Ch

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

SP:0x0018FA00h

Page 19: Data Structures Unit 04 -

pop BP

r: 2

z: 0

BP: 0x0018FA1Ch

IP: 0x00f11464h

a: 3

b: 8

.…

BP:0x0018FA08h

Function call simulation

SP:0x0018FA08h

SP:0x0018FA0Ch

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

SP:0x0018FA00h

pop BP

Page 20: Data Structures Unit 04 -

return

r: 2

z: 0

BP: 0x0018FA1Ch

IP: 0x00f11464h

a: 3

b: 8

.…

BP:0x0018FA08h

Function call simulation

SP:0x0018FA08h

SP:0x0018FA0Ch

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

SP:0x0018FA00h

pop BP

return

Page 21: Data Structures Unit 04 -

r: 2

z: 0

BP: 0x0018FA1Ch

IP: 0x00f11464h

a: 3

b: 8

.…

BP:0x0018FA08h

Function call simulation

SP:0x0018FA08h

SP:0x0018FA0Ch

BP:0x0018FA1Ch

SP:0x0018FA18h

SP:0x0018FA14h

SP:0x0018FA10h

SP:0x0018FA00h

pop BP

return

Page 22: Data Structures Unit 04 -

PUSH Pseudocode

1. memory allocation (newNode);

2. newNode initialization;

3. if (stack_full)

stack overflow;

else

newNode >next = empty(head)? NULL: head;

head = newNode;

Page 23: Data Structures Unit 04 -

POP Pseudocode

1. declaring a temporary node (tmpNode);

2. if (stack_empty)

stack underflow;

else

value = head->info;

tmpNode = head;

head = head–>next;

free(tmpNode);

return value;

Page 24: Data Structures Unit 04 -

QUEUESIt is a linear structure in which the two basicoperations, insertion anddeletion

are made on a FIFO based model. The insertion is made on the tail section

andthedeletion ismadeonthefrontsectionofthequeue.

Queue

enqueue

dequeue put

get

Page 25: Data Structures Unit 04 -

Queue•A logical structure implemented with different type of data structures: linked

lists or arrays;

•A homogenous structure with elements of the same type;

•Basic operations:

•Adding an element, called the enqueuer or put operation

•Removing an element, called the dequeuer or get operation

struct Node{

int info;

Node * next;

} ;

struct Queue{

Node* head;

Node* tai l ;

} ;

Page 26: Data Structures Unit 04 -

Implementation model

FIFO – Fifo In First Out

5 2 7 1

backfront

Queue

Page 27: Data Structures Unit 04 -

Queue – Implementation

E U E U Qput get

Page 28: Data Structures Unit 04 -

PUT Pseudocode

1. memory allocation for a new node (newNode);

2. newNode initialization;

3. if (full queue)

queue overflow;

else

if (empty queue)

head = tail = newNode ;

else

tail–>next = newNode;

Page 29: Data Structures Unit 04 -

GET Pseudocode

1. declaring a temporary node (tmpNode);

2. if (empty queue)

queue underflow;

else

value = head–>info;

tmpNode = head;

head = head–>next;

free(tmpNode);

return value;

Page 30: Data Structures Unit 04 -

Queues with Linked Lists

2 1005

1002 1003

5 1328

1005 1006

8 2500

1328 1329

34 NULL

2500 2501

2 NULL

3241 3242

head of the queue

tail of the queue

Page 31: Data Structures Unit 04 -

Queues with Linked Lists

2 1005

1002 1003

5 1328

1005 1006

8 2500

1328 1329

34 3241

2500 2501

2 NULL

3241 3242

head of the queue

tail of the queue

Page 32: Data Structures Unit 04 -

Queues with Linked Lists

2 1005

1002 1003

5 1328

1005 1006

8 2500

1328 1329

34 3241

2500 2501

2 NULL

3241 3242

temporary node

tail of the queue

head of the queue

Page 33: Data Structures Unit 04 -

Initial state (back < front && empty == true):

back front

Queue – Circular array

Page 34: Data Structures Unit 04 -

Enqueue (2):

2

back

front

Queue – Circular array

Page 35: Data Structures Unit 04 -

Enqueue (4):

2 4

backfront

Queue – Circular array

Page 36: Data Structures Unit 04 -

Enqueue (1):

1 2 4

back front

Queue – Circular array

Page 37: Data Structures Unit 04 -

Queue – Circular arrayEnqueue (3):

1 3 2 4

back front

Page 38: Data Structures Unit 04 -

Queue – Circular arrayDequeue => return 2:

1 3 2 4

back front

Page 39: Data Structures Unit 04 -

Queue – Circular arrayDequeue => return 4:

1 3 2 4

backfront

Page 40: Data Structures Unit 04 -

Queue – Circular arrayDequeue => return 1:

1 3 2 4

back

front

Page 41: Data Structures Unit 04 -

Queue – Circular arrayDequeue => return 3:

1 3 2 4

back front

Page 42: Data Structures Unit 04 -

Palindrome

Able was I ere I saw Elba

Page 43: Data Structures Unit 04 -

Ring buffer

Page 44: Data Structures Unit 04 -

•Evaluating mathematical expressions by using the stack data

structure: reordering the elements so that the expression can be

evaluated easily by one single crossing over the entire expression;

•The Polish notation invented by Jan Lukasiewicz also called the prefix

expression;

•The Reverse Polish notation is called postfix notation.

Infix – Postfix – Prefix notations

Page 45: Data Structures Unit 04 -

Evaluating mathematical expressions:

•Prefix or Polish notation: the operators are written

before the operands

•Postfix or revers Polish notation: the operators are

placed after the operands

Infix – Postfix – Prefix notations

Page 46: Data Structures Unit 04 -

The postfix or the reverse Polish notation is easily interpreted than

the infix, normal mathematical expressions:

•the order of operations is straight forward;

•the parentheses are not necessary;

•the evaluation are automatically made by the use of a stack data

structure;

Infix – Postfix – Prefix notations

Page 47: Data Structures Unit 04 -

The algorithm for casting an infix expression to a postfix notation is

called Dijkstra Shunting-Yard algorithm:

•using a stack for storing the operators in order to be transferred into

the postfix notation;

•each operator has an adequate hierarchy level as depicted in the

next table;

Infix – Postfix – Prefix notations

Page 48: Data Structures Unit 04 -

Infix – Postfix – Prefix notations

Operators hierarchy level

4321

( [ { ) ] } + - * /

Page 49: Data Structures Unit 04 -

Mathematical

expression

(the infix notation)

Polish notation

(prefix expression)

Reverse Polish

notation

(postfix expression)

4 + 5 + 4 5 4 5 +

4 + 5 * 5 + 4 * 5 5 4 5 5 * +

4 * 2 + 3 + * 4 2 3 4 2 * 3 +

4 + 2 + 3 + + 4 2 3 4 2 + 3 +

4 * (2 + 3) * 4 + 2 3 4 2 3 + *

Infix – Postfix – Prefix notations

Page 50: Data Structures Unit 04 -

•Scan the entire infix expression from left to right for tokens

(operators, operands and parentheses)

•For each token, test whether:

•If token is an operator:

•All operators from the top of the stack that are higher or

equal precedence than the incoming one are popped out

from the stack and appended to the postfix expression;

after this operation the new token is pushed on the stack;

Dijkstra Shunting-Yard algorithm

Page 51: Data Structures Unit 04 -

•If token is an operand, append it to the postfix expression;

•If token is a left parenthesis, push it on the stack;

•If token is a right parenthesis:

•Pop all the operators from the stack and append them to the postfix

expression, till the matching left parenthesis is found, which is

popped out without being added to the result;

•When all tokens of the infix expression have been scanned, pop all the

elements from the stack and append them to the postfix expression;

Dijkstra Shunting-Yard algorithm

Page 52: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:

Output:2

Page 53: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:*

Output:2

Page 54: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:*(

Output:2

Page 55: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:*(

Output:24

Page 56: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:*(+

Output:24

Page 57: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:*(+

Output:243

Page 58: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:*

Output:243+

Page 59: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:+

Output:243+*

Page 60: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:+

Output:243+*9

Page 61: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:+/

Output:243+*9

Page 62: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:+/

Output:243+*93

Page 63: Data Structures Unit 04 -

Infix to Postfix

2*(4+3)+9/3

Stack:

Output:243+*93/+

Page 64: Data Structures Unit 04 -

•Scan the entire postfix expression from left to right for tokens (operands and

operators):

•For each token, test whether:

•If token is an operand is pushed on the stack;

•If token is an operator:

•Pop the left element from the stack into a y variable;

•Pop the right element from the stack into a x variable;

•The operation between x operator y is computed;

•The result is pushed on the stack;

•The last value standing on the stack is the result of the expression.

Postfix evaluation algorithm

Page 65: Data Structures Unit 04 -

Infix->Postifix->Evaluation

E =5*3+2-(3*2+1)

Page 66: Data Structures Unit 04 -

References

•Ion IVAN, Marius POPA, Paul POCATILU, coordonatori – Structuri de date, Editura ASE, Bucuresti,

2008, ISBN 978-606-505-031-0

•Ion SMEUREANU – Programarea in limbajul C/C++, Editura CISON, 2001

•Michael MAIN – Data Structures & Other Objects Using Java, Editura Pearson, 2012, ISBN:

9780132911504

•http://www.acs.ase.ro

•http://www.itcsolutions.eu