1 / 39

Stacks and Queues

Stacks and Queues. Chapter 3. Templates in C++. Template function in C++ makes it easier to reuse classes and functions.

lazaro
Download Presentation

Stacks and Queues

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and Queues Chapter 3

  2. Templates in C++ • Template function in C++ makes it easier to reuse classes and functions. • A template can be viewed as a variable that can be instantiated to any data type, irrespective of whether this data type is a fundamental C++ type or a user-defined type.

  3. Selection sort using templates

  4. Code fragment illustration template instantiation

  5. Stack • Last-In-First-Out (LIFO) list • Push • Add an element into a stack • Pop • Get and delete an element from a stack push pop

  6. System stack after function call

  7. Abstract data type Stack

  8. Implementation • private: inttop; T *stack; int capacity; • template<class T> Stack<T>::Stack(intstackCapacity): MaxSize(stackCapacity) { stack=new T[capacity]; top=-1; }

  9. template<classsT> inline Boolean Stack<T>::IsFull() { if (top==capacity-1) return TRUE; else return FALSE; } • template<classs T> inline Boolean Stack<T>::IsEmpty() { if (top==-1) return TRUE; else return FALSE; }

  10. Adding to a stack

  11. Deleting from a stack

  12. Queue • First-In-First-Out (FIFO) list • Add an element into a queue • Get and delete an element from a queue • Variation • Priority queue

  13. Abstract data type Queue

  14. Implementation • private: intfront,rear; T *queue; int capacity; • template<class T> Queue<T>::Queue(intqueueCapacity):capacity(queueCapacity) { queue=new T[capacity]; front=rear= 0; }

  15. template<classsT> inline Boolean Queue<T>::IsFull() { if (rear==capacity-1) return TRUE; else return FALSE; } • template<classs T> inline Boolean Stack<T>::IsEmpty() { if (front==rear) return TRUE; else return FALSE; }

  16. Circular queue • Two indices • front: one position clockwise from the first element • rear: current end

  17. Adding to a queue

  18. Deleting from a queue

  19. Problem: if front=rear? Increase the capacity of a queue just before it become full

  20. A mazing problem 0: path 1: block

  21. Allowable moves

  22. Data type move • struct offsets { inta,b; }; enum directions{ N, NE, E, SE, S, SW, W, NW}; offsets move[8];

  23. Table of moves

  24. Implementation with a stack • A maze is represented by a two dimensional array maze[m][p] • struct Items{ int x, y, dir; };

  25. Example

  26. Example

  27. First pass at finding a path through a maze

  28. Finding a path through a maze

  29. Evaluation of expressions • X = a / b - c + d * e - a * c • Suppose that a = 4, b = 2, c = 2, d =3, e = 3 • Interpretation 1: • ((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1 • Interpretation 2: • (4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2= -2.66666… • How to generate the machine instructions corresponding to a given expression? • Precedence rule + associative rule

  30. Priority of operators in C++

  31. Evaluation of expressions • Infix • Each operator comes in-between the operands • Eg. 2+3 • Postfix • Each operator appears after its operands • Eg. 23+ • Prefix • Each operator appears before its operands • Eg. +23

  32. Example

  33. Example: 6/2-3+4*2  62/3-42*+

  34. Evaluating postfix expressions voidEval(Expression e) {// Assume that the last token in e is‘#’ // NextTokenis used to get the next token frome Stack<Token>stack; // initialize stack for (Token x = NextToken(e); x!= ‘#’;x=NextToken(e)) if (xis an operand) stack.Push(x) // add to stack else {// operator remove the correct number of operands for operatorx fromstack; perform the operationxand store the result onto the stack; } }

  35. Infix to postfix • (1) Fully parenthesize expression • a / b - c + d * e - a * c  • ((((a / b) - c) + (d * e)) – (a * c)) • (2) All operators replace their corresponding right parentheses. • ((((a / b)- c)+ (d * e)) - a * c)) • (3) Delete all parentheses. • ab/c-de*+ac*-

  36. Example: a+b*c

  37. Example: a*b+c

  38. Example: a*(b+c)*d

More Related