Transcript
Page 1: 7 – Stack and Queue

1

Objectives of these slides: Explain the design, use, and operation of a stack and

a queue Implement a stack and a queue using a linked list

structure

7 – Stack and Queue7 – Stack and Queue

Page 2: 7 – Stack and Queue

22

Overview:

1. A Stack Collection

2. Stack Implementation

3. Stack applications

4. A queue Collection

5. Queue Implementation

Page 3: 7 – Stack and Queue

33

A stack is a sequence of items that are accessible only from the top of the stack.

A Stack (of plates)

pushpop

Other operations:

isEmptypeeksize

1. The Stack Collection

Page 4: 7 – Stack and Queue

44

A stack is a Last In, First Out (LIFO) data structure in which all insertions and deletion are restricted to one end called a top

Three basic stack operations

- Push

- Pop

- Stack Top

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

1. The Stack Collection: Stack Operations

Page 5: 7 – Stack and Queue

55

1. Push : adds an item at the top of the stack after the push, the new item becomes the

top the stack is in an overflow state if there is

no room for the new item

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

1.2 Stack Operations : Push

Page 6: 7 – Stack and Queue

66

2. Pop when a stack is popped, the item at the top

of the stack is removed and return it to the user

as the top item has been removed, the next older item in the stack becomes the top

when the last item in the stack is deleted, it must be set to its empty state

if pop is called when the stack is empty, then it is in an underflow state

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

1.2 Stack Operations : Pop

Page 7: 7 – Stack and Queue

77

1.2 Stack Operations : Pop

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 8: 7 – Stack and Queue

88

1.2 Stack Operations : Stack Top

3. Stack Top copies the item at the top of the stack it returns the data in the top element to the

user but does not delete it stack top can also result in underflow if the

stack is empty

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 9: 7 – Stack and Queue

99

Stack operations Example (1/2)

0. with an empty stack

1. push green into stack

2. push blue into stack

3. pop

4. push red into stack

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 10: 7 – Stack and Queue

1010

Stack operations Example (2/2)

5. stack top =?

6. pop

7. pop

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 11: 7 – Stack and Queue

1111

2. Stack implementation

There are several data structures that could be used to implement a stack, e.g. array or linked list

In this section, we implement it as a linked list

To implement the linked-list stack, we need two different structures: - a head node - a data node

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 12: 7 – Stack and Queue

1212

2.1 Stack Head and Stack Data Node

stack head requires at least 2 attributes a count of the number of elements in the

stack a top pointer

other stack attributes can be placed in a stack head such as the time the stack was created

stack data node also requires at least 2 attributes data pointer to the next node

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 13: 7 – Stack and Queue

1313

Stack head node structure and data node structure

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 14: 7 – Stack and Queue

1414

2.2 Stack Algorithms

8 operations are defined to solve any basic stack problem create stack destroy stack push stack pop stack Checking stack status

full stack empty stack a number of member (stack count) stack top

there may be additional stack operations

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 15: 7 – Stack and Queue

1515

Stack operations

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 16: 7 – Stack and Queue

1616

Create Stack

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 17: 7 – Stack and Queue

1717Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Push Stack

Page 18: 7 – Stack and Queue

1818Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Pop Stack

Page 19: 7 – Stack and Queue

1919

Stack Top

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 20: 7 – Stack and Queue

2020

Empty Stack Checking

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 21: 7 – Stack and Queue

2121

Full Stack Checking

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 22: 7 – Stack and Queue

2222

Stack Count

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 23: 7 – Stack and Queue

2323

Stack Destruction

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 24: 7 – Stack and Queue

2424

3. Stack Applications

3.1 Reversing Data

3.2 Converting Decimal to Binary

3.3 Converting Infix to Postfix (optional)

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 25: 7 – Stack and Queue

2525

3.1 Reversing Data

given a set of data, the first and last elements are exchanged with all of the positions between the first and last being relatively exchanged also

for example: {1, 2, 3, 4} becomes {4, 3, 2, 1}

we examine 2 different reversing applications: reveres a list convert decimal to binary

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 26: 7 – Stack and Queue

2626

Algorithm Reverse a number series (1/2)

Algorithm reverseNumberThis program reverses a list of integers read from the keyboard by pushing them into a stack and retrieving them one by one

Begin reverseNumber

1 stack = createStack2 print(Enter a number)3 read(number)

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Continued…

Page 27: 7 – Stack and Queue

2727

Algorithm Reverse a number series (2/2)

