Skip to content

Instantly share code, notes, and snippets.

@milon
Last active January 19, 2018 01:29
Show Gist options
  • Save milon/94f58a02ec0446e6e79c7f91b08d554d to your computer and use it in GitHub Desktop.
Save milon/94f58a02ec0446e6e79c7f91b08d554d to your computer and use it in GitHub Desktop.
Data Structure: Stack
//Stack
//Author: Milon
import java.util.NoSuchElementException;
//Node class
class Node{
//Instance variable
protected Object data;
private Node nextNode;
//Constructor
public Node(){
data = null;
nextNode = null;
}
public Node(Object item, Node next){
data = item;
nextNode = next;
}
//Set methods
public void setData(int item){
data = item;
}
public void setNextNode(Node next){
nextNode = next;
}
//Get methods
public Object getData(){
return data;
}
public Node getNextNode(){
return nextNode;
}
//Overloaded toString method
public String toString(){
return "" + data;
}
}
//Stack class
public class Stack{
//Instance variable
private Node top;
private int size;
//Constructor
public Stack(){
top = null;
size = 0;
}
//Returns is the stack empty
public boolean isEmpty(){
return top == null;
}
//Returns size of the stack
public int size(){
return size;
}
//Push method
public void push(Object item){
top = new Node(item, top);
++size;
}
//Pop method
public Object pop(){
if(!isEmpty()){
Node current = top;
top = top.getNextNode();
--size;
return current.getData();
}
else{
System.out.println("Stack underflow.");
//throw new NoSuchElementException();
return null;
}
}
//Peek method
public Object peek(){
if(!isEmpty())
return top.getData();
else{
System.out.println("Stack underflow.");
//throw new NoSuchElementException();
return null;
}
}
//Overloaded toString method to show the entire list
public String toString(){
String str = "[ ";
if(top != null){
str += top.getData();
Node current = top.getNextNode();
while(current.getNextNode() != null){
str += ", " + current.getData();
current = current.getNextNode();
}
str += ", " + current.getData();
}
str += " ]";
return str;
}
}
//Stack Implementation class
//Author: Milon
import java.util.Scanner;
import java.util.Random;
public class StackImplement{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
Random rand = new Random();
Stack stack = new Stack();
System.out.print("How many number of element do you want: ");
int n = input.nextInt();
for(int i=0;i<n;i++)
stack.push(new Integer(rand.nextInt(100)));
while(true){
System.out.println("\n\n");
System.out.println("* * * * * Stack Implementation * * * * *");
System.out.println("1\. Show the whole stack.");
System.out.println("2\. Show does the stack empty.");
System.out.println("3\. Show the top element.");
System.out.println("4\. Insert an element.");
System.out.println("5\. Delete an element.");
System.out.println("6\. Show the stack size.");
System.out.println("7\. Exit.");
System.out.println("\n\n");
System.out.print("Enter your choice: ");
int choice = input.nextInt();
switch(choice){
case 1:
System.out.println("The stack is: "+stack.toString());
break;
case 2:
if(stack.isEmpty())
System.out.println("The stack is empty.");
else
System.out.println("The stack is not empty.");
break;
case 3:
System.out.println("The top element is "+stack.peek());
break;
case 4:
System.out.print("Inter the element: ");
int item = input.nextInt();
stack.push(new Integer(item));
System.out.println("Item inserted successfully.");
break;
case 5:
System.out.println("Item "+stack.pop()+" deleted successfully.");
break;
case 6:
if(stack.isEmpty())
System.out.println("The stack is empty.");
else
System.out.println("Size of the stack is "+stack.size());
break;
case 7:
System.exit(1);
break;
default:
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment