Reading-Notes

View the Project on GitHub MohammadAl-khatib/Reading-Notes

Stacks and Queues

A stack is a set of nodes in which each node refrences the next node, pushing to stack means to add to it, while popping removes a node from the stack, peek in turn calls the top node in the stack.

Push

Pushing is always O(1) operation, because it just creates a new node and assign it as top node, the next to this new node is the old top

push node

Pop

Also an O(1), keep in mind that pop needs to return the popped node, so you have to store it before popping it, this is done by assiging the top to the next of current top, and putiing null as next of current (old) top. pop

Queue

It is the opposit (in terms of ordering) to stacks, it has its own terminology: