代码拉取完成,页面将自动刷新
package wek1;
public class LinkedStack<T> implements StackADT<T> {
//在栈中储存元素的数量。
private int count;
//指向栈顶的指针。
private LinearNode<T> top;
public LinkedStack(){
count = 0;
top = null;
}
/*Adds the specified element to the top of this stack.
* @param element element to be pushed on stack*/
@Override
public void push(T element) {
LinearNode<T> temp = new LinearNode<T>(element);
temp.setNext(top);
top = temp;
count++;
}
/*Removes the element at the top of this stack and returns a
* reference to it.Throws an EmptyCollectionException if the stack
* is Empty.
* @return T element from top of stack
* @throw EmptyCollection on pop from empty stack*/
@Override
public T pop() throws EmptyCollectionException
{
if(isEmpty()) {
throw new EmptyCollectionException("Stack");
}
T result = top.getElement();
top = top.getNext();
count--;
return result;
}
@Override
public T peek() throws EmptyCollectionException{
if(isEmpty()) {
throw new EmptyCollectionException("Stack");
}
T result = top.getElement();
return result;
}
@Override
public boolean isEmpty() {
if(size() == 0) {
return true;
} else {
return false;
}
}
@Override
public int size() {
return count;
}
@Override
public String toString(){
LinearNode<T> temp;
temp = top;
String result =" ";
for(int a = 0;a <count ;a ++){
result+=temp.getElement()+"\t";
temp=temp.getNext();
}
return result;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。