linkedstack.java

来自「真的很麻烦也」· Java 代码 · 共 62 行

JAVA
62
字号
public class LinkedStack implements Stack {  private Node top;		// reference to the head node  private int size;		// number of elements in the stack     public LinkedStack() {	// initializes an empty stack    top = null;    size = 0;  }  public int size() {    return size;  }  public boolean isEmpty() {    if (top == null)      return true;    return false;  }  public Object push(Object elem) {    Node v = new Node();	// create a new node    v.setElement(elem);    v.setNext(top);		// link-in the new node    top = v;    size++;    return ("-");  }  public Object top() //throws StackEmptyException   {    if (isEmpty())		{			return ("Stack is empty.");		}     // throw new StackEmptyException("Stack is empty.");    return top.getElement();  }  public Object pop() //throws StackEmptyException   {    if (isEmpty())    {		return ("Stack is empty.");	}    //  throw new StackEmptyException("Stack is empty.");    Object temp = top.getElement();    top = top.getNext();	// link-out the former top node    size--;    return temp;  }    public String getContent()	{		String cont= "";		if (size == 0)		 	return cont;		Node CurNode = top;		for (int i =1; i<=size; i++ )		{			cont += (CurNode.getElement() + ",");			CurNode = CurNode.getNext();		}				return (cont.substring(0,cont.length()-1));	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?