5 stacks 2
This presentation is the property of its rightful owner.
Sponsored Links
1 / 56

บทที่ 5 สแต๊ก ( Stacks ) (ส่วนที่ 2) PowerPoint PPT Presentation


  • 101 Views
  • Uploaded on
  • Presentation posted in: General

บทที่ 5 สแต๊ก ( Stacks ) (ส่วนที่ 2). By Juthawut Chantharamalee. Abstract Data Type ของสแต็ก ( Stack ADT ).

Download Presentation

บทที่ 5 สแต๊ก ( Stacks ) (ส่วนที่ 2)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


5 stacks 2

5 (Stacks)( 2)

By Juthawut Chantharamalee

(Data Structure) 4122202


Abstract data type stack adt

Abstract Data Type (Stack ADT)

ADT 1. 2. 3. Stack Top Pop

(Data Structure) 4122202


5 stacks 2

4. O(1) ADT ADT

(Data Structure) 4122202


5 stacks 2

main

Dynamic memory

data

stack

N

link

count

top

dataPtr

data

link

dataPtr

.

.

data

dataPtr

link

5.13 ADT (Stack ADT Structural Concepts)

(Data Structure) 4122202


5 stacks 2

ADT ADT ADT 5.13

STACK* stack

..

..

Stack = createStack ();

(Data Structure) 4122202


5 stacks 2

ADT C Stack Definitions, Create Stack, Push Stack, Pop Stack, Stack Top, Empty Stack, Stack Count Destroy Stack

(Data Structure) 4122202


5 stacks 2

1

2

3

4

5

6

7

8

9

10

11

12

// Stack ADT Type Definitions

typedef struct node

{

void* dataPtr;

struct node* link;

} STACK_NODE;

typedef struc

{

int count ;

STACK_NODE* top;

} STACK;

5.1 Stack ADT Definitions

(Data Structure) 4122202


5 stacks 2

5.2 Create Stack

(Data Structure) 4122202


5 stacks 2

5.3 Push Stack

(Data Structure) 4122202


5 stacks 2

5.4 Pop Stack

(Data Structure) 4122202


5 stacks 2

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/*=================== stackTop ===============

Retrieves data from the top of stack without changing the stack.

Pre stack is a pointer to the stack

Post Returns data pointer if successful

null pointer if stack empty

*/

void* stackTop (STACK* stack)

{

// Statements

if (stack->count == 0)

return NULL;

else

return stack->top->dataPtr;

} //stackTop

5.5 Retrieve Stack Top

(Data Structure) 4122202


5 stacks 2

1

2

3

4

5

6

7

8

9

10

/*================= emptyStack ==================

This function determines if a stack is empty.

Pre stack is pointer to a stack

Post Returns 1 if empty; 0 if data in stack

*/

bool emptyStack (STACK* stack)

{

// Statements

return (stack->count == 0);

} //emptyStack

5.6 Empty Stack

(Data Structure) 4122202


5 stacks 2

5.7 Full Stack

(Data Structure) 4122202


5 stacks 2

1

2

3

4

5

6

7

8

9

10

/*============= stackCount ============

Returns number of elements in stack.

Pre stack is a pointer to the stack

Post count returned

*/

int stackCount (STACK* stack)

{

// Statements

return stack->count;

} //stackCount

5.8 Stack Count

(Data Structure) 4122202


5 stacks 2

5.9 Destroy Stack

(Data Structure) 4122202


5 stacks 2

Push

(Data Structure) 4122202


5 stacks 2

Pop

(Data Structure) 4122202


5 stacks 2

Call

Task A

..

..

Task B

..

..

Call

Return

Task C

..

..

Call

Return

Task D

..

..

Return

5.14

(Data Structure) 4122202


5 stacks 2

5.14 Task A Task B Task B Task C Task C Task D Task D Task

(Data Structure) 4122202


5 stacks 2

C

B

B

A

A

A

(b)

(a)

(c)

(d)

B

A

A

(e)

(f)

(g)

5.15 Push Pop

(Data Structure) 4122202


Stack applications

(Stack Applications)

(Reversing Data)

Top {4 3 2 1} {1 2 3 4}

(Data Structure) 4122202


5 stacks 2

Pop

4

1

3

2

2

3

4

1

Stack 2

Stack 1

5.16

(Data Structure) 4122202


5 stacks 2

(Parsing)

(Tokens)

(Data Structure) 4122202


5 stacks 2

?

( ( A + B ) / C

(a) Opening parenthesis not matched

?

( A + B ) / C )

(b) Closing parenthesis not matched

5.17

(Data Structure) 4122202


5 stacks 2

(Postponement)

Infix Postfix

(Backtracking Steps)

(Goal Seeking)

(Data Structure) 4122202


5 stacks 2

6

7

4

Star node

8

5

11

9

10

The goal

1

2

3

13

12

14

15

16

17

18

5.18

(Data Structure) 4122202


5 stacks 2

1. Infix

2. Postfix

3. Prefix

