Arithmetic expressions can be evaluated using two stacks - one for operands and other for numeric values.
Algorithm:
If element is numeric, push to numeric stack.
If element is operand, push to operand stack.
If element is "(", ignore.
If element is ")", pop operand from operand stack and pop two numerics from numeric stack - perform the operation and push the result back into numeric stack.
Queues follow FIFO standard. To implement, we need to maintain two indices to first and last elements of the array, for enqueue and dequeue operations.
When implementing stacks using arrays, we may run into overflow and underflow problems. We can resize the array on need basis by doubling the array in push op when the array has reached it's limit and by halving the array in pop op when the array is just 25% utilized.
This will result in constant amortized time operations, but ~N in worst case.
Linked list on the other hand has constant time even in worst case. But still, linked list may take more time and memory because of the links.
If occasional slow ops are bearable, then arrays would be a better option than linked lists.
Stack works in a LIFO order. It supports insert, remove, iteration and test for empty operations.
They can be represented using array. An initial capacity for this array is taken as input. We manipulate an index variable for push and pop, instead of actually deleting items from stack.
If using objects in stack, avoid loitering(holding references to objects and preventing GC) by using following code in pop operation: