Data Structures Array Based Stacks
CIS265/506: Chapter 04 - Stacks and Queues Definition: list of homogeneous elements addition and deletion of elements occurs only at one end, called the top of the stack Last In First Out (LIFO) data structure Used to implement method calls Used to convert recursive algorithms (especially non-tail recursive) into nonrecursive algorithms CIS265/506: Chapter 04 - Stacks and Queues
Various Types of Stacks CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues LIFO Last In First Out (LIFO) data structure Top element of stack is last element to be added to stack Elements added and removed from one end (top) Item added last are removed first CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Empty Stack CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Stack Operations CIS265/506: Chapter 04 - Stacks and Queues
Basic Operations on a Stack initializeStack: Initializes the stack to an empty state isEmptyStack: Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false isFullStack: Checks whether the stack is full. If full, it returns true; otherwise, it returns false CIS265/506: Chapter 04 - Stacks and Queues
Basic Operations on a Stack push: Add new element to the top of the stack The input consists of the stack and the new element Prior to this operation, the stack must exist and must not be full CIS265/506: Chapter 04 - Stacks and Queues
Basic Operations on a Stack top: Returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty pop: Removes the top element of the stack. Prior to this operation, the stack must exist and must not be empty CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Efficiency Efficient Array Implementation of a stack means making use of how arrays work. When pushing, add an element to the end of the used elements in the array. When popping, do the reverse. Now, push and pop are both O(1) Doing it the other way is not O(1) unless some kind of a circular (more later) stack is used CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Example of a Stack CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Empty Stack CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues initializeStack public void initializeStack() { for(int i = 0; i < stackTop; i++) list[i] = null; stackTop = 0; }//end initializeStack CIS265/506: Chapter 04 - Stacks and Queues
emptyStack and fullStack public boolean isEmptyStack() { return(stackTop == 0); }//end isEmptyStack public boolean isFullStack() { return(stackTop == maxStackSize); }//end isFullStack CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Push CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Push public void push(DataElement newItem) throws StackOverflowException { if(isFullStack()) throw new StackOverflowException(); //add newItem at the top of the stack list[stackTop] = newItem.getCopy(); //increment stackTop stackTop++; }//end push CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Return Top Element public DataElement top() throws StackUnderflowException { if(isEmptyStack()) throw new StackUnderflowException(); DataElement temp = list[stackTop - 1].getCopy(); return temp; }//end top CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Pop public void pop() throws StackUnderflowException { if(isEmptyStack()) throw new StackUnderflowException(); stackTop--; //decrement stackTop list[stackTop] = null; }//end pop CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Pop CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues copy private void copy(StackClass otherStack) { list = null; System.gc(); maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new DataElement[maxStackSize]; //copy otherStack into this stack for(int i = 0; i < stackTop; i++) list[i] = otherStack.list[i].getCopy(); }//end copy 6 CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Constructors //constructor with a parameter public StackClass(int stackSize) { if(stackSize <= 0) System.err.println(“The size of the array to implement “ + “the stack must be positive.”); System.err.println(“Creating an array of size 100.”); maxStackSize = 100; } else maxStackSize = stackSize; //set the stack size to //the value specified by //the parameter stackSize stackTop = 0; //set stackTop to 0 list = new DataElement[maxStackSize]; //create the array }//end constructor CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Constructors //default constructor public StackClass() { maxStackSize = 100; stackTop = 0; //create array list = new DataElement[maxStackSize]; }//end default constructor CIS265/506: Chapter 04 - Stacks and Queues
Copy Constructor and copyStack public StackClass(StackClass otherStack) { copy(otherStack); }//end copy constructor public void copyStack(StackClass otherStack) { if(this != otherStack) //avoid self-copy copy(otherStack); }//end copyStack CIS265/506: Chapter 04 - Stacks and Queues
Time Complexity of Operations of class stackType CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Java class Stack Java provides a class to implement a stack in a program The name of the Java class defining a stack is Stack The class Stack is contained in the package java.util CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Java class Stack CIS265/506: Chapter 04 - Stacks and Queues
Empty and Nonempty Linked Stack CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Default Constructor public LinkedStackClass() { stackTop = null; } CIS265/506: Chapter 04 - Stacks and Queues
initializeStack, isStackEmpty, and isStackFull public void initializeStack() { stackTop = null; }//end initializeStack public boolean isEmptyStack() { return(stackTop == null); } public boolean isFullStack() return false; CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Push Stack before the push operation Stack and newNode CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Push Stack after the statement newNode.link = stackTop; executes Stack after the statement stackTop = newNode; executes CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Return Top Element public DataElement top() throws StackUnderflowException { if(stackTop == null) throw new StackUnderflowException(); return stackTop.info.getCopy(); }//end top CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Pop Stack after the statement stackTop = stackTop.link; executes Stack before the pop operation Stack after popping the top element CIS265/506: Chapter 04 - Stacks and Queues
Stack Implementation of Prefix, Infix & Postfix Data Structures Stack Implementation of Prefix, Infix & Postfix 1
CIS265/506: Chapter 04 - Stacks and Queues Basic Definitions There are many ways to write (and evaluate) mathematical equations. The first, called infix notation, is what we are familiar with from elementary school: (5*2)-(((3+4*7)+8/6)*9) You would evaluate this equation from right to left, taking in to account precedence. So: 10 - (((3+28)+1.33)*9) 10 - ((31 + 1.33)*9) 10 - (32.33 * 9) 10 - 291 -281 CIS265/506: Chapter 04 - Stacks and Queues 2
CIS265/506: Chapter 04 - Stacks and Queues Basic Definitions An alternate method is postfix or Reverse Polish Notation (RPN). The corresponding RPN equation would be: 5 2 * 3 4 7 * + 8 6 / + 9 * - We’ll see how to evaluate this in a minute. CIS265/506: Chapter 04 - Stacks and Queues 3
CIS265/506: Chapter 04 - Stacks and Queues Basic Definitions Note that in an infix expression, the operators appear in between the operands (1 + 2). Postfix equations have the operators after the equations (1 2 +). In Forward Polish Notation or prefix equations, the operators appear before the operands. The prefix form is rarely used (+ 1 2). CIS265/506: Chapter 04 - Stacks and Queues 4
CIS265/506: Chapter 04 - Stacks and Queues Basic Definitions Reversed Polish Notation got its name from Jan Lukasiewicz, a Polish mathematician, who first published in 1951. Lukasiewicz was a pioneer in three-valued propositional calculus, he also was interested in developing a parenthesis-free method of representing logic expressions. Today, RPN is used in many compilers and interpreters as an intermediate form for representing logic. http://www-groups.dcs.st-and.ac.uk/~history/PictDisplay/Lukasiewicz.html University of St. Andrews. CIS265/506: Chapter 04 - Stacks and Queues 3
CIS265/506: Chapter 04 - Stacks and Queues Examples RPN expressions Infix Prefix Postfix A+B +AB AB+ A+B*C +A*BC ABC*+ A*(B+C) *A+BC ABC+* A*B+C +*ABC AB*C+ A+B*C+D-E*F -++A*BCD*EF ABC*+D+EF*- (A+B)*(C+D-E)*F **+AB-+CDEF AB+CD+E-*F* CIS265/506: Chapter 04 - Stacks and Queues
Evaluating RPN Expressions We evaluate RPN using a left-to-right scan. An operator is preceded by two operands, so we store the first operand, then the second, and once the operator arrives, we use it to compute or evaluate the two operands we just stored. 3 5 + Store the value 3, then the value 5, then using the + operator, evaluate the pending calculation as 8. CIS265/506: Chapter 04 - Stacks and Queues 5
Evaluating RPN Expressions What happens if our equation has more than one operator? Now we’ll need a way to store the intermediate result as well: 3 5 + 10 * Store the 3, then 5. Evaluate with the +, getting 8. Store the 8, then store10, when * arrives evaluate the expression using the previous two arguments. The final result is 80. CIS265/506: Chapter 04 - Stacks and Queues 6
Evaluating RPN Expressions It starts to become apparent that we apply the operator to the last two operands we stored. Example: 3 5 2 * - Store the 3, then the 5, then the 2. Apply the * to the 5 and 2, getting 10. Store the value 10. Apply the - operator to the stored values 3 and 10 (3 - 10) getting -7. CIS265/506: Chapter 04 - Stacks and Queues 7
Evaluating RPN Expressions We have been saving values in such a way that the last two values saved become the first two retrieved. Remember stacks? They are last-in, first-out lists….. Exactly what we need for this application! CIS265/506: Chapter 04 - Stacks and Queues 8
Evaluating RPN Expressions How about an algorithm to evaluate an RPN expression? We scan our input stream from left to right, removing the first character as we go. We check the character to see if it is an operator or an operand. If it is an operand, we push it on the stack. If it is an operator, we remove the top two items from the stack, and perform the requested operation. We then push the result back on the stack. If all went well, at the end of the stream, there will be only one item on the stack - our final result. CIS265/506: Chapter 04 - Stacks and Queues 9
Evaluating RPN Expressions Step Stack RPN Expression Step Stack RPN Expression 4 2 8 - * 6 * 3 5 + 2 4 - * 6 * 6 1 -2 8 3 5 + 2 4 - * 6 * 2 * 6 * 7 53 -16 3 + 2 4 - * 6 * 8 6 * 8 4 2 4 - * 6 * 6 -16 9 * 2 8 4 - * 6 * -96 5 10 CIS265/506: Chapter 04 - Stacks and Queues 10
Evaluating RPN Expressions 1/3 package csu.matos; import java.util.Stack; import java.util.StringTokenizer; public class Driver { public static void main(String[] args) { // Taken from Daniel Liang – Intro to Java Prog. // the input is a correct postfix expression String expression = "1 2 + 3 *"; try { System.out.println( evaluateExpression(expression) ); } catch (Exception ex) { System.out.println("Wrong expression"); /** Evaluate an expression */ public static int evaluateExpression(String expression) { // Create operandStack to store operands Stack<Integer> operandStack = new Stack<Integer>(); // Extract operands and operators StringTokenizer tokens = new StringTokenizer(expression, " +-/*%", true); 10
Evaluating RPN Expressions 2/3 // Phase 1: Scan tokens while (tokens.hasMoreTokens()) { String token = tokens.nextToken().trim(); // Extract a token if (token.length() == 0) { // Blank space continue; // Back to the while loop to extract the next token } else if (token.charAt(0) == '+' || token.charAt(0) == '-' || token.charAt(0) == '*' || token.charAt(0) == '/') { processAnOperator(token.charAt(0), operandStack); else { // An operand scanned // Push an operand to the stack operandStack.push(new Integer(token)); // Return the result return ((Integer)(operandStack.pop())).intValue(); 10
Evaluating RPN Expressions 3/3 /** Process one opeator: Take an operator from operatorStack and * apply it on the operands in the operandStack */ public static void processAnOperator(char op, Stack operandStack) { if (op == '+') { int op1 = ((Integer)(operandStack.pop())).intValue(); int op2 = ((Integer)(operandStack.pop())).intValue(); operandStack.push(new Integer(op2 + op1)); } else if (op == '-') { operandStack.push(new Integer(op2 - op1)); else if ((op == '*')) { operandStack.push(new Integer(op2 * op1)); else if (op == '/') { operandStack.push(new Integer(op2 / op1)); 10
Converting Infix to Postfix Manual Transformation (Continued) Example: A + B * C Step 1: (A + ( B * C ) ) Change all infix notations in each parenthesis to postfix notation starting from the innermost expressions. This is done by moving the operator to the location of the expression’s closing parenthesis Step 2: ( A + ( B C * ) ) Step 3: ( A ( B C * ) + ) CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix Manual Transformation (Continued) Example: A + B * C Step 2: (A + ( B * C ) ) Step 3: (A ( B C * ) + ) Remove all parentheses Step 4: A B C * + CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix Another Example (A + B ) * C + D + E * F - G Add Parentheses ( ( ( ( ( A + B ) * C ) + D ) + ( E * F ) ) - G ) Move Operators ( ( ( ( ( A B + ) C * ) D + ) ( E F * ) + ) G - ) Remove Parentheses A B + C * D + E F * + G - CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix This looks very difficult to write a computer program to solve this problem. Lets try again Example: A * B This looks easy. Write the A, store the * on a stack, write the B, then get the * and write it. So, solution is: A B * CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix Here are a few things to consider: How to handle operator precedence What about parentheses? What happens if the equation is just typed wrong? (Operator error) CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix How to handle operator precedence What about parentheses? Fortunately, we can handle these situations in one step. Let us assume the parentheses are an operator. CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix Precedence for operators Highest 2: * / 1: + - Lowest: 0: ( What about closing parentheses? A closing parenthesis always signals the end of an expression or sub-expression. We will see in a minute how to handle this. CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix Based on what we know so far, here is the basic algorithm for conversion while there is more data get the first symbol if symbol = ( put it on the stack if symbol = ) take item from top of stack while this item != ( add it to the end of the output string Cont.... CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix if symbol is +, -, *, \ look at top of the stack while (stack is not empty AND the priority of the current symbol is less than OR equal to the priority of the symbol on top of the stack ) Get the stack item and add it to the end of the output string; put the current symbol on top of the stack if symbol is a character add it to the end of the output string End loop Cont.... CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix Finally While ( stack is not empty ) Get the next item from the stack and place it at the end of the output string End CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix What about precedence testing? Function precedence_test (operator) case operator “*” OR “/” return 2; case operator “+” OR “-” return 1; case operator “(“ return 0; default return 99; //signals error condition! CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix The line we are analyzing is: A*B-(C+D)+E Input Buffer *B-(C+D)+E B-(C+D)+E -(C+D)+E (C+D)+E C+D)+E +D)+E D)+E )+E +E E Operator Stack EMPTY * - -( -(+ + Output String A A B A B * 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 + CIS265/506: Chapter 04 - Stacks and Queues
Converting Infix to Postfix 1/4 public static void main(String[] args) { // Provide a correct infix expression to be converted String expression = "( 1 + 2 ) * 3"; try { System.out.println(infixToPostfix(expression)); } catch (Exception ex) { System.out.println("Wrong expression"); public static String infixToPostfix(String expression) { // Result string String s = ""; // Create operandStack to store operands Stack operandStack = new Stack(); // Create operatorStack to store operators Stack operatorStack = new Stack(); // Extract operands and operators StringTokenizer tokens = new StringTokenizer(expression, "()+-/*%", true);
Converting Infix to Postfix 2/4 // Phase 1: Scan tokens while (tokens.hasMoreTokens()) { String token = tokens.nextToken().trim(); // Extract a token if (token.length() == 0) { // Blank space continue; // Back to the while loop to extract the next token } else if (token.charAt(0) == '+' || token.charAt(0) == '-') { // Process all +, -, *, / in the top of the operator stack while (!operatorStack.isEmpty() && (operatorStack.peek().equals('+') || operatorStack.peek().equals('-') || operatorStack.peek().equals('*') || operatorStack.peek().equals('/') )) { s += operatorStack.pop() + " "; // Push the incoming + or - operator into the operator stack operatorStack.push(new Character(token.charAt(0)));
Converting Infix to Postfix 3/4 else if (token.charAt(0) == '*' || token.charAt(0) == '/') { // Process all *, / in the top of the operator stack while (!operatorStack.isEmpty() && (operatorStack.peek().equals('*') || operatorStack.peek().equals('/') )) { s += operatorStack.pop() + " "; } // Push the incoming * or / operator into the operator stack operatorStack.push(new Character(token.charAt(0))); else if (token.trim().charAt(0) == '(') { operatorStack.push(new Character('(')); // Push '(' to stack else if (token.trim().charAt(0) == ')') { // Process all the operators in the stack until seeing '(' while (!operatorStack.peek().equals('(')) { operatorStack.pop(); // Pop the '(' symbol from the stack else { // An operand scanned // Push an operand to the stack s += token + " ";
Converting Infix to Postfix 4/4 // Phase 2: process all the remaining operators in the stack while (!operatorStack.isEmpty()) { s += operatorStack.pop() + " "; } // Return the result return s;
Data Structures Queues 1
CIS265/506: Chapter 04 - Stacks and Queues Think of a queue as a waiting line at bank or store. Customers are served in the order they arrive, that is, the first person to arrive is the first person served. A queue is a FIRST-IN, FIRST-OUT structure. For that reason, queues are commonly called FIFO structures. CIS265/506: Chapter 04 - Stacks and Queues 2
CIS265/506: Chapter 04 - Stacks and Queues A queue is a collection of items holding the following properties: Items are somehow ordered in the collection Only one item, called the front element, can be removed from the collection New items can be added to the collection only at the other end - called the back or rear of the queue CIS265/506: Chapter 04 - Stacks and Queues 3
CIS265/506: Chapter 04 - Stacks and Queues View Data Here New Values Here 100 25 33 12 200 50 Front Back 100 is the only visible value from the queue. A new value can only be added after (“behind”) the 50. CIS265/506: Chapter 04 - Stacks and Queues 4
CIS265/506: Chapter 04 - Stacks and Queues Queue Data Structures front rear plum apple kiwi grape fig Conceptual View of A Queue Front Ptr count Rear Ptr Head Node 5 plum apple kiwi grape fig front rear Physical View of A Queue CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Array Based Queues In the same way we have array based stacks, we can also make array based queues There are several ways to look at the implementation problem We can say that front is always at index 0, while rear can “float” We can say that rear is always at index 0 and front can float Or, we can imagine a circular array. CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Types of Queues Queue.front is always zero, shift elements left on dequeue Queue.rear is always zero, shift elements right on enqueue In a circular representation, we start front and read at zero. As an element is added, we increment the rear value. When one is deleted, we increment the front value. We need to make sure we “wrap” around the array, to give the illusion of a circle. CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Queue Operations CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Create Queue Creates an initialized head node for an empty queue CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Front Ptr count Rear Ptr No Queue ? before after CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Enqueue Inserts an element at the rear of the queue If queue is built as an array, enqueue could cause an OVERFLOW condition. CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Front Ptr count Rear Ptr Front Ptr count Rear Ptr 1 plum data next before after CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Front Ptr count Rear Ptr Front Ptr count Rear Ptr 2 1 plum apple plum data next data next data next before after CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Dequeue The data at the front of the queue are removed and returned to the user. Similar to “pop” from stacks Attempting to remove data from an empty queue results in an UNDERFLOW. CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Front Ptr count Rear Ptr Front Ptr count Rear Ptr 1 2 apple plum apple data next data next data next before after CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Destroy Queue Deletes all data from the queue, and returns all allocated memory to the heap CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Front Ptr count Rear Ptr No Queue 1 ? apple data next before after CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues “peekFront” This operation allows the program to view the data at the front of the queue, without destroying the state of the queue. CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues “peekRear” This operation, similar to “queue front”, allows the program to view the data at the rear of the queue. The state of the queue is not altered. CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Other Queue Methods Empty Queue - Is the queue empty? Return (Does queue->count equal zero?) Full Queue - Is the queue full? Allocate (tempPtr) if (allocation successful) recycle(tempPtr) return false else return true Old style allocations – Java takes care of most of this for us. CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Other Algorithms Queue count - how many elements are in the queue? Return queue->count CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Priority Queues Priority queues are simply partially sorted queues The order depends on the implementation Inserting and deleting are a little trickier, as you need to take order into account CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Circular Queue A Circular Queue is a “regular” queue, but the enqueue and the dequeue can wrap around ends of the array by using modulus operation. n n - 1 1 2 CIS265/506: Chapter 04 - Stacks and Queues
CIS265/506: Chapter 04 - Stacks and Queues Efficiency Efficient Array Implementation of a queue means making use of a circular queue. When enqueing, add an element to the end of the used elements in the array with modulus for wrap around. When dequeing, remove an element from the beginning of the used elements in the array. Doing this ensures enqueue and dequeue are both O(1) Doing it the other way can only be O(1) for one of those methods, the other is O(N) CIS265/506: Chapter 04 - Stacks and Queues