Stacks and queues

Download Stacks and queues

Post on 08-Jul-2015

184 views

Category:

Education

0 download

Embed Size (px)

DESCRIPTION

Stacks, Queues, types of queues and standard operations on queues and stacks

TRANSCRIPT

  • 1. Stacks and QueuesTrupti Agrawal 1

2. Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.Trupti Agrawal 2 3. Stack Of CupstopbottomEDCBA Add a cup to the stack.topbottomFEDCBA Remove a cup from new stack. A stack is a LIFO list.Trupti Agrawal 3 4. Towers Of Hanoi/Brahma1 2 3 4A B C 64 gold disks to be moved from tower A to tower C each tower operates as a stack cannot place big disk on top of a smaller oneTrupti Agrawal 4 5. Towers Of Hanoi/Brahma1 2 3A B C 3-disk Towers Of Hanoi/BrahmaTrupti Agrawal 5 6. Towers Of Hanoi/Brahma1 2A B C 3-disk Towers Of Hanoi/Brahma3Trupti Agrawal 6 7. Towers Of Hanoi/Brahma1 2 3A B C 3-disk Towers Of Hanoi/BrahmaTrupti Agrawal 7 8. Towers Of Hanoi/Brahma1 2 3A B C 3-disk Towers Of Hanoi/BrahmaTrupti Agrawal 8 9. Towers Of Hanoi/Brahma1 2 3A B C 3-disk Towers Of Hanoi/BrahmaTrupti Agrawal 9 10. Towers Of Hanoi/Brahma3 2 1A B C 3-disk Towers Of Hanoi/BrahmaTrupti Agrawal 10 11. Towers Of Hanoi/BrahmaA B C 3-disk Towers Of Hanoi/Brahma1 23Trupti Agrawal 11 12. Towers Of Hanoi/BrahmaA B C 3-disk Towers Of Hanoi/Brahma1 2 3 7 disk moves Trupti Agrawal 12 13. Recursive Solution1A B C n > 0 gold disks to be moved from A to C using B move top n-1 disks from A to B using CTrupti Agrawal 13 14. Recursive Solution1A B C move top disk from A to CTrupti Agrawal 14 15. Recursive SolutionA B C move top n-1 disks from B to C using A1Trupti Agrawal 15 16. Recursive Solution1A B C moves(n) = 0 when n = 0 moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0Trupti Agrawal 16 17. Towers Of Hanoi/Brahma moves(64) = 1.8 * 1019 (approximately) Performing 109 moves/second, a computer would take about570 years to complete. At 1 disk move/min, the monks will take about 3.4 * 1013 years.Trupti Agrawal 17 18. Rat In A MazeTrupti Agrawal 18 19. Rat In A Maze Move order is: right, down, left, up Block positions to avoid revisit.Trupti Agrawal 19 20. Rat In A Maze Move order is: right, down, left, up Block positions to avoid revisit.Trupti Agrawal 20 21. Rat In A Maze Move backward until we reach a square from which a forwardmove is possible.Trupti Agrawal 21 22. Rat In A Maze Move down.Trupti Agrawal 22 23. Rat In A Maze Move left.Trupti Agrawal 23 24. Rat In A Maze Move down.Trupti Agrawal 24 25. Rat In A Maze Move backward until we reach a square from which a forwardmove is possible.Trupti Agrawal 25 26. Rat In A Maze Move backward until we reach a square from which aforward move is possible. Move downwTarurptid Ag.rawal 26 27. Rat In A Maze Move right. Backtrack. Trupti Agrawal 27 28. Rat In A Maze Move downward.Trupti Agrawal 28 29. Rat In A Maze Move right.Trupti Agrawal 29 30. Rat In A Maze Move one down and then right.Trupti Agrawal 30 31. Rat In A Maze Move one up and then right.Trupti Agrawal 31 32. Rat In A Maze Move down to exit and eat cheese. Path from maze entry to current position operates as a stack.Trupti Agrawal 32 33. Stacks Standard operations: IsEmpty return true iff stack is empty IsFull return true iff stack has no remaining capacity Top return top element of stack Push add an element to the top of the stack Pop delete the top element of the stackTrupti Agrawal 33 34. Stacks Use a 1D array to represent a stack. Stack elements are stored in stack[0] through stack[top].Trupti Agrawal 34 35. Stacksa b c d e0 1 2 3 4 5 6 stack top is at element e IsEmpty() => check whether top >= 0 O(1) time IsFull() => check whether top == capacity 1 O(1) time Top() => If not empty return stack[top] O(1) timeTrupti Agrawal 35 36. Derive From arrayLista b c d e0 1 2 3 4 5 6 Push(theElement) => if full then either error orincrease capacity and then add at stack[top+1] Suppose we increase capacity when full O(capacity) time when full; otherwise O(1)Pop() => if not empty, delete from stack[top] O(1) timeTrupti Agrawal 36 37. Pusha b c d e0 1 2 3 4top void push(element item) {/* add an item to the global stack */ if (top >= MAX_STACK_SIZE - 1) StackFull(); /* add at stack top */ stack[++top] = item; }Trupti Agrawal 37 38. Popa b c d e0 1 2 3 4top element pop() { if (top == -1) return StackEmpty(); return stack[top--]; }Trupti Agrawal 38 39. StackFull()void StackFull(){fprintf(stderr, Stack is full, cannot addelement.);exit(EXIT_FAILURE);}Trupti Agrawal 39 40. StackFull()/Dynamic Array Use a variable called capacity in place ofMAX_STACK_SIZE Initialize this variable to (say) 1 When stack is full, double the capacity using REALLOC This is called array doublingTrupti Agrawal 40 41. StackFull()/Dynamic Arrayvoid StackFull(){REALLOC(stack, 2*capacity*sizeof(*stack);capacity *= 2;}Trupti Agrawal 41 42. Complexity Of ArrayDoubling Let final value of capacity be 2k Number of pushes is at least 2k-1+ 1 Total time spent on array doubling is S1 delete queue[0] O(queue size) time AddQ(x) => if there is capacity, add at right end O(1) timeTrupti Agrawal 51 52. Circular Array(Circular Queue) Use a 1D array queue.queue[] Circular view of array.[0][1][2] [3][4][5]Trupti Agrawal 52 53. Circular Array Possible configuration with 3 elements.[0][1][2] [3][4][5]A BCTrupti Agrawal 53 54. Circular Array Another possible configuration with 3elements.[0][1][2] [3][4][5]B ACTrupti Agrawal 54 55. Circular Array Use integer variables front and rear. front is one position counterclockwise from firstelement rear gives position of last element[0][1][2] [3][4][5]A BCfrontrear[0][1][2] [3][4][5]B ACfrontrearTrupti Agrawal 55 56. Add An Element[0][1][2] [3][4][5]A BCfrontrear Move rear one clockwise.Trupti Agrawal 56 57. Add An Element Move rear one clockwise. Then put into queue[rear].[0][1][2] [3][4][5]A BCfrontDrearTrupti Agrawal 57 58. Delete An Element[0][1][2] [3][4][5]A BCfrontrear Move front one clockwise.Trupti Agrawal 58 59. Delete An Element Then extract from queue[front].[0][1][2] [3][4][5]A BCfrontrear Move front one clockwise.Trupti Agrawal 59 60. Moving rear Clockwise rear++;if (rear = = capacity) rear = 0;front rear[0][1][2] [3][4][5]A BC rear = (rear + 1) % capacity;Trupti Agrawal 60 61. Empty That Queue[0][1][2] [3][4][5]B ACfrontrearTrupti Agrawal 61 62. Empty That Queue[0][1][2] [3][4][5]BCfrontrearTrupti Agrawal 62 63. Empty That Queue[0][1][2] [3][4][5]CfrontrearTrupti Agrawal 63 64. Empty That Queue[0]rear[1][2] [3][4][5]frontTrupti Agrawal 64 65. Dequque In computer science, a double-ended queue (dequeue,often abbreviated to deque) is an abstract data type thatgeneralizes a queue, for which elements can be added toor removed from either the front (head) or back (tail). It is also often called a head-tail linked list,Trupti Agrawal 65 66. Dequque [Cont] This differs from the queue abstract data type or First-In-First-Out List (FIFO), where elements can only be added toone end and removed from the other. This general dataclass has some possible sub-types: An input-restricted deque is one where deletion can bemade from both ends, but insertion can be made at one endonly. An output-restricted deque is one where insertion can bemade at both ends, but deletion can be made from one endonlyTrupti Agrawal 66 67. Dequque [Cont] Both the basic and most common list types incomputing, queues and stacks can be consideredspecializations of deques, and can be implemented usingdeques.Trupti Agrawal 67 68. Applications of stacks: Conversion forminfix to postfix and prefixexpressions There are a number of applications of stacks such as: To print characters/string in reverse order. Check the parentheses in the expression. To evaluate the arithmetic expressions such as, infix, prefixand postfix.Trupti Agrawal 68 69. Arithmetic expression:An expression is defined as a number of operands or dataitems combined using several operators. There arebasically three types of notations for an expression:1) Infix notation2) Prefix notation3) Postfix notation(Main advantage of postfix and prefix notation are no needto use brackets)Trupti Agrawal 69 70. Infix notation: It is most common notation in which,the operator is written or placed in-between the twooperands. For eg. The expression to add two numbersA and B is written in infix notation as: A+ B A and B : Operands + : Operator In this example, the operator is placed in-between theoperands A and B. The reason why this notation iscalled infix.Trupti Agrawal 70 71. Prefix Notation: It is also called Polish notation,named after in the honor of the mathematician JanLukasiewicz, refers to the notation in which theoperator is placed before the operand as: +AB As the operator + is placed before the operands A andB, this notation is called prefix (pre means before).Trupti Agrawal 71 72. Postfix Notation: In the postfix notation theoperators are written after the operands, so it iscalled the postfix notation (post means after), it isalso known as suffix notation or reverse polishnotation. The above postfix if written in postfixnotation looks like follows; AB+Trupti Agrawal 72 73. Notation Conversions: Let us take an expression A+B*Cwhich is given in infix notation. To evaluate this expressionfor values 2, 3, 5 for A, B, C respectively we must followcertain rule in order to have right result. For eg. A+B*C =2+3*5 = 5*5 = 25!!!!!!!! Is this the right result? No, this is because the multiplicationis to be done before addition, because it has higherprecedence over addition. This means that for an expressionto be calculated we must have the knowledge of precedenceof operators.Trupti Agrawal 73 74. Operator PrecedenceOperator Symbol PrecedenceExponential $ HighestMultiplication/Division *,/ Next highestAddition/Substraction +,- LowestTrupti Agrawal 74 75. THANK YOU!!!Trupti Agrawal 75