A sequential data structure that resembles a stack. Imagine a stack of plates: you would put one on top of the other and you would remove plates by removing the ones on top first. So basically: last in first out
Also a sequential data structure, just like the name suggests it is just line that earlier data would be removed first. So: first in first out
Although it is unlikely that you would have to implement these when you are coding for work or personal projects because Array can pretty much do all things Stack and Queue
During interviews, interviewers might still asks you to re-implement them to see if you know common data structures and algorithms.
Requirements:
- both would need the following properties:
- implement them without using Arrays
for stack: insert, remove, peak, for queue: enqueue, dequeue, peek,
for both: length
Write a function that takes a string checks and see if all the parentheses match correctly
Ex:
balancedParens('(hi)');
// returns true
balancedParens('}nope{');
// returns false
Write a method that returns the current minimum value of a stack without going through the entire stack.
var stack = new Stack();
stack.insert(4);
stack.insert(8);
stack.currentMin();
// returns 4;
stack.insert(1);
stack.currentMin();
// return 1;
stack.remove();
stack.remove();
// both 1 and 8 are removed
stack.currentMin();
// returns 4;
Define a Queue class by using at most two stacks Design an experiment to test the performance of your implementations
Check out linked lists, and reimplement the queue again using this type of data structure (link), and compare the performance with your earlier implementations.