chapter 3 stacks and queues · 2016. 10. 20. · ncue csie wireless communications and networking...

36
NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 3 Stacks And Queues 1

Upload: others

Post on 03-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

CHAPTER 3

Stacks And Queues

1

Page 2: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Stack: a Last-In-First-Out (LIFO/FILO) list

2

Stack

Push A

A Top

Push B

B

Push C

C

Pop C

Page 3: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

(a) (b)

System stack after function call a1

system stack before a1 is invoked system stack after a1 is invoked

An application of stack: stack

frame of function call

Old frame pointer

Return address stack frame of invoking function

fp

main

Old frame pointer

Return address

Local variables

Old frame pointer

Return address

fp

main

al

Page 4: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Abstract Data Type for stack

structure Stack is

objects: a finite ordered list with zero or more elements.

functions:

for all stack Stack, item element, max_stack_size

positive integer

Stack CreateS(max_stack_size) ::=

create an empty stack whose maximum size is

max_stack_size

Boolean IsFull(stack, max_stack_size) ::=

if (number of elements in stack == max_stack_size)

return TRUE

else return FALSE

Stack Push(stack, item) ::=

if (IsFull(stack)) stack_full

else insert item into top of stack and return

4

Page 5: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Boolean IsEmpty(stack) ::=

if(stack == CreateS(max_stack_size))

return TRUE

else return FALSE

Element Pop(stack) ::=

if(IsEmpty(stack)) return

else remove and return

the item on

the top of the stack.

5

Page 6: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Implementation : using Array

Stack CreateS(max_stack_size) ::=

#define MAX_STACK_SIZE [1..n] /* maximum stack size */

typedef struct {

int key;

/* other fields */

} element;

element stack[MAX_STACK_SIZE];

int top = 0;

Boolean IsEmpty(Stack) ::= top== 0;

Boolean IsFull(Stack) ::= top == MAX_STACK_SIZE;

6

Page 7: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Add to a stack (Push)

void push(int *top, element item)

{

/* add an item to the global stack */

if (*top == MAX_STACK_SIZE) {

stack_full( );

return;

}

stack[++*top] = item;

}

top=top+1

*top=item

7

Page 8: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Delete from a stack (Pop)

element pop(int *top)

{

/* return the top element from the

stack */

if (*top == 0)

return stack_empty( ); /*

returns and error key */

return stack[(*top)--];

} *top=item

top=top-1 8

Page 9: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Queue: a First-In-First-Out (FIFO/LILO) list

(1) Add – Rear

(2) Delete – Front

9

Queue

A Front Rear

Insert A B

B C

C D

D

Delete

Page 10: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

front rear

Different Queue

(1) FIFO Queue

(2) Priority Queue

(3) Double-ended Queue

(4) Double-ended Priority Queue

ADD Delete

10

Page 11: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Application: Job scheduling

front rear Q[0] Q[1] Q[2] Q[3] Comment

-1 -1 Queue is empty

-1 0 J1 Jop 1 is added

-1 1 J1 J2 Jop 2 is added

-1 2 J1 J2 J3 Jop 3 is added

0 2 J2 J3 Jop 1 is deleted

1 2 J3 Jop 2 is deleted

11

Page 12: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Abstract Data Type of queue

structure Queue is

objects: a finite ordered list with zero or more elements.

functions:

for all queue Queue, item element,

max_ queue_ size positive integer

Queue CreateQ(max_queue_size) ::=

create an empty queue whose maximum size is

max_queue_size

Boolean IsFullQ(queue, max_queue_size) ::=

if(number of elements in queue==max_queue_size)

return TRUE

else return FALSE

Queue AddQ(queue, item) ::=

if (IsFullQ(queue)) queue_full

else insert item at rear of queue and return queue 12

Page 13: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Boolean IsEmptyQ(queue) ::=

if (queue ==CreateQ(max_queue_size))

return TRUE

else return FALSE

Element DeleteQ(queue) ::=

if (IsEmptyQ(queue)) return

else remove and return the item at front of queue.

13

Page 14: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Implementation : using Array

Queue CreateQ(max_queue_size) ::=

# define MAX_QUEUE_SIZE [1…n]

typedef struct {

int key;

/* other fields */

} element;

element queue[MAX_QUEUE_SIZE];

int rear = 0;

int front = 0;

14

Page 15: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Add to a queue

void addq(int *rear, element item)

{

/* add an item to the queue */

if (*rear == MAX_QUEUE_SIZE) {

queue_full( );

return;

}

queue [++*rear] = item;

}

15

Page 16: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Delete from a queue

element deleteq(int *front, int rear)

{

/* remove element at the front of the queue */

if ( *front == * rear)

return queue_empty( ); /* return an error

key */

else

return queue [++ *front];

}

problem: there may be available space when IsFullQ

is true, i.e., movement is required.

16

Page 17: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

EMPTY QUEUE

front = 0 front = 0

rear = 0 rear = 3

Implementation : regard an array as a

circular queue

17

Page 18: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Queue CreateQ(max_queue_size) ::=

# define MAX_QUEUE_SIZE [0…n-1]

typedef struct {

int key;

/* other fields */

} element;

element queue[MAX_QUEUE_SIZE];

int rear = 0;

int front = 0;

Create a circular queue

18

Page 19: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Add to a circular queue

void addq(int *front, int *rear, element item)

{

/* add an item to the queue */

*rear = (*rear +1) % MAX_QUEUE_SIZE;

if (*front == *rear) then queue_full( );

return;

}

queue[*rear] = item;

}

19

