📄 linkedstack.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -