package com.zengwen.stack;
/**
* 单向链表模拟栈
*/
public class SingleLinkedListStackDemo {
public static void main(String[] args) {
//创建一个栈
SingleLinkedListStack singleLinkedListStack = new SingleLinkedListStack(5);
System.out.println("遍历栈~~");
singleLinkedListStack.list();
System.out.println("入栈~~");
singleLinkedListStack.push(1);
singleLinkedListStack.push(2);
singleLinkedListStack.push(3);
singleLinkedListStack.push(4);
singleLinkedListStack.push(5);
System.out.println("遍历栈~~");
singleLinkedListStack.list();
System.out.println("出栈");
int pop = singleLinkedListStack.pop();
System.out.println(pop);
System.out.println("遍历栈~~");
singleLinkedListStack.list();
System.out.println("4次出栈~");
for (int i = 0; i < 4; i++) {
singleLinkedListStack.pop();
}
System.out.println("遍历栈~~");
singleLinkedListStack.list();
// //创建一个单链表
// SingleLinkedList singleLinkedList = new SingleLinkedList(5);
//
//
// singleLinkedList.update(1,1);
// singleLinkedList.update(2,2);
// singleLinkedList.update(3,3);
// singleLinkedList.update(4,4);
// singleLinkedList.update(5,5);
// singleLinkedList.list();
// for (int i = 1; i <= 5; i++) {
// System.out.println(singleLinkedList.showNode(i));
// }
}
}
//单链表模拟栈
class SingleLinkedListStack {
private int maxSize; // 栈的大小
private SingleLinkedList singleLinkedList; // 单链表模拟栈,数据就放在该链表
private int top = -1;// top指针 默认为-1,代表栈空
//初始化栈
public SingleLinkedListStack(int maxSize) {
this.maxSize = maxSize;
singleLinkedList = new SingleLinkedList(5);
}
//判断栈空
public boolean isEmpty() {
return top == -1;
}
//判断栈满
public boolean isFull() {
return top == maxSize - 1;
}
//入栈(push)
public void push(int value) {
if (isFull()) {
System.out.println("栈满");
return;
}
top++;//top指针向上移动 -1-->0
singleLinkedList.update((top + 1), value); //修改第(top + 1) 个节点的值 0 -->
}
//出栈
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈空~~");
}
int value = singleLinkedList.showNode(top + 1);//得到第(top+1)个节点的值
top--;//top指针下移
return value;//返回值
}
//遍历栈
public void list(){
if (isEmpty()) {
System.out.println("栈空");
return;
}
for (int i = (top + 1); i >= 1; i--) {
System.out.println("stack[" + i + "]=" +singleLinkedList.showNode(i));
}
}
}
//单链表
class SingleLinkedList {
int maxSize = 0;
//头节点
private Node head = new Node(-999999);
//创建一个单链表
public SingleLinkedList(int maxSize) {
this.maxSize = maxSize;
Node temp = head;
for (int i = 0; i < maxSize; i++) {
Node node = new Node(0);
temp.next = node;
temp = temp.next;//temp指针后移
}
}
//遍历单链表
public void list() {
Node temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;//temp指针后移
System.out.println(temp);
}
}
//修改第n个节点的值为value
public void update(int n, int value) {
Node temp = head;
for (int i = 1; i <= n; i++) {//指针向后移动n次
temp = temp.next;
}
temp.value = value;
}
//返回第n个节点的值
public int showNode(int n) {
Node temp = head;
for (int i = 1; i <= n; i++) {//指针向后移动n次
temp = temp.next;
}
return temp.value;
}
}
//节点
class Node {
int value; // 值
Node next; // 指向下一个节点
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}