

The isEmpty () operation of the Stack class checks if the stack object is empty. In the above stack example, “intStack.peek ()” will return 200. The peek operation returns the Top of the stack without removing the element. The stack representation for push and pop operation is as follows: The variable val will contain the value 200 as it was the last element pushed into the stack. Stack intStack = new Stack() intStack.push(100) intStack.push(200) int val = intStack.pop()
#Stacks on main code
The following piece of code achieves this. The element pointed by the Top at present is popped off the stack. We can remove the element from the stack using the “pop” operation. If we perform another push() operation as shown below, push(25) The initial stack obtained as a result of the above piece of code execution is shown below:

Stack myStack = new Stack() myStack.push(10) myStack.push(15) myStack.push(20) The following piece of code is used to initialize an integer stack with the values. Once we create a stack instance, we can use the push operation to add the elements of the stack object type to the stack. The push operation is used to push or add elements into the stack. We will discuss these methods in the below section.

It also provides a method to check if the stack is empty. The Stack class provides methods to add, remove, and search data in the Stack. Stack stack_obj = new Stack() Stack str_stack = new Stack() Stack API Methods In Java Here data_type can be any valid data type in Java.įor example, we can create the following Stack class objects. We can also create a generic type of Stack class object as follows: Stack myStack = new Stack Once we import the Stack class, we can create a Stack object as shown below: Stack mystack = new Stack() To include Stack class in the program, we can use the import statement as follows. The Stack class is a part of java.util package. The below diagram shows the hierarchy of the Stack class.Īs shown in the above diagram, the Stack class inherits the Vector class which in turn implements the List Interface of Collection interface. This Stack class extends the Vector class and implements the functionality of the Stack data structure. Java Collection Framework provides a class named “Stack”. If the size of the stack is N, then the top of the stack will have the following values at different conditions depending on what state the stack is in. The top of the stack that is used as an end to add/remove elements from the stack can also have various values at a particular instant. Peek: This operation is used to look up or search for an element.After the pop operation, the value of the top is decremented. Pop: An element is removed from the stack.As a result, the value of the top is incremented. An element currently pointed to ‘Top’ is removed by the pop operation.Ī stack data structure supports the following operations: This operation is used to remove an element from the stack. In the last representation, we initiate a “pop” operation. We again push element 20 in the stack thereby incrementing the top furthermore. So in the second representation, we push element 10. Then we initiate a “push” operation that is used to add an element to the stack. Stack Implementation In Java Using ArrayĪ pictorial representation of the stack is given below.Īs shown in the above sequence of representation, initially the stack is empty and the top of the stack is set to -1.
