//链结点
public class Link {
public long dData;
public Link next;
public Link(long dd) {
dData=dd;
}
public void displayLink() {
System.out.print(dData+" ");
}
}
public class LinkList {
public Link first;
public LinkList() {
first=null;
}
//是否为空
public boolean isEmpty() {
return first==null;
}
//插入
public void insertFirst(long dd) {
Link newLink=new Link(dd);
newLink.next=first;//插入头部,需要将next值为以前的first
first=newLink;//改变frist为现在的插入
}
//删除头部
public long deleteFirst() {
Link temp=first;
first=first.next;//改变头
return temp.dData;
}
//遍历
public void displayList() {
Link current=first;
while(current!=null) {
current.displayLink();
current=current.next;
}
System.out.println();
}
}
public class LinkStack {
private LinkList theList;
public LinkStack() {
theList=new LinkList();
}
public void push(long j) {
theList.insertFirst(j);
}
public long pop() {
return theList.deleteFirst();
}
public boolean isEmpty() {
return theList.isEmpty();
}
public void displayStack() {
System.out.print("Stack(top-->bottom):");
theList.displayList();
}
}
public class Test {
public static void main(String[] args) {
LinkStack theStack =new LinkStack();
theStack.push(20);
theStack.push(40);
theStack.displayStack();
theStack.push(60);
theStack.push(80);
theStack.displayStack();
theStack.pop();
theStack.pop();
theStack.displayStack();
}
}