Ai
2 Star 0 Fork 0

CS-IMIS-23/20172309_javaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LindedStack.java 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
20172309 提交于 2018-09-18 22:56 +08:00 . 用链表构成栈类
package second_term.second_week;
import second_term.first_week.EmptyCollectionException;
import second_term.first_week.StackADT;
public class LindedStack<T> implements StackADT<T> {
private int count;//用于计算栈中元素的个数
private LinearNode<T> top;//指向顶栈的指针
public LindedStack(){
count = 0 ;
top = null;
}
@Override
public T pop() {
if (isEmpty())
throw new EmptyCollectionException("Stack");
T result = top.getElement();
top = top.getNext();
count--;
return result;
}
@Override
public void push(T element) {
LinearNode<T> temp = new LinearNode<T>(element);
temp.setNext(top);
top = temp;
count++;
}
@Override
public T peek() throws EmptyCollectionException
{
if (isEmpty()){
throw new EmptyCollectionException("Stack");}
T result = top.getElement();
return result;
}
@Override
public boolean isEmpty() {
if (count == 0)
return true;
else
return false;
}
@Override
public int Size() {
return count;
}
public String toString(){
String result="";
for (int i =0;i<count;i++){
System.out.println(top.getElement());
top = top.getNext();
}
return result;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172309_javaProgramming.git
git@gitee.com:CS-IMIS-23/20172309_javaProgramming.git
CS-IMIS-23
20172309_javaProgramming
20172309_javaProgramming
master

搜索帮助