# 7 – stack and queue

Post on 05-Jan-2016

63 views

Embed Size (px)

DESCRIPTION

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 Queue. Overview:. A Stack Collection Stack Implementation 3.Stack applications A queue Collection Queue Implementation. - PowerPoint PPT PresentationTRANSCRIPT

*Objectives of these slides:Explain the design, use, and operation of a stack and a queueImplement a stack and a queue using a linked list structure7 Stack and Queue

*Overview:A Stack CollectionStack Implementation3.Stack applicationsA queue CollectionQueue Implementation

*A stack is a sequence of items that are accessible only from the top of the stack.1. The Stack Collection

*A stack is a Last In, First Out (LIFO) data structure in which all insertions and deletion are restricted to one end called a topThree basic stack operations- Push- Pop- Stack TopOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.1. The Stack Collection: Stack Operations

*1. Push : adds an item at the top of the stackafter the push, the new item becomes the topthe 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

*2. Popwhen a stack is popped, the item at the top of the stack is removed and return it to the useras the top item has been removed, the next older item in the stack becomes the topwhen the last item in the stack is deleted, it must be set to its empty stateif 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

*1.2 Stack Operations : PopOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*1.2 Stack Operations : Stack Top3. Stack Top copies the item at the top of the stackit returns the data in the top element to the user but does not delete itstack top can also result in underflow if the stack is emptyOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*Stack operations Example (1/2)0. with an empty stackpush green into stackpush blue into stackpoppush red into stackOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*Stack operations Example (2/2)stack top =?poppopOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*2. Stack implementationThere are several data structures that could be used to implement a stack, e.g. array or linked listIn this section, we implement it as a linked listTo implement the linked-list stack, we need two different structures: - a head node- a data nodeOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*2.1 Stack Head and Stack Data Nodestack head requires at least 2 attributesa count of the number of elements in the stacka top pointerother stack attributes can be placed in a stack head such as the time the stack was createdstack data node also requires at least 2 attributesdata pointer to the next nodeOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*Stack head node structure and data node structureOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*2.2 Stack Algorithms8 operations are defined to solve any basic stack problemcreate stackdestroy stack push stackpop stackChecking stack statusfull stackempty stacka number of member (stack count)stack topthere may be additional stack operationsOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

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

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

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

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

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

*Empty Stack CheckingOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*Full Stack CheckingOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

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

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

*3. Stack Applications3.1 Reversing Data3.2 Converting Decimal to Binary3.3 Converting Infix to Postfix (optional)Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*3.1 Reversing Datagiven a set of data, the first and last elements are exchanged with all of the positions between the first and last being relatively exchanged alsofor example:{1, 2, 3, 4} becomes {4, 3, 2, 1}we examine 2 different reversing applications:reveres a listconvert decimal to binaryOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*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

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

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

*3.2 Convert Decimal to Binary1 read (number)2 loop (number > 0)1 digit = number modulo 22 print (digit)3 number = number / 2Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*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 0s and 1s produces.

Begin decimalToBinary1 stack = createStack2prompt(Enter a decimal to convert to binary)3read (number)4loop (number > 0)1 digit = number modulo 22 pushOK = push (stack, digit)3 if (pushOK false)1 print (Stack overflow creating digit)2 quit algorithm4 number = number/2Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.Continued

*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 decimalToBinaryOriginal slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*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 notationPostfix notationa + bRPN: a b +a * b + cRPN: a b * c +a + (b * c)RPN: a b c * +(a + b) * cRPN: 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

*Postfix Evaluation (1/4)To evaluate a postfix expression, execute the following steps until the end of the expression.If current input is an operand, push it on the stack.If current input is an operator, pop its two operands, apply the operator, and push the result onto the stack.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

*Example: evaluate "4 3 * 5 +"

Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.pptPostfix Evaluation (2/4)Evaluate these postfix expressions: 8 2 3 / 4 2 * + 28 7 13 5 6 + - * /

*Postfix evalutation: Error: too many operators (3/4)e.g. 3 8 + * 9The 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

*Postfix evalutation: Error: too many operands (4/4)e.g. 9 8 + 7The 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

*Algorithm for coverting Infix to Postfix (1/2)Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

*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