Data Structurecontents.kocw.net/KOCW/document/2015/hanyang/parkjongil/...Division of Computer...

Post on 12-Mar-2020

0 views 0 download

transcript

Data Structure- Stack and Queue-

Hanyang University

Jong-Il Park

Division of Computer Science and Engineering, Hanyang University

STACK

Division of Computer Science and Engineering, Hanyang University

Stack ADT List that insertions and deletions can be performed at the

end of the list Operations

Push(X, S): insert X in the list S Pop(S): deletes the most recently inserted element from S

X3

2

1

Push(X, S) Pop(S) = X

Top(S)

stack SPush(1, S) Push(2, S) Push(3, S) Push(X, S)

Division of Computer Science and Engineering, Hanyang University

Stack ADT: linked list implementation

struct Node;typedef struct Node *PtrToNode;typedef PtrToNode Stack;

struct Node{ElementType Element;PtrToNode Next;

};

Division of Computer Science and Engineering, Hanyang University

Stack ADT: linked list implementation

Stack CreateStack (void){Stack S;

S = malloc(sizeof (struct Node));if (S == NULL)

FatalError(“Out of space !!!”);MakeEmpty(S);return S;

}

void MakeEmpty(Stack S) {if (S == NULL)

Error (“Must use CreateStack first”);else

while( !IsEmpty(S))Pop(S);

}

Division of Computer Science and Engineering, Hanyang University

Stack ADT: linked list implementation

Push(ElementType X, Stack S)

XA3

A2

A1

Push(X, S)

A3

A2

A1

A3

S

header A2 A1

S

header A3 A2 A1X

Division of Computer Science and Engineering, Hanyang University

Stack ADT: linked list implementation

void Push (ElementType X, Stack S) {PtrToNode TmpCell;

TmpCell = malloc (sizeof (struct Node));if (TmpCell ==NULL)

FatalError(“Out of space !!!”);else {

TmpCell -> Element = X;TmpCell -> Next = S -> Next;S -> Next = TmpCell;

}} 7

A3

S

S

header

header

A2 A1

A3 A2 A1X

Division of Computer Science and Engineering, Hanyang University

Stack ADT: linked list implementation

Pop(Stack S)

A2

A1

Pop(S)

A3

A2

A1

A3

S

header A2 A1

S

header A2 A1

Division of Computer Science and Engineering, Hanyang University

Stack ADT: linked list implementationElementType Top (Stack S) {

if (!IsEmpty(S))return S->Next->Element;

Error (“Empty stack”);return 0;

}

void Pop (Stack S) {PtrToNode FirstCell;

if (IsEmpty(S))Error(“Empty stack”);

else{FirstCell = S->Next;S->Next = S->Next->Next;free(FirstCell);

}}

Division of Computer Science and Engineering, Hanyang University

Stack ADT: array implementation

typedef struct StackRecord *Stack;

struct StackRecord{

int Capacity;int TopOfStack;ElementType *Array;

};

Capacity

TopOfStack

Array

Division of Computer Science and Engineering, Hanyang University

Stack ADT: array implementation#define EmptyTOS ( -1 )#define MinStackSize ( 5 )

Stack CreateStack( int MaxElements ){

Stack S;

if( MaxElements < MinStackSize )Error( “Stack size is too small” );

S = malloc( sizeof( struct StackRecord ) );if( S == NULL )

FatalError( “Out of space!!!” );

S->Array = malloc( sizeof( ElementType ) * MaxElements );if( S->Array == NULL )

FatalError( “Out of space!!!” );S->Capacity = MaxElements;MakeEmpty( S );

return S;}

11

Division of Computer Science and Engineering, Hanyang University

Stack ADT: array implementation

void MakeEmpty( Stack S ){

S->TopOfStack = EmptyTOS;}

void Push( ElementType X, Stack S ){

if( IsFull( S ) )Error( “Full stack” );

elseS->Array[ ++S->TopOfStack ] = X;

}

12

Division of Computer Science and Engineering, Hanyang University

Stack ADT: array implementation

ElementType Top( Stack S ){

if( !IsEmpty( S ) )return S->Array[ S->TopOfStack ];

Error( “Empty stack” );return 0; /* return value used to avoid warning */

}

void Pop( Stack S ){

if( IsEmpty( S ) )Error( “Empty stack” );

elseS->TopOfStack--;

}13

