In this post I have implemented Stack using Java Generics and the LinkedList class. This post isn't a tutorial rather an implementation of Stack Data Structure.
Code for Stack Interface : StackInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codehackersblog.JStack; | |
/** | |
* | |
* @author Psycho_Coder | |
* @param <T> | |
*/ | |
public interface StackInterface<T> { | |
/** | |
* | |
* @param item This method push the item passed into the stack | |
*/ | |
public abstract void push(T item); | |
/** | |
* | |
* @return This method removes the top element from the stack | |
*/ | |
public abstract T pop(); | |
/** | |
* | |
* @return This method returns whether the stack is empty or not | |
*/ | |
public abstract boolean isEmpty(); | |
/** | |
* | |
* @return This Methods returns the top element of the stack | |
*/ | |
public abstract T peek(); | |
/** | |
* | |
* @return This method returns the size of the stack | |
*/ | |
public abstract int size(); | |
/** | |
* | |
* @return This method returns the array of the items in the stack | |
*/ | |
public abstract String display(); | |
} |
Code for Stack Interface : JStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codehackersblog.JStack; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
/** | |
* | |
* @author Psycho_Coder | |
* @param <T> | |
*/ | |
public class JStack<T> implements StackInterface<T> { | |
private LinkedList<T> list; | |
public JStack() { | |
this.list = new LinkedList<>(); | |
} | |
/* | |
* @see com.psychocoder.JStack.StackInterface#push(java.lang.Object) | |
*/ | |
@Override | |
public synchronized void push(T item) { | |
list.addFirst(item); | |
} | |
/* | |
* @see com.psychocoder.JStack.StackInterface#pop() | |
*/ | |
@Override | |
public synchronized T pop() { | |
return list.removeFirst(); | |
} | |
/* | |
* @see com.psychocoder.JStack.StackInterface#isEmpty() | |
*/ | |
@Override | |
public boolean isEmpty() { | |
return list.isEmpty(); | |
} | |
/* | |
* @see com.psychocoder.JStack.StackInterface#peek() | |
*/ | |
@Override | |
public synchronized T peek() { | |
return list.getFirst(); | |
} | |
/* | |
* @see com.psychocoder.JStack.StackInterface#size() | |
*/ | |
@Override | |
public int size() { | |
return list.size(); | |
} | |
/* | |
* @see com.psychocoder.JStack.StackInterface#display() | |
*/ | |
@Override | |
public String display() { | |
String m = ""; | |
for (Iterator<T> it = list.iterator(); it.hasNext();) { | |
m = m + it.next() + " "; | |
} | |
return m; | |
} | |
/** | |
* @returns An array of all the element in the stack | |
*/ | |
public Object[] elements() { | |
return list.toArray(); | |
} | |
} |
Demo Run of Above Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.codehackersblog.JStack.JStack; | |
/** | |
* | |
* @author psychocoder | |
*/ | |
public class JStackDemo { | |
public static void main(String[] args) { | |
JStack<String> jstack = new JStack(); | |
jstack.push("1"); | |
jstack.push("2"); | |
jstack.push("3"); | |
jstack.push("4"); | |
System.out.println("Print All Elements in Stack : \n"); | |
for (Object m : jstack.elements()) { | |
System.out.println(m.toString()); | |
} | |
jstack.pop(); | |
jstack.pop(); | |
System.out.println("\nAll Elements in Stack are : \n" + jstack.display()); | |
System.out.println("\nTop Element : " + jstack.peek()); | |
jstack.push("3"); | |
jstack.push("4"); | |
System.out.println("\nSize of Stack : " + jstack.size()); | |
} | |
} |
Print All Elements in Stack : 4 3 2 1 All Elements in Stack are : 2 1 Top Element : 2 Size of Stack : 4