JAVA 堆栈和队列的模拟

第一次写东西,刚自己练练手模拟了下堆栈和队列的的类,希望能给自己一点点激励。以后希望自己能多写点JAVA和JAVASCRIPT的东西在这上面,也是希望能和大家多多交流,互相进步。
堆栈:

public class LinkedStack<T> {
private static class Node<U>{
U item;
Node<U> next;
Node(){
this.item=null;
this.next=null;
}
Node(U item,Node<U> next){
this.item=item;
this.next=next;
}
boolean end(){return this.item==null&&this.next==null;}
}
private Node<T> top=new Node<T>();
/*
* 放进元素
*/
public void push(T item){
top=new Node(item,top);
}
/*
* 取出元素
*/
public T pop(){
T item=top.item;
if(!top.end()){
top=top.next;
}
return item;
}
}

队列:

public class MyQueue<T>{
private static class Node <U>{
U item;
Node<U> previous;
Node(){
this.item=null;
this.previous=null;
}
Node(U item,Node<U> previous){
this.item=item;
this.previous=previous;
}


boolean end(){return this.previous==null;}
}
private Node<T> bottom=new Node<T>();
private int length=0;
/*
* 放进元素
*/
public void push(T item){
if(length==0){
this.bottom.item=item;
length++;
}else{
Node<T> node =new Node<T>(item, null);
Node<T> top=this.bottom.previous;
if(top==null){
this.bottom.previous=node;
}else{
Node<T> next=null;
while(top!=null){
next=top;
top=top.previous;
}
next.previous=node;
}
}
}
/*
* 取出元素
*/
public T pop(){
T item=bottom.item;
if(!bottom.end()){
this.bottom=bottom.previous;
}
return item;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值