jiloclassic.blogg.se

Java stack program code
Java stack program code









java stack program code

Look at the above figure to understand push and pop operations in java stack more clearly. Original elements of stack: Įlements of stack after removing: Įlements of stack after adding: ("Elements of stack after adding: " +st) ("Elements of stack after removing: " +st) Removing elements from the stack one by one. Using pop() method, we can pop an element from the stack in Java. ("Position of element 80: " +st.search(80)) ("Position of element 50: " +st.search(50)) Find the position of element into the stack. ("Element at the top of stack: " +peekElement) Retrieving element at the top of stack.

java stack program code

Create an empty stack that contains Integer objects. We will also search the position of an element into the stack using search() method. Using peek() method, we can retrieve or fetch an element at the top of stack without removing it. ("Elements of Stack: " +st) Įlements of Stack: Adds elements to the top of stack using push() method. Create an empty stack that contains String objects. The push() method places the element at the top of stack. Using push() method, we can add elements to the stack. Let’s take some example programs to perform the various operations based on the stack class methods in java. If the element is not present in the stack, it returns -1. int search(Object obj): This method returns the position of element obj from the top of stack. E push(E obj): This method pushes an element obj onto the top of the stack and returns that element (object).ĥ. E pop(): The pop() method is used to pop (remove) the top-most element from the stack and returns it.Ĥ. E peek(): This method is used to retrieve the top-most element from the stack without removing it.ģ. It returns true if and only if stack contains no elements (objects). boolean empty(): This method is used to check the stack is empty or not. In addition to methods that inherit from vector class, stack class has five additional methods of its own. Stack stack = new Stack() // It will store only Integer type objects. The general form to create a stack object is as follows: Stack stack = new Stack() Stack(): This constructor is used to create an empty stack object. Stack class has provided only one constructor that is as follows:ġ. Duplicate elements are allowed into the stack. Null elements are allowed into the stack.ħ. Pop operation removes an element from the stack and returns it.ĥ. Push operation inserts an element to the top of stack.Ĥ. In the Java stack, all operations take place at the top of the stack.ģ. Stack is a group of elements with “last-in, first-out” retrieval.Ģ. There are several features of stack in Java that are as follows:ġ. Here, E represents the type of elements that stack can hold. Stack is a generic class in java that has the following declaration in a general form: public class Stack The hierarchy diagram of stack in java is shown in the below figure. Stack class also implements Serializable and Cloneable interfaces. It implements List interface and RandomAccess interface. Java Stack class extends vector class that extends AbstractList class. ArrayDeque provides all the normal functionality of a stack and it is consistent with the JCF. Java API recommends using Java ArrayDeque class at the place of stacks. This class is now considered as legacy class in java because it is not consistent with Java Collections Framework. Stack class was introduced in Java 1.0 version. Similarly, a DVD disk holder where CDs are arranged in such that the last CD will be out first for use. A second real-time example of stack is a pile of plates in a cafeteria where last washed plate will be out first for use.ģ. We can not take a book from the stack without removing books that are first stacked on top of it.Ģ. A realtime example of stack is a stack of books as shown in the below figure. Let’s understand it with realtime examples to clear more. This fashion is called Last-In, First-Out data structure. When they are removed from the stack, we obtain elements C, B, and A fashion. A typical example of stack in java is shown in the below figure.Īs you can see in the above figure, we have added three elements A, B, and C into the stack. Therefore, stack is called last-in, first-out data structure. That is, new elements are added to the top of the stack, and elements are removed from the top of the stack. Insertion and deletion of elements take place only from one side of the stack, traditionally called the top of the stack.











Java stack program code