Page 20: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Delete from a circular queue

element deleteq(int *front, int *rear)

{

element item;

/* remove front element from the queue and put it

in item */

if (*front ==*rear) return queue_empty( );

/* queue_empty returns an error key */

else

*front = (*front+1) % MAX_QUEUE_SIZE;

return queue[*front];

}

20

Page 21: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

FULL QUEUE FULL QUEUE

front =0

rear = 5

front =4

rear =3

Problem: one space is left

when queue is full

21

Page 22: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Queue CreateQ(max_queue_size) ::=

# define MAX_QUEUE_SIZE [0…n-1]

typedef struct {

int key;

/* other fields */

} element;

element queue[MAX_QUEUE_SIZE];

int rear = 0;

int front = 0;

boolean tag =0

Create a circular queue

(with n spaces used)

22

Page 23: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

void addq(int front, int *rear, element item)

{

if (*rear=*front) and tag =1) then queue_full();

else

*rear = (*rear +1) % MAX_QUEUE_SIZE;

queue[*rear]=item

if (* front == *rear) tag=1;

}

Add to a circular queue

(with n spaces used)

23

Page 24: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

element deleteq(int* front, int rear)

{

element item;

if ((*front=*rear) and (tag=0)) then

queue_empty();

else

*front = (*front+1) %

MAX_QUEUE_SIZE;

return queue[*front];

if (*front=*rear) then tag=0;

}

Delete from a circular queue

(with n spaces used)

24

Page 25: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Evaluation of Expressions

X = a / b - c + d * e - a * c

a = 4, b = c = 2, d = e = 3

Interpretation 1:

((4/2)-2)+(3*3)-(4*2)=0 + 9-8=1

Interpretation 2:

(4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666…

How to generate the machine instructions corresponding

to a given expression?

precedence rule + associative rule 25

Page 26: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Token Operator Precedence Associativity

( )

[ ]

->

founction call

array element

struct or union member

17

left-to-right

-- ++ decreament,increament 16 left-to-right

-- ++

!

-

- +

& *

Sizeof

decreament,increament

logical not

one’s complement

unaryminus or plus

address or indirection

size (in bytes)

15

right-to-left

(type) type cast 14 right-to-left

*/% multiplicative 13 left-to-right

26

Page 27: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

+ - binary add or subtract 12 left-to-right

<< >> shift 11 left-to-right

> >=

< <=

relational 10 left-to-right

== != equality 9 left-to-right

& bitwise and 8 left-to-right

^ bitwise exclusive or 7 left-to-right

| bitwise or 6 left-to-right

&& logical and 5 left-to-right

[[ logical or 4 left-to-right

27

Page 28: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

1.The precedence column is taken from Harbison and Steele.

2.Postfix form

3.prefix form

?: conditional 3 right-to-left

= += -= /=

*= %= <<=

>>= &= [=

^=

assignment

2

right-to-left

, comma 1 left-to-right

28

Page 29: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

user compiler

Postfix: no parentheses, no precedence

Infix Postfix

2+3*4

a*b+5

(1+2)*7

a*b/c

(a/(b-c+d))*(e-a)*c

a/b-c+d*e-a*c

234*+

ab*5+

12+7*

ab*c/

abc-d+/ea-*c*

ab/c-de*ac*-

29

Page 30: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Infix to Postfix Conversion

(Intuitive Algorithm)

30

(1) Fully parenthesize expression

a / b - c + d * e - a * c -->

((((a / b) - c) + (d * e)) - a * c))

(2) All operators replace their corresponding right

parentheses.

((((a / b) - c) + (d * e)) - a * c))

(3) Delete all parentheses.

ab/c-de*+ac*-

two passes

Page 31: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

(1) evaluation

(2) transformation

Infix Prefix

a*b/c

a/b-c+d*e-a*c

a*(b+c)/d-g

/*abc

-+- /abc*de*ac

-/*a+bcdg

31

Page 32: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Please transform the following expression into postfix.

Ans:

32

Question:

GFEDCBA ))(^()(

GFCDEABPostfix ^:

Page 33: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Use array to implement the following operations for stack S

(1)PUSH an item into S.

(2)POP an item from S.

33

Question:

Page 34: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

Ans:

(1)void PUSH (item x, stack s)

{

if (top==n) then stackFull

else { Top=Top+1;

S[Top]=x;

}

}

34

Page 35: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

(2) item POP(stack S)

{

if (Top== ) then stockempty;

else {

X=S[Top];

Top=Top-1;

}

}

35

Page 36: CHAPTER 3 Stacks And Queues · 2016. 10. 20. · NCUE CSIE Wireless Communications and Networking Laboratory Abstract Data Type for stack structure Stack is objects: a finite ordered

NCUE CSIE Wireless Communications and Networking Laboratory

36

Reference

Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed

〝Fundamentals of Data Structures in C〞, W. H. Freeman & Co

Ltd, 1992.

Ellis Horowitz, Sartaj Sahni, and Dinesh Mehta

〝Fundamentals of Data Structures in C++〞 Silicon Pr, 2006

Richard F.Gilberg, Behrouz A. Forouzan,

〝Data Structures: A Pseudocode Approach with C〞, S

Baker & Taylor Books, 2004

Fred Buckley, and Marty Lewinter 〝A Friendly Introduction to Graph

Theory〞 Prentice Hall, 2002

〝資料結構-使用C語言〞蘇維雅譯,松崗,2004

〝資料結構-使用C語言〞 蔡明志編著,全華,2004

〝資料結構(含精選試題)〞洪逸編著,鼎茂,2005