Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Stack

Declaration

class Stack<E>

Stack extends the Vector class.

It has only one constructor.

List of Stack Constructors (CLICK HERE)

List of Stack Methods (CLICK HERE)

Non Generic Demo

In non-generic demo, we have used non-generic Stack class, constructor & methods. Therefore it can contain any type of elements (heterogeneous elements) as shown in program below:

Example

 import java.util.Stack;

 public class StackNonGenDemo 
 {
     public static void main(String args[])
     {
         Stack s = new Stack();
         System.out.println("Default Stack :: "+s);
         s.push(10); // Integer added
         s.push(20); // Integer added
         s.push('A'); // Character added
         s.push("XYZ"); // String added
         System.out.println("Stack :: "+s);
     }
 }

Output

Default Stack :: []
Stack :: [10, 20, A, XYZ]

If we want Stack must contain elements of same type then we must use generic Stack.

Generic Demo

In the program below we have used generic Stack that can contain only Integer type of elements.
If we tried to insert element other than Integer, we will get compile time error.

Example – I

 import java.util.Stack;

 public class StackGenDemo1 
 {
     public static void main(String args[])
     {
         Stack<Integer> s = new Stack<Integer>();
         s.push(10);
         s.push(20);
         s.push(30);
         s.push(40);
     //  s.push('X'); // The method push(Integer) 
		         // in the type Stack<Integer> 
		         // is not applicable 
		         // for the arguments (char)
         System.out.println(s);
     }
 }

Output

[10, 20, 30, 40]

Example – II

 import java.util.Stack;

 public class StackGenDemo2 
 {
     public static void main(String args[])
     {
         Stack<Integer> s = new Stack<Integer>();
         System.out.println("Default Stack :: "+s);
         System.out.println("Is stack empty? :: "+s.empty());
         s.push(100);
         s.push(200);
         s.push(300);
         s.push(400);
         System.out.println("Stack :: "+s);
         System.out.println("Is stack empty? :: "+s.empty());
         System.out.println("Element at peek in stack :: "+s.peek());
         s.pop();
         System.out.println("Stack :: "+s);
         s.pop();
         System.out.println("Stack :: "+s);
     }
 }

Output

Default Stack :: []
Is stack empty? :: true
Stack :: [100, 200, 300, 400]
Is stack empty? :: false
Element at peek in stack :: 400
Stack :: [100, 200, 300]
Stack :: [100, 200]

Example – III

 import java.util.Stack;

 class Employee 
 {
     int emp_id;
     String name;
     double salary;
	
     Employee(int i, String s, double d)
     {
         emp_id = i;
         name = s;
         salary = d;
     }
 }

 class StackGenDemo3 
 {
     public static void main(String args[])
     {
         Employee e1 = new Employee(20,"DAVID", 65000.98);
         Employee e2 = new Employee(50,"JAMES", 78000.69);
         Employee e3 = new Employee(10,"RICKY", 45987.45);
         Employee e4 = new Employee(90,"JACK", 130006.68);

         Stack<Employee> s = new Stack<Employee>();
         s.push(e1);
         s.push(e2);
         s.push(e3);
         s.push(e4);
		
         for(Employee e:s)
         {
             System.out.println(e.emp_id+"  "+e.name+"  "+e.salary);
         }
		
         s.pop();
         s.pop();
		
         System.out.println("\n After Pop :: \n");
		
         for(Employee e:s)
         {
             System.out.println(e.emp_id+"  "+e.name+"  "+e.salary);
         }
     }
 }

Output

20 DAVID 65000.98
50 JAMES 78000.69
10 RICKY 45987.45
90 JACK 130006.68
After Pop ::
20 DAVID 65000.98
50 JAMES 78000.69

 

Leave a reply