/*---- Fill stack ---* /4 loop ( not end of data AND stack not full) 1 pushStack (number) 2 prompt(Enter next number: <EOF> to stop) 3 read( number )/*---- Print numbers in reverse ---*/5 loop (not emptyStack (stack) 1 popStack (stack, dataOut) 2 print (dataOut)6 stack = destroyStack (stack)

End reverseNumber

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 28: 7 – Stack and Queue

2828

3.2 Convert Decimal to Binary

1 read (number)

2 loop (number > 0)

1 digit = number modulo 2

2 print (digit)

3 number = number / 2

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 29: 7 – Stack and Queue

2929

Algorithm Convert decimal to binary (1/2)

algorithm decimalToBinary This algorithm reads an integer from the keyboard and print its binary equivalent. It

uses a stack to reverse the order of 0’s and 1’s produces.

Begin decimalToBinary1 stack = createStack2 prompt(Enter a decimal to convert to binary)3 read (number)4 loop (number > 0)

1 digit = number modulo 22 pushOK = push (stack, digit)3 if (pushOK ≠ false)

1 print (Stack overflow creating digit)2 quit algorithm

4 number = number/2

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Continued…

Page 30: 7 – Stack and Queue

3030

Algorithm Convert decimal to binary (2/2)

/* Binary number create in stack. Now print it. */

5  loop (not emptyStack(stack))1 popStack (stack, digit)2 print(digit)

/* Binary number create. Destroy stack and return */

6 destroy(stack)

End decimalToBinary

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 31: 7 – Stack and Queue

3131

3.3 Convert Infix to Postfix (arithmetic expression)

Prefix Notation (Polish notation): operator is always placed before its operandse.g. + 1 2 , * 3 – 4 5 , / 9 + 1 – 8 6

In the Postfix evaluation format for an expression, an operator comes after its operands. also called Reverse Polish Notation (RPN)

Examples:Infix notation Postfix notation

a + b RPN: a b + a * b + c RPN: a b * c + a + (b * c) RPN: a b c * + (a + b) * c RPN: a b + c * (a*b + c) / d RPN: a b * c + d /

Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Page 32: 7 – Stack and Queue

3232

Postfix Evaluation (1/4)

To evaluate a postfix expression, execute the following steps until the end of the expression.

1. If current input is an operand, push it on the stack.

2. If current input is an operator, pop its two operands, apply the operator, and push the result onto the stack.

3. At the end of input, the value of the postfix expression is on the top of the stack.

Postfix and prefix (Polish) notations do not need the parenthesis and priority of operators. The order of evaluation is not ambiguous. One expression can only be evaluated in one way.

Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Page 33: 7 – Stack and Queue

3333

Example: evaluate "4 3 * 5 +"

44 44 1212 1212 1717

33 55

Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Postfix Evaluation (2/4)

Evaluate these postfix expressions: 8 2 – 3 / 4 2 * + 28 7 13 5 6 + - * /

Page 34: 7 – Stack and Queue

3434

Postfix evalutation: Error: too many operators (3/4)

e.g. 3 8 + * 9

The stack will contain only one element when we reach "*".

Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Page 35: 7 – Stack and Queue

3535

Postfix evalutation: Error: too many operands (4/4)

e.g. 9 8 + 7

The stack will contain too many operands at end of input.

Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt

Page 36: 7 – Stack and Queue

3636

Algorithm for coverting Infix to Postfix (1/2)

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

Page 37: 7 – Stack and Queue

3737

Algorithm for coverting Infix to Postfix (2/2)

Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

3 end loop 4 pushStack (Stack, token) 4 else Character is operand 1 Concatenate token to postFix 5 end if3 end loop Input formula empty. Pop stack to postfix4 loop ( not emptyStack(Stack) ) 1 popStack(Stack, character) 2 concatenate token to postFixExpr5 end loop6 return postFix

end intoPostFix

Page 38: 7 – Stack and Queue

3838

4. Queue Collection

Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end.

Accessing the elements of queues follows a First In, First Out (FIFO) order. Like customers standing in a check-out line

in a store, the first customer in is the first customer served (first come first serve).

rearfront

Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

Page 39: 7 – Stack and Queue

3939

4.1 Enqueue and Dequeue

Primary queue operations: Enqueue and Dequeue

Like check-out lines in a store, a queue has a front and a rear. Enqueue

insert an element at the rear of the queue Dequeue

remove an element from the front of the queue

Insert (Enqueue)

Remove(Dequeue)

rearfrontOriginal slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

Page 40: 7 – Stack and Queue

4040

5. Implementation of Queue

Just as stacks can be implemented as arrays or linked lists, so with queues.Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacksExample: Queue implemented with linked list data structure Queue Head Structure

Count (number of elements) Front (pointer to front node) Rear (pointer to rear node)

Queue Node StructureData (node data) Link (pointer to next node)

Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

Page 41: 7 – Stack and Queue

4141

5.1 Application of Queues

In a multitasking operating system, the CPU time is shared between multiple processes. At a given time, only one process is running, all the others are ‘sleeping’. The CPU time is administered by the scheduler. The scheduler keeps all current processes in a queue with the active process at the front of the queue.

Original slides from http://isg.cs.tcd.ie/giangt/Stack-Queue.pdf

Page 42: 7 – Stack and Queue

4242

5.2 Stack and Queue comparison

List out one similarity and one difference between Stack and Queue Similarity

Both ADTs can be implemented using array or linked list

Both support fast insertion and extraction (although the rule to govern the insertion and extraction is different)

And more… Difference

Stack is a LIFO data structure, while Queue is a FIFO data structure

And more…Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt


Top Related