入栈和出栈参考
二叉树前序-中序-后序遍历(递归、栈结构)
以下代码均可直接运行:
其中单链表的两种方法,主要在于pop()和push()的方式不太一样,其余都大同小异。同样也可以用双链表实现,和单链表也没差差别,定义Node的时候多加一个Node pre
属性即可;
数组实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| public class ArrayStackDemo {
public static void main(String[] args) { ArrayStack stack = new ArrayStack(4); String key = ""; boolean loop = true; Scanner scanner = new Scanner(System.in);
while (loop) { System.out.println("show: 表示显示栈"); System.out.println("exit: 退出程序"); System.out.println("push: 表示添加数据到栈(入栈)"); System.out.println("pop: 表示从栈取出数据(出栈)"); System.out.print("请输入你的选择:"); key = scanner.next(); switch (key) { case "show": stack.list(); break; case "push": System.out.print("请输入一个数:"); int value = scanner.nextInt(); stack.push(value); break; case "pop": try { int res = stack.pop(); System.out.printf("出栈的数据是%d \n",res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case "exit": scanner.close(); loop = false; break; } } System.out.println("程序退出!");
} }
class ArrayStack { private int maxSize; private int[] stack; private int top = -1;
public ArrayStack(int maxSize) { this.maxSize = maxSize; stack = new int[this.maxSize]; }
public boolean isFull() { return top == maxSize - 1; }
public boolean isEmpty() { return top == -1; }
public void push(int value) { if (isFull()) { System.out.println("栈满"); return; } top++; stack[top] = value; }
public int pop() { if (isEmpty()) { throw new RuntimeException("栈空,没有数据~"); } int value = stack[top]; top--; return value;
}
public void list() { if (isEmpty()) { System.out.println("栈空,没有数据~"); return; } for (int i = top; i >= 0; i--) { System.out.printf("stack[%d] = %d \n", i, stack[i]); } }
}
|
单链表(头插法)实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| public class SingleLinkedListHeadInsertDemo { public static void main(String[] args) { SingleLinkedList sll = new SingleLinkedList(5); Node dum = sll.dum; boolean flag = true;
String key = ""; Scanner scanner = new Scanner(System.in);
while(flag){ System.out.println("show: 显示链表"); System.out.println("push: 加入节点"); System.out.println("pop: 弹出链表节点"); System.out.println("exit: 退出循环"); System.out.print("请输入你的选项: "); key = scanner.next(); switch (key){ case "show": sll.list(); break; case "push": System.out.print("请输入一个节点值: "); int value = scanner.nextInt(); Node node = new Node(value); sll.push(node); break; case "pop": sll.pop(dum); break; case "exit": flag = false; break; } } System.out.println("程序已结束~"); }
}
class SingleLinkedList{ int maxSize;
public SingleLinkedList(int maxSize){ this.maxSize = maxSize; }
Node dum = new Node(0);
public void push(Node node){ if(isFull()){ System.out.println("链表已满~"); return; }
node.next = dum.next; dum.next = node; }
public void pop(Node node){ if(isEmpty()){ System.out.println("链表为空~"); } if(dum.next != null){ Node cur = dum.next; dum.next = cur.next; System.out.printf("弹出:节点 %d \n",cur.val); } }
public boolean isFull(){ return size(dum) == maxSize;
}
public boolean isEmpty(){ return size(dum) == 0; }
public void list(){ if(isEmpty()){ System.out.println("此时链表为空~"); } Node cur = dum.next; while(true){ if(cur == null){ break; } System.out.printf("节点 %d\n",cur.val); cur = cur.next; } }
public int size(Node node){ int count = 0; Node cur = dum.next; while(cur != null){ count++; cur = cur.next; } return count; }
}
class Node{
Node next; int val;
public Node(int val){ this.val = val; }
@Override public String toString() { return "Node{" + "val=" + val + '}'; } }
|
单链表(普通末尾添加顺序)实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| public class SingleLinkedListStackDemo { public static void main(String[] args) { SingleLinkedList sll = new SingleLinkedList(5); Node dum = sll.dum; boolean flag = true;
String key = ""; Scanner scanner = new Scanner(System.in);
while(flag){ System.out.println("show: 显示链表"); System.out.println("push: 加入节点"); System.out.println("pop: 弹出链表节点"); System.out.println("exit: 退出循环"); System.out.print("请输入你的选项: "); key = scanner.next(); switch (key){ case "show": sll.list(); break; case "push": System.out.print("请输入一个节点值: "); int value = scanner.nextInt(); Node node = new Node(value); sll.push(node); break; case "pop": sll.pop(dum); break; case "exit": flag = false; break; } } System.out.println("程序已结束~"); } }
class SingleLinkedList{ int maxSize;
public SingleLinkedList(int maxSize){ this.maxSize = maxSize; }
Node dum = new Node(0);
public void push(Node node){ if(isFull()){ System.out.println("链表已满~"); return; } Node cur = dum; while(true){ if(cur.next == null){ break; } cur = cur.next; } cur.next = node; }
public void pop(Node node){ if(isEmpty()){ System.out.println("链表为空~"); } Node cur = dum; for(int i = 0; i < size(dum) - 1; i++){ cur = cur.next; } if(cur.next != null){ Node temp = cur.next; cur.next = null; System.out.printf("弹出:节点 %d \n",temp.val); } }
public boolean isFull(){ return size(dum) == maxSize;
}
public boolean isEmpty(){ return size(dum) == 0; }
public void list(){ if(isEmpty()){ System.out.println("此时链表为空~"); } Node cur = dum.next; while(true){ if(cur == null){ break; } System.out.printf("节点 %d\n",cur.val); cur = cur.next; } }
public int size(Node node){ int count = 0; Node cur = dum.next; while(cur != null){ count++; cur = cur.next; } return count; }
}
class Node{
Node next; int val;
public Node(int val){ this.val = val; }
@Override public String toString() { return "Node{" + "val=" + val + '}'; } }
|