07--栈【作业:使用单链表模拟栈数据结构】
- 头插法
- 每加入一个节点就将其加入到头节点之后,遍历则从前往后,出栈则是将头节点的指针指向下个节点对象所指向的节点,例:0 --> 1 --> 2 --> 3 变为:0 --> 2 --> 3
1 //定义数字节点类,每个数字节点对象都是一个节点
2 class NumberNode{
3 //data域
4 private int top = -1; //【头节点记住当前栈顶到那里了】
5 private int number;
6
7 public NumberNode(int number) {
8 this.number = number;
9 }
10
11 //next域
12 NumberNode next; //指向下个节点
13
14 public int getTop() {
15 return top;
16 }
17
18 public void setTop(int top) {
19 this.top = top;
20 }
21
22 public int getNumber() {
23 return number;
24 }
25
26 public void setNumber(int number) {
27 this.number = number;
28 }
29
30 public NumberNode getNext() {
31 return next;
32 }
33
34 public void setNext(NumberNode next) {
35 this.next = next;
36 }
37
38 }
1 //管理节点,生成链表【带头节点的】
2 class SingleLinkedListStack{
3 //链表的最大值
4 private int maxSize;
5 public SingleLinkedListStack(int maxSize) {
6 this.maxSize = maxSize;
7 }
8
9 //头节点
10 NumberNode head = new NumberNode(0);
11
12 //链表满
13 public boolean isFull() {
14 return head.getTop() == maxSize - 1;
15 }
16
17 //链表空
18 public boolean isEmpty() {
19 return head.getTop() == -1;
20 }
21
22 //压栈
23 public void push(int n) {
24 if (isFull()) {
25 System.out.println("链表满,无法加入数据");
26 return;
27 }
28 head.setTop(head.getTop()+1);
29 NumberNode newNode = new NumberNode(n);
30 newNode.setNext(head.getNext());
31 head.setNext(newNode);
32 }
33
34 //出栈
35 public int pop() {
36 if (head.getNext() == null) {
37 throw new RuntimeException("链表空");
38 }else {
39 head.setTop(head.getTop() - 1);
40 int popNumber = head.getNext().getNumber();
41 head.setNext(head.getNext().getNext());
42 return popNumber;
43 }
44 }
45
46 //遍历
47 public void show() {
48 if (head.getNext() == null) {
49 System.out.println("链表空,无数据");
50 return;
51 }else {
52 NumberNode tmp = head.getNext();
53 while(tmp != null) {
54 System.out.println(tmp.getNumber());
55 tmp = tmp.getNext(); //继续遍历
56 }
57 }
58 }
59
60 }
1 import java.util.Scanner;
2
3 public class StackDemo {
4
5 public static void main(String[] args) {
6 SingleLinkedListStack singleLinkedListStack = new SingleLinkedListStack(3);
7 Scanner sc = new Scanner(System.in);
8
9 int command = 0;
10 boolean loop = true;
11 while (loop) {
12 System.out.println("*****************************");
13 System.out.println("***1、push(√)***********2、pop***");
14 System.out.println("***3、list(√)***********4、exit***");
15 System.out.println("请选择-->");
16 command = sc.nextInt();
17 switch (command) {
18 case 1:
19 System.out.println("请输入您要压栈数据");
20 int n = sc.nextInt();
21 singleLinkedListStack.push(n);
22 break;
23 case 2:
24 try {
25 int popNum = singleLinkedListStack.pop();
26 System.out.println(popNum + "出栈");
27 } catch (Exception e) {
28 System.out.println(e.getMessage());
29 }
30 break;
31 case 3:
32 try {
33 singleLinkedListStack.show();
34 } catch (Exception e) {
35 System.out.println(e.getMessage());
36 }
37 break;
38 case 4:
39 loop = false;
40 sc.close();
41 System.out.println("程序退出~~~");
42 break;
43 default:
44 System.out.println("不支持该选择,请重新输入~");
45 break;
46 }
47 }
48 }
49 }