One of the ways of representing a collection of data is through stack. Stack is a data structure that follows the "LIFO" concept, which means last in, first out. This means that the recently inserted value or object is the first to be removed. "Stack" is used as a term in programming in reference to the concept of stacking objects on top of each other, and if a person removes something from the stack, it would be the topmost object in the stack. An example would be plates being stacked on top of each other. With this concept, it also follows the "LIFO" principle, hence the name of "Stack" used in programming. An example of the stacks data structure application is in Web browsers in reference to the last visited page by the user. The site and page address are "pushed" into the stack of addresses, and once the user decides to go back, it kind of performs "popping," wherein the last visited site will be seen. The "pushing" and "popping" terms will be explained in the next paragraph.

The fundamental operations to be used in Stacks are "pushing" and "popping". Pushing is the adding of elements in the stack while popping is the removal of the topmost element in the stack. The methods to be used to implement these are push(x), where x is the element to be inserted for pushing, and pop() for popping. Other most used methods in stack are size(), isEmpty(), and top() or peek(). For the method size(), it determines the number of elements inside the stack, which is its size. For isEmpty(), this is a boolean method wherein it checks the stack if it is empty or not, returns true if it is empty, and false if the stack has objects. The methods top() or peek() have the same function. Their function is to return the topmost element in the stack without removing the element. If there are no elements, then the program will give an error once run.
With the importance of stack data structure, there is a built-in class in Java called the java.util.Stack. Some of the methods included in this class are push(), pop(), peek() (same function as top()), and empty() (same function as isEmpty()). Although using the Stack class of Java is convenient, learning the design and implementation of stacks from the basics is much better, especially if the programmer is still learning or a beginner. This is an Abstract Data Type (ADT) to be implemented in the chosen Java Application Programming Interface (API). This is just a guide on how to implement the actual codes for the stacks. It serves as an outline.
Part of the implementation of stacks is the importance of defining the exceptions for any possible errors that may arise from the methods used. In this case, calling the methods top() or pop() on an empty stack has to throw an exception called EmptyStackException. This is due to the fact that calling these methods on an empty stack will be useless as there are no values that can be "popped" or can be viewed. In a similar way, calling the method push(x), where x is the element to be pushed into the stack on a full stack, should also throw an exception called FullStackException. This is also due to the fact the values to be pushed into the stack have reached their limit. This is an important concept of stacks wherein there is a limit to how many values can be inserted into the stack. Therefore, it is important for the programmer to define the appropriate limit of the stack based on what the program needs.
Overall, stacks are significant data structures in programming. They play a massive role in the collection of data, making it one of the ideal ways of representing these data.
Posted using Honouree