Java Stack

  1. Stack - Push (Insert) method in Java
  2. Stack - Push & Pop (Delete) methods in Java
  3. Stack - Push & Peek (Select) methods in Java
  4. Stack - Push, Pop & Peek methods in Java
  5. Insert an element into the Stack using Index method in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Stack - Push (Insert) method in Java


Stack_Ex1.java

import java.util.Scanner;
import java.util.Stack;
 
public class Stack_Ex1 {
	
	Scanner scan;
	Stack<String> stack;
	
	int n;
 
	void push() {
		
		scan = span class="keyword">new Scanner(System.in);
		stack = new Stack<String>();
		
		System.out.println("Stack Push (Insert)");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the data - PUSH");
 
		for(int i=0; i<n; i++) {
			
			stack.push(scan.next());
		}
	}
	
	void display() {
		
		System.out.println("\nThe Stack");
 
		for(int i=0; i<stack.size(); i++) {
			
			System.out.println(stack.get(i));
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Stack_Ex1 obj = new Stack_Ex1();
		
		obj.push();
		obj.display();
	}
}

Sample Output

Stack Push (Insert)

Enter 'n' value :
3
Enter the data - PUSH
Vinoth
Mani
Boopalan

The Stack
Vinoth
Mani
Boopalan




B. Stack - Push & Pop (Delete) methods in Java


Stack_Ex2.java

import java.util.Scanner;
import java.util.Stack;
 
public class Stack_Ex2 {
	
	Scanner scan;
	Stack<String> stack;
	
	int n;
 
	void push() {
		
		scan = new Scanner(System.in);
		stack = new Stack<String>();
		
		System.out.println("Stack Push & Pop (Delete)");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the data - PUSH");
 
		for(int i=0; i<n; i++) {
			
			stack.push(scan.next());
		}
	}
	
	void pop() {
		
		System.out.println("\nThe Stack - POP");
 
		while(!stack.empty()) {
			
			System.out.println(stack.pop());
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Stack_Ex2 obj = new Stack_Ex2();
		
		obj.push();
		obj.pop();
	}
}

Sample Output

Stack Push & Pop (Delete)

Enter 'n' value :
3
Enter the data - PUSH
1
2
3

The Stack - POP
3
2
1




C. Stack - Push & Peek (Select) methods in Java


Stack_Ex3.java

import java.util.Scanner;
import java.util.Stack;
 
public class Stack_Ex3 {
	
	Scanner scan;
	Stack<String> stack;
	
	int n;
 
	void push() {
		
		scan = new Scanner(System.in);
		stack = new Stack<String>();
		
		System.out.println("Stack Push & Peek (Select)");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the data - PUSH");
 
		for(int i=0; i<n; i++) {
			
			stack.push(scan.next());
		}
	}
	
	void peek() {
		
		System.out.println("\nTop Value of the Stack - PEEK");
			
		System.out.println(stack.peek());
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Stack_Ex3 obj = new Stack_Ex3();
		
		obj.push();
		obj.peek();
	}
}

Sample Output

Stack Push & Peek (Select)

Enter 'n' value :
3
Enter the data - PUSH
3
6
9

Top Value of the Stack - PEEK
9




D. Stack - Push, Pop & Peek methods in Java


Stack_Ex4.java

import java.util.Scanner;
import java.util.Stack;
 
public class Stack_Ex4 {
	
	Scanner scan;
	Stack<String> stack;
	
	int n;
 
	void push() {
		
		scan = new Scanner(System.in);
		stack = new Stack<String>();
		
		System.out.println("Stack Push, Pop & Peek");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the data - PUSH");
 
		for(int i=0; i<n; i++) {
			
			stack.push(scan.next());
		}
	}
	
	void peek() {
		
		System.out.println("\nTop Value of the Stack - PEEK");
			
		if(stack.isEmpty())
			System.out.println("The Stack is Empty..");
 
		else
			System.out.println(stack.peek());
	}
	
	void pop() {
		
		System.out.println("\nThe Stack - POP");
		
		while(!stack.empty()) {
			
			System.out.println(stack.pop());
		}
		
		peek();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Stack_Ex4 obj = new Stack_Ex4();
		
		obj.push();
		obj.peek();
		obj.pop();
	}
}

Sample Output

Stack Push, Pop & Peek

Enter 'n' value :
3
Enter the data - PUSH
2
5
8

Top Value of the Stack - PEEK
8

The Stack - POP
8
5
2

Top Value of the Stack - PEEK
The Stack is Empty..




E. Insert an element into the Stack using Index method in Java


Stack_Ex5.java

import java.util.Scanner;
import java.util.Stack;
 
public class Stack_Ex5 {
	
	Scanner scan;
	Stack<String> stack;
	
	int n;
 
	void push() {
		
		scan = new Scanner(System.in);
		stack = new Stack<String>();
		
		System.out.println("Stack - Insertion using Index");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the data - PUSH");
 
		for(int i=0; i<n; i++) {
			
			stack.push(scan.next());
		}
	}
	
	void display() {
		
		System.out.println("\nThe Stack");
			
		for(int i=0; i<stack.size(); i++) {
			
			System.out.println(stack.get(i));
		}
	}
	
	void AddIndex() {
		
		System.out.println("\nInsertion using Index");
		
		System.out.println("Enter the index :");
		int index = scan.nextInt();
		
		System.out.println("Enter the Element :");
		String element = scan.next();
		
		stack.add(index - 1, element);
 
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Stack_Ex5 obj = new Stack_Ex5();
		
		obj.push();
		obj.display();
		obj.AddIndex();
	}
}

Sample Output

Stack - Insertion using Index

Enter 'n' value :
3
Enter the data - PUSH
1
2
3

The Stack
1
2
3

Insertion using Index
Enter the index :
2
Enter the Element :
0

The Stack
1
0
2
3






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com