//链式栈
public class StackDemo2<T> {
private Code value;
public StackDemo2(){
}
//进栈
public void enter(T t){
value = new Code(t,value);
}
//出栈
public void out(){
value = value.getCode();
}
//获取栈顶的值
public Object get(){
if (value == null){
return null;
}
return value.getValue();
}
//空栈返回false,非空栈返回true
public boolean isIndex(){
return value != null;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Code{
private Object value;
private Code code;
}
//链式队列
public class QueueDemo2<T> {
private Code hard;
private Code end;
public QueueDemo2(){
}
//入队
public void enter(T t){
Code code = new Code(t, null);
if (end == null){
end = code;
hard = code;
}
end.setCode(code);
end = code;
}
//出队
public void out(){
Code code = hard.getCode();
hard = code;
if (hard == null){
end = null;
}
}
//获取队头的值
public Object hard(){
if (hard == null){
return null;
}
return hard.getValue();
}
//获取队尾的值
public Object end(){
if (end == null){
return null;
}
return end.getValue();
}
//空队列返回false,非空队列返回true
public boolean isIndex(){
return hard != null;
}
}