Friday, 10 July 2015

Stacks and Queues

https://github.com/prasanthlouis/C-equivalent-to-Cracking-the-Coding-Interview

So stacks are a kind of FIFO structure.

Similar to a stack of plates. The last plate that was placed on a pile of plates will be the first one to be taken out/used.

So when I was in school, we were taught to implement stacks as an array.

Again, after researching online. C++ has a built in stack class that allows you to manipulate stacks easily with built in functions.


Declaration

stack<date type> name;

It's similar to a vector, but there's no need to specify the size.

.push(element), allows you to push the element into the stack.

.pop(element) removes the top element.

.top() gives access to the top most element.

.empty() whether the stack is empty.

If only they taught all this in school. Sigh.


Queues are a FIFO structure. Like waiting in a line for a movie ticket. The people who've arrived earlier than others will get their tickets before them.

The syntax is pretty much the same, but instead of .top() you have the function .front()


Don't forget about the headers
#include<queue>
#include<stack>

No comments:

Post a Comment