Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Data Structures – Unit 04Bucharest University of Economic Studies
Stacks and Queues
Agenda
•Definition
•Graphical representation
•Internal interpretation
•Characteristics
•Operations
•Implementations
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
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;
} ;
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
Stack – graphical representation
Stack mechanism
K
C
A
T
S
push pop
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
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
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
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
Function call simulation
initialization
.…
BP:0x0018FA1Ch
SP:0x0018FA18h
push
b: 8
.…
BP:0x0018FA1Ch
SP:0x0018FA18h
SP:0x0018FA14h
Function call simulation
push a
a: 3
b: 8
.…
BP:0x0018FA1Ch
SP:0x0018FA18h
SP:0x0018FA14h
SP:0x0018FA10h
Function call simulation
push IP
IP: 0x00f11464h
a: 3
b: 8
.…
SP:0x0018FA0Ch
Function call simulation
BP:0x0018FA1Ch
SP:0x0018FA18h
SP:0x0018FA14h
SP:0x0018FA10h
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
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
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
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
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
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
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
PUSH Pseudocode
1. memory allocation (newNode);
2. newNode initialization;
3. if (stack_full)
stack overflow;
else
newNode >next = empty(head)? NULL: head;
head = newNode;
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;
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
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 ;
} ;
Implementation model
FIFO – Fifo In First Out
5 2 7 1
backfront
Queue
Queue – Implementation
E U E U Qput get
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;
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;
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
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
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
Initial state (back < front && empty == true):
back front
Queue – Circular array
Enqueue (2):
2
back
front
Queue – Circular array
Enqueue (4):
2 4
backfront
Queue – Circular array
Enqueue (1):
1 2 4
back front
Queue – Circular array
Queue – Circular arrayEnqueue (3):
1 3 2 4
back front
Queue – Circular arrayDequeue => return 2:
1 3 2 4
back front
Queue – Circular arrayDequeue => return 4:
1 3 2 4
backfront
Queue – Circular arrayDequeue => return 1:
1 3 2 4
back
front
Queue – Circular arrayDequeue => return 3:
1 3 2 4
back front
Palindrome
Able was I ere I saw Elba
Ring buffer
•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
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
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
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
Infix – Postfix – Prefix notations
Operators hierarchy level
4321
( [ { ) ] } + - * /
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
•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
•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
Infix to Postfix
2*(4+3)+9/3
Stack:
Output:2
Infix to Postfix
2*(4+3)+9/3
Stack:*
Output:2
Infix to Postfix
2*(4+3)+9/3
Stack:*(
Output:2
Infix to Postfix
2*(4+3)+9/3
Stack:*(
Output:24
Infix to Postfix
2*(4+3)+9/3
Stack:*(+
Output:24
Infix to Postfix
2*(4+3)+9/3
Stack:*(+
Output:243
Infix to Postfix
2*(4+3)+9/3
Stack:*
Output:243+
Infix to Postfix
2*(4+3)+9/3
Stack:+
Output:243+*
Infix to Postfix
2*(4+3)+9/3
Stack:+
Output:243+*9
Infix to Postfix
2*(4+3)+9/3
Stack:+/
Output:243+*9
Infix to Postfix
2*(4+3)+9/3
Stack:+/
Output:243+*93
Infix to Postfix
2*(4+3)+9/3
Stack:
Output:243+*93/+
•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
Infix->Postifix->Evaluation
E =5*3+2-(3*2+1)
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