Division of Computer Science and Engineering, Hanyang University

Example 3.1 [system stack] place an activation record or a stack frame on top of

the system stack the activation record for the invoked function

contain a pointer to the previous stack frame and a return address(program counter)

on top of the system stack : one function executed at any given time

Division of Computer Science and Engineering, Hanyang University

Eg. System stack1 int main()

{int i=3;

20 fcn1(i);}

100 int fcn1(int a){

int j=5150 fcn2(j);

…}

200 void fcn2(int b){

…}

mainPC=1

fcn1PC=100a=3

fcn2PC=200b=5

mainPC=20

mainPC=20

mainPC=20

mainPC=21

fcn1PC=150a=3,j=5

fcn1PC=151a=3,j=5

Division of Computer Science and Engineering, Hanyang University

EVALUATION OF EXPRESSIONS

Division of Computer Science and Engineering, Hanyang University

Parentheses Matching

Division of Computer Science and Engineering, Hanyang University

Parentheses Matching

scan expression from left to right when a left parenthesis is encountered, add its position

to the stack when a right parenthesis is encountered, remove

matching position from stack

Division of Computer Science and Engineering, Hanyang University

Example

Division of Computer Science and Engineering, Hanyang University

Example

Division of Computer Science and Engineering, Hanyang University

Example

Division of Computer Science and Engineering, Hanyang University

Example

Division of Computer Science and Engineering, Hanyang University

Example

Division of Computer Science and Engineering, Hanyang University

Evaluation of Expressions Expressions

to fix the order of evaluation, assign to each operator a priority

How to generate the machine instructions corresponding to a given expression?

X = A / B - C + D * E - A * CX = 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 + 8 + 9 = 1Interpretation 2:(4/(2-2+3))*(3-4)*2 = (4/3)*(-1)*2 = -2.6666…

A = 4, B = C = 2, D = E = 3

Interpretation 1:((4/2)-2) + (3*3) – (4*2) = 0 + 8 + 9 = 1Interpretation 2:(4/(2-2+3))*(3-4)*2 = (4/3)*(-1)*2 = -2.6666…

Division of Computer Science and Engineering, Hanyang University

infix, prefix, postfix notation

3 + 4 * 6

* + 3 4 6

+ 3 * 4 6

3 4 + 6 *

3 4 6 * +

25

(3 + 4) * 6

3 + (4 * 6)

(3 + 4) * 6

3 + (4 * 6)

(3 + 4) * 6

3 + (4 * 6)

infix

prefix

postfix

?

?

Division of Computer Science and Engineering, Hanyang University

evaluation of postfix expression7 2 3 * 4 9 3 / +

7 6 4 9 3 / +

1 4 9 3 / +

1 9 3 / +

1 3 +

26

2 * 3 = 6

7 - 6 = 1

14 = 1

9 / 3 = 3

1 + 3 = 4

Division of Computer Science and Engineering, Hanyang University

Stack ADT: postfix expression6 5 2 3 + 8 * + 3 + * TopOfStack

27

3

2

5

6

5

5

6

8

5

5

6

40

5

6

+ 8 *

45

6

3

45

6

48

6

3 ++

288

*

Division of Computer Science and Engineering, Hanyang University

Infix to postfix

an algorithm for producing postfix from infix

1) fully parenthesize the expression2) move all operators for replacing their corresponding

right parentheses3) delete all parentheses

infix: A / B - C + D * E - A * C

when fully parenthesized: ((( A / B ) - C ) + ( D * E )) - ( A * C ))

postfix: A B / C - D E * + A C * -

infix: A / B - C + D * E - A * C

when fully parenthesized: ((( A / B ) - C ) + ( D * E )) - ( A * C ))

postfix: A B / C - D E * + A C * -

Division of Computer Science and Engineering, Hanyang University

Stack ADT: infix to postfix conversion

a + b * c + (d * e + f) * g a b c * d e * f + g * +

+ +

*

+ +

a a a b a b c a b c *

+

a b c * +