(Data Structure) 4122202


5 stacks 2

Operator

A + X1 * 2

Operand

Prefix: +AB

Infix: A+B

Postfix: AB+

5.19

(Data Structure) 4122202


5 stacks 2

Infix Postfix

Infix Infix Infix Postfix Prefix

(Data Structure) 4122202


5 stacks 2

Infix Postfix

1.

2. Infix Postfix Postfix 3.

(Data Structure) 4122202


5 stacks 2

5.1 A + B * C Postfix

Infix A + B * C

1

(A + (B * C))

2 * C

(A + (BC*))

+

(A (BC*) + )

2

ABC*+

(Data Structure) 4122202


5 stacks 2

5.2 5 + 6 - 10 Postfix

5 * 6 -10= ((5 * 6)-10)

= ((56*)- 10)

= ((56*)10-)

= 56*10-

Postfix 5.2 postfix

(Data Structure) 4122202


5 stacks 2

Expression

Action

Stack

5 6 * 10 -

Push 5

5

5 6 * 10 -

Push 6

6

5

5 6 * 10 -

Pop 6 and 5

Evaluate 5 * 6

Push 30

30

5 6 * 10 -

Push 10

10

30

5 6 * 10 -

Pop 10 and 30

Evaluate 30 - 10

Push 20

20

5 6 * 10 -

Pop 20

Stack is Empty

Result is 20

5.20 Postfix

(Data Structure) 4122202


5 stacks 2

Infix Postfix (Algorithmic Transformation)

Infix Postfix 1. Postfix 2. - Push - - Pop

(Data Structure) 4122202


5 stacks 2

- Postfix Push 3. Push 4. Pop Postfix 5. Pop Postfix

(Data Structure) 4122202


5 stacks 2

Infix

Stack

Postfix

(a)

A

A

(b)

+

+

A

(c)

B

+

A B

(d)

*

*

A B

+

(e)

*

*

A B C

+

(f)

-

A B C * +

-

(g)

D

A B C * + D

-

(h)

/

/

A B C * + D

-

(i)

E

/

A B C * + D E

-

(j)

A B C * + D E / -

5.21 Infix Postfix

(Data Structure) 4122202


5 stacks 2

5.9 Infix Postfix (Convert Infix to Postfix)

(Data Structure) 4122202


5 stacks 2

5.3 A * B (C + D) Postfix

Input (Infix)

Operator Stack

Output String (Postfix)

A

A

*

*

A

B

*

A B

-

-

A B *

