Mar 13, 2017 05:58 AM
(This post was last modified: Mar 20, 2017 09:39 AM by c-_-m.)
Stacks
So basically while going through data structures and algorithms on my course I decided I would try posting the concepts I am learning here in hopes that people can find them useful and also to cement my understanding by explaining them to you guys. I've got a whole semester worth of material so if you enjoy this one let me know and I can post more ✌
Remember this is a course so ideas are gradually introduced and built upon throughout
Next Tut on LinkedLists HERE
So basically while going through data structures and algorithms on my course I decided I would try posting the concepts I am learning here in hopes that people can find them useful and also to cement my understanding by explaining them to you guys. I've got a whole semester worth of material so if you enjoy this one let me know and I can post more ✌
Remember this is a course so ideas are gradually introduced and built upon throughout
Next Tut on LinkedLists HERE
The Stack ADT
The stack ADT(Abstract Data Type) is one of the simplest ADT. In a stack you are only able to add and remove elements at the top of the stack.
The stack operates on a last in first out structure or LIFO meaning that the last element that is added to the stack is also the first to leave the stack. Pretty simple right?
The stacks main operations are:
- push(object) - This adds an object to the top of the stack.
- pop() - This removes and returns the element at the top
Some auxiliary operations are
- top() - return the element at the top(also called peep())
- size() - returns the number of elements
- isEmpty() - checks to see if it's empty
The error conditions would be if we were trying to pop() or top() an empty stack.
Illustration
(Image taken from online)Examples
To give you an example of what stacks are used we have some real world examples e.g
- paper in a printer
- Magazines stacked on your desk
- Pizza boxes stacked in your kitchen cause you're too lazy to bin em
- The undo button
- pages visited in your browser
- even calling methods in your java programs
Implementation of the Stack ADT
For the purpose of part one we will implement the stack ADT using arrays, but there are many ways to implement a stack.
The downside to using an array is that the size is fixed so we are bound by that.
This also means we must handle the error if pushing to a full array. This is done by throwing FullStackException:
Code
public void push(Object element)throws FullStackExceptionI'll now give code for a stack using a fixed-length array as described.
This shows the basic methods in code form, I hope you enjoyed this tutorial, leave your feedback and remember I've got plenty more material so let me know if interested or if I should go more indepth on the ideas 🤝
--_--


![[Image: eBxEg.gif]](https://i.imgur.com/eBxEg.gif)

![[Image: BlUH1Un.png]](https://i.imgur.com/BlUH1Un.png)