package shujujiegou.stack;
import java.util.Stack;
/**
class LinkListStack {
private StackNode first = null;
private int size;
//是否栈满
public Boolean isFull() {
int count = 0;
while (true) {
count++;
first = first.getNext();
if (first == null) {
break;
}
}
return count == size;
}
//入栈
public void push(StackNode node) {
if (first == null) {
first = node;
return;
}
if (isFull()) {
System.out.println("栈满,无法添加!!!");
return;
}
StackNode temp = this.getFirst();
while (true) {
if (temp.getNext() == null) {
break;
}
temp = temp.getNext();
}
temp.setNext(node);
}
//出栈
public StackNode pop() {
if (first == null) {
throw new RuntimeException("当前链表为空!!");
}
StackNode temp = first;
StackNode pre = first;
while (true) {
if (temp.getNext() == null) {
break;
}
pre = temp;
temp = temp.getNext();
}
pre.setNext(null);
return temp;
}
public LinkListStack(int size) {
this.size = size;
}
public void show() {
if (first == null) {
System.out.println("当前栈中无数据,请添加数据!!!");
return;
}
Stack<StackNode> stack = new Stack<>();
while (true) {
stack.add(first);
if (first.getNext() == null) {
break;
}
first = first.getNext();
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
public StackNode getFirst() {
return first;
}
public void setFirst(StackNode first) {
this.first = first;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
class StackNode {
private String no;
private int value;
private StackNode next;
public String getNo() {
return no;
}
public int getValue() {
return value;
}
public StackNode getNext() {
return next;
}
public StackNode(String no, int value) {
this.no = no;
this.value = value;
}
public void setNo(String no) {
this.no = no;
}
public void setValue(int value) {
this.value = value;
}
public void setNext(StackNode next) {
this.next = next;
}
@Override
public String toString() {
return "StackNode{" +
"no=" + no +
", value=" + value +
'}';
}
}
public class LinklistStackDemo {
public static void main(String[] args) {
LinkListStack linkListStack = new LinkListStack(4);
StackNode node1 = new StackNode("NO1", 1);
StackNode node2 = new StackNode("NO2", 2);
// linkListStack.push(new StackNode("NO1", 1));
// linkListStack.push(new StackNode("NO2", 2));
linkListStack.push(node1);
linkListStack.push(node2);
linkListStack.show();
}
}