Stack & Queue
Stack is a linear data structure which follows a particular order in which the operations are performed.
The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out)
push(x) places the item x onto the top of the stack
pop() removes the top item from the stack, and returns that item.
Implement string stack using only string operations
const stack = new StringStack();
stack.push("one");
stack.push("two");
stack.push("three");
stack.pop() // three
Implement string stack using linked list
const stack = new Stack();
stack.push("one");
stack.push("two");
stack.push("three");
stack.pop() // three
Implement string stack using linked list
function validateBrackets(ext) {
...
}
validateBrackets("[(1 + 1)]^2"); // true
validateBrackets("(1 + [2 *) - 1]"); // false
validateBrackets("({[]})"); // true
Queue is a linear data structure where the first element is inserted from one end called REAR and deleted from the other end called as FRONT. (FIFO)
enqueue() | This function defines the operation for adding an element into queue. |
dequeue() | This function defines the operation for removing an element from queue. |
Pointers |
|
Front | Front is used to get the front data item from a queue. |
Rear | Rear is used to get the last item from a queue. |
Implement queue using linked list
1. Implement queue using stack API only
2. Implement stack using queue API only
Implement priority queue
const queue = new PriorityQueue();
queue.enqueue("one", 10);
queue.enqueue("two", 10);
queue.equeue("truee", 1);
queue.dequeue() // "three";
queue.dequeue() // "one"