Bag,queue, and stack API s:
You can notice two places, one is Item and the other is iterable. That's generics, and iterable collections.
Generics
When implementing generic collections, we don't know the specific type of Item. We only determine when we create objects. Once the type is determined, adding inconsistent types will cause compilation errors.
Stack<String> stack = new Stack<String>();
stack.push("Test");
Automatic packing and unpacking
Since Item can only be a reference type, how does the native type interact with it? The answer is auto-boxing and auto-unboxing.
Stack<Integer> stack = new Stack<Integer>();
stack.push(17); //Automatic boxing
int i = stack.pop(); //Auto-unboxing
Iterable Collection
A Sharp Tool for Implementing for-each Cycle
Queue<Transaction> collection = new Queue<Transaction>();
//process with collection
for(Transaction t : collection){
//process with t
}
Bags
Deletion of elements is not supported. Used to collect elements and traverse and process them. Especially for operations that are not sequential in the processing of elements. For example, find the average of a group of numbers
Queue (FIFO)
The use is to preserve elements while maintaining their relative order. When using for-each operation, the order is first in first out
Stack (LIFO)
When using for-each operation, the order is first in first out
Arithmetic expression evaluation
Given a series of expressions, how to write the program to calculate the value? For example:
(1 + ((2 + 3) * (4 * 5)))
Dijkstra uses two stack s to compute the algorithm steps as follows:
Processing strings from left to right is as follows: 1. Encountering operands and putting them on the operand stack 2. Enter operator stack 3. When encountering left parentheses, ignore 4. encounter right parentheses, out-of-stack operators, out-of-stack operands (twice, first get a, then get b), calculate the results, into the operand stack Until you encounter the last right parenthesis, there is only one element at the top of the operand stack, returned
Implementation of Stack
(Stop lazy, go directly to stack code, the original book on stack construction is very exciting, recommend a look):
Links:
https://algs4.cs.princeton.edu/13stacks/ResizingArrayStack.java.html
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayStack<Item> implements Iterable<Item> {
private Item[] a; // array of items
private int n; // number of elements on stack
/**
* Initializes an empty stack.
*/
public ResizingArrayStack() {
a = (Item[]) new Object[2];
n = 0;
}
/**
* Is this stack empty?
* @return true if this stack is empty; false otherwise
*/
public boolean isEmpty() {
return n == 0;
}
/**
* Returns the number of items in the stack.
* @return the number of items in the stack
*/
public int size() {
return n;
}
// resize the underlying array holding the elements
private void resize(int capacity) {
assert capacity >= n;
// textbook implementation
Item[] temp = (Item[]) new Object[capacity];
for (int i = 0; i < n; i++) {
temp[i] = a[i];
}
a = temp;
// alternative implementation
// a = java.util.Arrays.copyOf(a, capacity);
}
/**
* Adds the item to this stack.
* @param item the item to add
*/
public void push(Item item) {
if (n == a.length) resize(2*a.length); // double size of array if necessary
a[n++] = item; // add item
}
/**
* Removes and returns the item most recently added to this stack.
* @return the item most recently added
* @throws java.util.NoSuchElementException if this stack is empty
*/
public Item pop() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Item item = a[n-1];
a[n-1] = null; // to avoid loitering
n--;
// shrink size of array if necessary
if (n > 0 && n == a.length/4) resize(a.length/2);
return item;
}
/**
* Returns (but does not remove) the item most recently added to this stack.
* @return the item most recently added to this stack
* @throws java.util.NoSuchElementException if this stack is empty
*/
public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return a[n-1];
}
/**
* Returns an iterator to this stack that iterates through the items in LIFO order.
* @return an iterator to this stack that iterates through the items in LIFO order.
*/
public Iterator<Item> iterator() {
return new ReverseArrayIterator();
}
// an iterator, doesn't implement remove() since it's optional
private class ReverseArrayIterator implements Iterator<Item> {
private int i;
public ReverseArrayIterator() {
i = n-1;
}
public boolean hasNext() {
return i >= 0;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
return a[i--];
}
}
}
(~Continuous update)