(

+

a b c * +

(

+

a b c * + d

stack

output

*

(

+

a b c * + d

*

(

+

a b c * + d e

(

+

a b c * + d e *

+

(

+

a b c * + d e *

+

(

+

a b c * + d e * f

+

a b c * + d e * f +

*

+

a b c * + d e * f +

*

+

a b c * + d e * f + g

+

a b c * + d e * f + g * a b c * + d e * f + g * +

*

+

a b

Division of Computer Science and Engineering, Hanyang University

Eg. Infix to postfix if the input expression is empty, then output all remaining operators

in the stack infix: A + B * C

next token stack output

none empty none

A empty A

+ + A

B + AB

* +* AB

C +* ABC

done empty ABC*+

Division of Computer Science and Engineering, Hanyang University

Infix to postfix (Cont.) a priority-based scheme for stacking and

unstacking operators

establish two priorities for operators : isp(in-stack priority) andicp(in-coming priority)

isp( '(' ) = 0, icp( '(' ) = 20, isp( ‘)' ) = 19

stacking rule : operators are taken out of the stack as long as their in-stack priority is numerically greaterthan or equal to the in-coming priority of the new operator

priority operator0 (19 )12 +12 -13 *13 /13 %0 eos

Priority of operations in C

Division of Computer Science and Engineering, Hanyang University

Infix to postfix (Cont.)

/* isp and icp arrays -- index is value of precedencelparen, rparen, plus, minus, times, divide, mod, eos */static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0};static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0};

/* isp and icp arrays -- index is value of precedencelparen, rparen, plus, minus, times, divide, mod, eos */static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0};static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0};

Division of Computer Science and Engineering, Hanyang University

QUEUE

Division of Computer Science and Engineering, Hanyang University

Queue ADT

a list that insertions is done at one end, whereas deletion is performed at the other end

operations enqueue: inserts an element at the end of the list dequeue: deletes the element at the start of the list

5 2 7 6 7

front rear

dequeue enqueue

Division of Computer Science and Engineering, Hanyang University

Queue ADT: circular array when front or rear gets to the end of the array, it is

wrapped around to the beginning

2 4 front rear

enqueue(1)

1 2 4

1 3 2 4

1 3 4

enqueue(3)

dequeue( )1 3

dequeue( )dequeue( )

3

dequeue( )

Division of Computer Science and Engineering, Hanyang University

Queue ADT: circular array

check the queue for emptiness because a dequeue() when the queue is empty will return an undefined value silently.

36

5 2 7 6 7

front rear

dequeue enqueue

Division of Computer Science and Engineering, Hanyang University

struct QueueRecord;typedef struct QueueRecord *Queue;

struct QueueRecord{int Capacity;int Front;int Rear;int Size;ElementType *Array;

};

37

Queue ADT: circular array

Division of Computer Science and Engineering, Hanyang University

void MakeEmpty (Queue Q){Q -> Size = 0;Q -> Front = 1;Q -> Rear = 0;

}static int Succ(int Value, Queue Q){

if (++Value == Q->Capacity)Value = 0;

return Value;}void Enqueue (ElementType X, Queue Q){

if (IsFull(Q))Error(“Full queue”);

else {Q -> Size++;Q -> Rear = Succ(Q->Rear, Q);Q -> Array[Q->Rear] = X;

} 38

Queue ADT: array implementation

Division of Computer Science and Engineering, Hanyang University

Circular Queues Using Dynamically Allocated Array

queue [0] [1] [2] [3] [4] [5] [6] [7]

C D E F G A B

(a) A full circular queue

rear = 4front = 5

A

B

C D

E

FG front = 5, rear = 4

(b) Flattened view of circular full queue

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]

C D E F G A B

front = 5, rear = 4(c) After array doubling

Division of Computer Science and Engineering, Hanyang University

Circular Queues Using Dynamically Allocated Array

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]

C D E F G A Bfront = 13, rear = 4(d) After shifting right segment

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]

A B C D E F Gfront = 15, rear = 6(e) Alternative configuration

Figure 3.7: Doubling queue capacity

Division of Computer Science and Engineering, Hanyang University

The Queue Abstract Data Type (Cont.) Doubling queue capacity

(1) Create a new array newQueue of twice the capacity.

(2) Copy the second segment (i.e., the elements queue[front+1] through queue[capacity-1]) to positions in newQueue beginning at 0.

(3) Copy the first segment (i.e., the elements queue[0] through queue[rear]) to positions in newQueuebeginning at capacity-front-1.

Division of Computer Science and Engineering, Hanyang University

The Queue Abstract Data Type (Cont.)

Division of Computer Science and Engineering, Hanyang University

The Queue Abstract Data Type (Cont.)