Implement a stack using linked list - Java:
import java.util.LinkedList;
/**
* This class implements a stack using linked list.
* @author
* @version 1.0
*/
public class Stack {
/**
* This is the main method.
* @param args String[]
*/
public static void main(String[] args) {
//Create a stack object
Stack stack = new Stack();
//Push elements to the stack
stack.push(1);
stack.push(2);
stack.push(3);
//Print the elements in the stack
System.out.println("Elements in the stack are: ");
stack.printStack();
//Pop elements from the stack
System.out.println("\nPopped element is: " + stack.pop());
System.out.println("\nPopped element is: " + stack.pop());
System.out.println("\nPopped element is: " + stack.pop());
}
/**
* This is the constructor of the class Stack.
*/
public Stack() {
//Create a linked list object to store the elements of the stack
list = new LinkedList<Integer>();
}
/**
* This method pushes an element to the top of the stack.
* @param element int to be pushed to the top of the stack.
*/
public void push(int element) {
//Add the element to the top of the stack
list.addFirst(element);
}
/**
* This method pops an element from the top of the stack.
* @return int Obtains the element from the top of the stack.
*/
public int pop() {
//Variable to store the popped element from the top of the stack
int poppedElement = 0;
//Check if the stack is empty or not
if (list.isEmpty()) {
System.out.println("Stack is empty");
//Return 0 if the stack is empty else return the popped element from the top of the stack
return poppedElement;
} else {
//Remove and return the element from the top of the stack
poppedElement = list.removeFirst();
return poppedElement;
}
}
/**
* This method prints all elements of the stack.
*/
public void printStack() {
//Check if the stack is empty or not
if (list.isEmpty()) {
System.out.println("Stack is empty");
} else {
//Print all elements of the stack
System.out.println(list);
}
}
/** LinkedList object to store elements of the stack */ private LinkedList<Integer> list;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
/** */ private static final long serialVersionUID = 1L;
/** */ private static final int serialVersion = 1;
Top comments (0)