(

- (

A B *

C

-(

A B * C

+

- ( +

A B * C

D

- ( +

A B * C D

)

-

A B * C D +

+

+

A B * C D + -

E

+

A B * C D + - E

A B * C D + - E +

(Data Structure) 4122202


5 stacks 2

Postfix (Evaluation Postfix Expressions)

Postfix Postfix 5.22 Postfix 5.10 Postfix

(Data Structure) 4122202


5 stacks 2

A B C + *

A=2 B = 4 C = 6

Postfix

Stack

(a)

2 4 6 + *

(b)

4 6 + *

2

4

(c)

6 + *

2

6

(d)

+ *

4

2

4 * 6 = 10

(e)

*

10

2

2 * 10 = 20

(f)

20

5.22 Postfix

(Data Structure) 4122202


5 stacks 2

Algorithm postFixEvaluate (expr)

This algorithm evaluates a postfix expression and returns its value.

Pre a valid expression

Post postfix value computed

Return value of expression

1 ereateStack (stack)

2 loop (for each character)

1 if (character is operand)

1 pushstaek (stack, character)

2 else

1 popStaek (stack, oper2)

2 popStack (stack, operl)

3 operator = character

4 set value to calculate (operl, operator, oper2)

5 pushstack (stack, value)

3 end if

3 end loop

4 popStack (stack, result)

5 return (result)

end postFixEvaluate

5.10 Postfix (Evaluation of Postfix Expressions

(Data Structure) 4122202


5 stacks 2

(Stack Array Algorithm)

0 ( C) Push Pop Push Pop

(Data Structure) 4122202


5 stacks 2

Algorithm createStack (stackElem <integer>)

Allocates memory for a. stack head node from, dynamic memory

and returns its address to the caller.

Pre stackElem contains size of stack

Post head node and array allocated or error returned

Return pointer to head node or null pointer if no memory

1 if(memory not available

1 stackPtr = null )

2 else }

1 allocate (stackPtr)

Head allocated. Now initialize and allocate stack array.

2 stackPtr -> count = 0

3 stackPtr -> top = -1

4 stackPtr -> stackMax = stackElem

5 if (memory not available)

1 recycle (stackPtr)

2 stackPtr = null

6 else

Allocate memory for stack array

1 allocate (stackPtr -> stackAry)

3 return stackPtr

end createStack

5.11 Create Stack

(Data Structure) 4122202


5 stacks 2

Algorithm pushstack (val stack <head pointer>, val data <dataType>)

Insert (push) one item into the stack.

Pre stack is a pointer to the stack head structure

data contains data to be pushed into stack

Post data have been pushed in stack

Return true if successful

false if memory overflow

1 if (stack -> count is at maximum)

1 success = false

2 else

1 stack -> count = stack -> count + 1

2 stack -> top = stack -> top + 1

3 stack -> stack&Ary[stack -> top] = data

4 success = true

3 return success

end pushstack

5.12 Push Stack

(Data Structure) 4122202


5 stacks 2

Algorithm popStack (val stack <head pointer>, ref dataOut <dataType>)

This algorithm pops the item on the top of the stack and returns it to the user.

Pre stack is a pointer to the stack head structure

dataOut is a reference variable to receive the data

Post data have been returned to calling algorithm

Return true if successful

false if underflow

1 if (stack empty)

1 success = false

2 else

1 dataOut = stack -> staek&ry [stack -> top]

2 stack -> top = stack -> top - 1

3 stack -> count = stack -> count - 1

4 success = true

3 return success

end popStack

5.13 Pop Stack

(Data Structure) 4122202


5 stacks 2

Algorithm stackTop (val stack <head pointer>,ref dataOut <dataType>)

This algorithm retrieves the data from the top of the

stack without changing the stack.

Pre stack is a pointer to the stack head structure

Post data have been returned to calling algorithm

Return true if data returned

false if underflow

1 if (stack -> count zero)

1 success = false

2 else

1 dataOut = stack -> stack&ry [stack -> top].data

2 success = true

3 return success

end stackTop

5.14 Stack Top

(Data Structure) 4122202


5 stacks 2

Algorithm emptyStack (val stack <head pointer>)

Determines if stack is empty and return a Boolean.

Pre stack is a pointer to the stack head structure

Post returns stack status

Return Boolean, true if stack empty

false if stack contains data

1 if (stack -> count > 0)

1 result = false

2 else

1 result = true

3 return result

end emptyStack

5.15 Empty Stack

(Data Structure) 4122202


5 stacks 2

Algorithm fullStack (val stack <head pointer>)

Determines if stack is full and return a Boolean.

Pre stack is a pointer to the stack head structure

Post returns stack status

Return Boolean, true if stack full

false if memory available

1 if (stack -> count < MAX_STACK)

1 result = false

2 else

1 result = true

3 return result

end fullStack

5.16 Full Stack

(Data Structure) 4122202


5 stacks 2

Algorithm stackCount (val stack <head pointer >)

Return the number of elements currently in stack.

Pre stack is a pointer to the stack head structure

Post returns stack count

Return integer count of number of elements in stack

1 return (stack -> count)

end stackCount

5.17 Stack Count

(Data Structure) 4122202


5 stacks 2

Algorithm destroyStack (val stack <typeStack>)

This algorithm releases all nodes back to the dynamic memory.

Pre stack is a pointer to the stack head structure

Post head structure and array recycled

Return null pointer

1 If (stack not empty)

1 recycle (stack -> stackAry)

2 recycle (stack)

2 return null pointer

end destroyStack

5.18 Destroy Stack

(Data Structure) 4122202


Recursion

(Recursion)

(Recursion) (Recursive) 2 1. (Iteration) (Looping) 2. (Recursion)

(Data Structure) 4122202


5 stacks 2

Factorial (n) = 1 (if n = 0)

Factorial (n) = n X (n-1) X (n-2) X X 3 X 2 X 1 {if n > 0}

  • Algorithm iterativeFactorial (n)

  • Calculates factorial of a number using a loop.

  • Pre n is the number to be raised factorially

  • Post n! Is returned

  • set i to 1

  • set facN to 1

  • 3 loop (i <= n)

  • 1 set facN to facN * 1

  • 2 increment i

  • 4 end loop

  • 5 return FactN

  • end iterativeFactorial

5.19 Iterative Factorial Algorithm

(Data Structure) 4122202


5 stacks 2

Factorial (3) = 3 X Factorial (2)

Factorial (3) = 3 X 2 = 6

Factorial (2) = 2 X Factorial (1)

Factorial (2) = 2 X 1 = 2

Factorial (1) = 1 X Factorial (0)

Factorial (1) = 1 X 1 = 1

Factorial (0) = 1

5.23 Factorial(3)

(Data Structure) 4122202


5 stacks 2

Factorial (n) = 1 (if n = 0)

Factorial (n) = n X (Factorial (n-1)) {if n > 0}

Algorithm recursiveFactorial (n)

Calculates factorial of a number using recursion.

Pre n is the number to be raised factorially

Post n! is returned

1 if {n equals 0}

1 return 1

2 else

1 return (n * recursiveFactorial (n - 1) )

3 end if

end recursiveFactorial

5.20 Recursive Factorial Algorithm

(Data Structure) 4122202


5 stacks 2

5.24 Factorial(3)

(Data Structure) 4122202


5 stacks 2

The End

Lesson 5

(Session 2)

(Data Structure) 4122201


  • Login