1 Star 0 Fork 0

CS-IMIS-23/20172306

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LinkedStack.java 2.01 KB
一键复制 编辑 原始数据 按行查看 历史
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;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172306.git
git@gitee.com:CS-IMIS-23/20172306.git
CS-IMIS-23
20172306
20172306
master

搜索帮助