Chained stack - Java implementation

package struct;

//interface
interface ILinkStack{
	//Number of elements in the stack (stack size)
	int size();
	// get the top element of the stack
	Object top();
	//Check if the stack is empty
	boolean isEmpty();
	// push the stack
	Object pop();
	// pop the stack
	Object push(Object value);
	// clear stack
	void clear();
}

//factory class
class Factory2{
	private Factory2(){}
	public static ILinkStack getILinkStackInstance(){
		return new LinkStackImpl();
	}
}

class LinkStackImpl implements ILinkStack {
	// top element of stack
	Node top;
	//The length of the linked list records the elements on the stack
	private int count;
	class Node{
		Node prev;
		Node next;
		Object data;
		public Node(Object data) {
			this.data = data;
		}
	}
	
	public int size() {
		return count;
	}

	public Object top() {
		return top.data;
	}
	// judge the stack is empty
	public boolean isEmpty() {
		return (size()==0);
	}
	// push the stack
	public Object push(Object value) {
		Node node = new Node(value);
		if(top == null){
			top = node;
		}else{
			top.next = node;
			node.prev = top;
			top = top.next;
		}
		count++;
		return top;
	}
	public void print(){
		System.out.println("Print elements on the stack from the top of the stack to the bottom of the stack: ");
		myPrint(top);
		return;
	}
	//Stack top -> stack bottom print linked list
	private void myPrint(Node top){
		for(Node node = top;node!=null;node=node.prev){
			System.out.print(node.data+" ");
		}
	}
	// pop the stack
	public Object pop() {
		Node node = top;
		if(top == null){
			System.out.println("No elements to pop out of the empty stack");
			return -1;
		}else{
			top = top.prev;
			node.prev = null;
			node.data = null;
		}
		count--;
		return top();
	}
	// clear stack
	public void clear(){
		Node node1 = top;
		for(Node node = top;node!=null;){
			node = node.prev;
			node1.data = null;
			node1.prev = null;
			count--;
		}
	}
}
public class LinkStack {
	public static void main(String[] args) {
		ILinkStack linkStack = Factory2.getILinkStackInstance();
		// downcast
		LinkStackImpl linkStack1 = (LinkStackImpl) linkStack;
		System.out.println("============Test isEmpty function (empty stack)===================");
		System.out.println(linkStack.isEmpty());
		System.out.println("============Test push and print functions=================");
		linkStack.push(5);
		linkStack.push(9);
		linkStack.push(10);
		linkStack.push(1);
		linkStack.push(8);
		linkStack.push(12);
		linkStack.push(6);
		linkStack.push(3);
		linkStack1.print();
		System.out.println();
		System.out.println("============Test top function after pushing into the stack=================");
		System.out.println(linkStack.top());
		System.out.println("============Test the size function after pushing into the stack=================");
		System.out.println(linkStack.size());
		System.out.println("============Test pop and print functions=================");
		linkStack.pop();
		linkStack.pop();
		linkStack.pop();
		linkStack1.print();
		System.out.println();
		System.out.println("============Test top function after outbound=================");
		System.out.println(linkStack.top());
		System.out.println("============Test the size function after popping the stack=================");
		System.out.println(linkStack.size());
		System.out.println("============size function after test clear=================");
		linkStack.clear();
		System.out.println(linkStack.size());
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986620&siteId=291194637