一、代码如下
/**
* Created by Xi on 2017/8/1.
* 用链表实现栈
*/
public class StackLink {
private LinkListStack theList;
public StackLink(){
theList=new LinkListStack();
}
/**
* 添加元素
*/
public void push(double dd){
theList.insertFirst(dd);
}
/**
* 取出元素
*/
public double pop(){
return theList.deleteFirst();
}
/**
* 判断栈是否为空
*/
public boolean isEmpty(){
return theList.isEmpty();
}
/**
* 展示栈
*/
public void displayStack(){
Log.v("StackLink","StackLink (top-->bottom):");
theList.displayList();
}
}
二、调用代码如下:
/**
* 用链表实现栈
*/
public void link_stack(){
StackLink theStack=new StackLink();
theStack.push(20);
theStack.push(30);
theStack.displayStack();
theStack.push(40);
theStack.push(50);
theStack.displayStack();
theStack.pop();
theStack.pop();
theStack.displayStack();
}
日志如下:
08-14 18:36:43.260 9687-9687/com.tool.wpn.quicksort V/StackLink: StackLink (top-->bottom):
08-14 18:36:43.261 9687-9687/com.tool.wpn.quicksort I/LinkListStack: List (first-->last):
08-14 18:36:43.261 9687-9687/com.tool.wpn.quicksort I/LinkStack: 30.0
08-14 18:36:43.261 9687-9687/com.tool.wpn.quicksort I/LinkStack: 20.0
08-14 18:36:43.261 9687-9687/com.tool.wpn.quicksort V/StackLink: StackLink (top-->bottom):
08-14 18:36:43.261 9687-9687/com.tool.wpn.quicksort I/LinkListStack: List (first-->last):
08-14 18:36:43.262 9687-9687/com.tool.wpn.quicksort I/LinkStack: 50.0
08-14 18:36:43.262 9687-9687/com.tool.wpn.quicksort I/LinkStack: 40.0
08-14 18:36:43.262 9687-9687/com.tool.wpn.quicksort I/LinkStack: 30.0
08-14 18:36:43.262 9687-9687/com.tool.wpn.quicksort I/LinkStack: 20.0
08-14 18:36:43.262 9687-9687/com.tool.wpn.quicksort V/StackLink: StackLink (top-->bottom):
08-14 18:36:43.262 9687-9687/com.tool.wpn.quicksort I/LinkListStack: List (first-->last):
08-14 18:36:43.262 9687-9687/com.tool.wpn.quicksort I/LinkStack: 30.0
08-14 18:36:43.262 9687-9687/com.tool.wpn.quicksort I/LinkStack: 20.0\
源码下载地址:点击打开链接