Stacks and Queues

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Stacks and queues:

Where do we store our data?


- When we program, we use computer memory to store variables, functions etc
- In terms of performance, we want to utilize the fastest memory possible to ensure quick,
stable, compilation times
- CPU registers are small, fast storage locations stored within the CPU
- We ideally want to store data in CPU registers, but as they are so small, we need an
alternative for larger amount of data

The programming stack:


- Whilst the stack exists as a data structure, it is also a region of memory for managing program
execution

- The stack has two main purposes:

o Keep track of a function that should return to the main once it finishes
o Hold the values of local variables (variables that aren’t needed once the containing
function finishes)

Stacks:
- Think of a stack like a deck of cards in uno- you only take the cards from the top
- Stacks use a first in last out system, which means that the most recent item added to the stack
is the only one that can be accessed and removed

The programming stack:


- As local variables are declared they’re added onto the stack
- The local variables within a specific function are grouped together in a stack frame
- When the function finishes, the stack frame can be quickly removed
- Due to the LIFO way that stacks store data, the most recent method is at the top, with its local
variables being in scope

The heap?
- Whereas the stack allows items to be added/removed from the top, any item in a heap can be
accessed at any time
- The heap stores data that needs to keep on existing after a function has ended, e.g., such as
objects or global variables
- We have only looked at stack allocation so far, dynamic allocation (where we use the Heap),
is something we will return to later on!

Stacks:
- .push(element) is the command that is used within a stack to add a new element at the top of
the stack.
- .pop() is the command that is used to remove the topmost element of a stack
- You can get the topmost element of a stack using .top()
- Stacks also have the .empty() method, which allows you to check whether it is empty
- You can also use the .size() method, which returns how many elements that are currently in
the stack
Why use stacks>
- The biggest advantage of stacks is the efficiency. It only takes O(1) to use any method in a
stack, which means it the amount of time it takes to access doesn’t scale with the number of
elements stored
- Stacks allow for easy memory management, as you can only add one item at a time
- Stacks become slow when lots of data items are used due to this
- You can only access the first element in a stack, you can’t access any others

Queues:
- A queue is very similar to a stack, but instead of using First In Last Out, it uses First In First
Out.
- Think of queue like a queue! You stand at the back, wait your turn, until the person in front of
you has gone, and then it’s your turn to go!
- It’s the same principle with queues, but for data elements. The first element inserted is always
the first one out of the queue, whereas the last element inserted is the last one out of the queue
- This is inverse to stacks where the first element entered was always the last one out of the
stack

Queue vs stacks:
- Stacks only maintain one pointer- used to access the last element present in the stack. (the
top)
- Queues use two pointers- one used to access the first element inserted into the queue, and the
rear pointer, which points to the most recent element entered into the queue
- For both of the data types, the methods used to add, remove and access data is O(1), which
makes the, fast for small amounts of data

Queues:
- The part where you join the queue is called Enqeue, whereas the part where you have to leave
the queue is Dequeue.
- .push (“element”) will add a new data element to the queue
- .push() will remove the foremost data element from the queue
- .front() will check the element at the front
- .size() will check how many elements are in the queue
- .empty() will check if the queue has no data elements in it
Priority queues:
- A priority queue is a type of queue where each data element has a priority, and elements are
served based on their priority.
- Think of it as the entrance to a nightclub. The average person will queue but if a VIO or
celebrity guest turns up they get to jump the queue.
- Priority queues can be created and used in a similar way to a normal queue. The main
difference being when they are initialized:
- Priority_queue<int>pqlnts;
- To add and remove elements, you simply use push and pop- the same as when using a normal
queue
- The only other difference is that instead of accessing a value using the front command, we go
back to the top() command used in stacks.
-

You might also like