链式栈

特点

  • 能够克服用数组实现的顺序栈空间利用率不高的特点
  • 但是需要为每个栈元素分配额外的指针空间用来存放指针域

    代码实现

    ```java import org.w3c.dom.Node;

import java.util.LinkedList; import java.util.Scanner;

/**

  • @author laoduan
  • @create 2020-04-10-17:53 */ public class LinkedListStackDemo { public static void main(String[] args) {
    1. LinkedListStack linkedListStack = new LinkedListStack();
  1. String key = "";
  2. boolean loop = true;
  3. Scanner scanner = new Scanner(System.in);
  4. while (loop){
  5. System.out.println("------------------------");
  6. System.out.println("show:显示栈");
  7. System.out.println("exit:退出程序");
  8. System.out.println("pop:出栈");
  9. System.out.println("push:入栈\n");
  10. System.out.println("输入你的选择\n");
  11. key = scanner.next();
  12. switch (key){
  13. case "show":
  14. linkedListStack.list();
  15. break;
  16. case "push":
  17. System.out.println("请输入一个数");
  18. int value = scanner.nextInt();
  19. System.out.println("请输入数据");
  20. String data = scanner.next();
  21. MyNode myNode = new MyNode(value, data);
  22. linkedListStack.push(myNode);
  23. break;
  24. case "pop":
  25. try {
  26. linkedListStack.pop();
  27. }catch (Exception e){
  28. System.out.println(e.getMessage());
  29. }
  30. break;
  31. case "exit":
  32. scanner.close();
  33. loop=false;
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. System.out.println("程序退出");
  40. }

}

class LinkedListStack{ private int top=-1;

  1. MyNode head = new MyNode(0,"");
  2. public MyNode getHead(){
  3. return head;
  4. }
  5. //入栈
  6. public void push(MyNode myNode){
  7. MyNode temp = head.next;
  8. if(head.next==null){
  9. head.next=myNode;
  10. }else {
  11. myNode.next=temp;
  12. head.next=myNode;
  13. }
  14. top++;
  15. }
  16. //出栈
  17. public MyNode pop (){
  18. MyNode node = head.next;
  19. head.next=node.next;
  20. System.out.println("取出的栈顶数据是:"+node);
  21. if(node==null){
  22. System.out.println("链表为空");
  23. }
  24. return node;
  25. }
  26. public void list(){
  27. if (head.next==null){
  28. System.out.println("链表为空");
  29. return;
  30. }
  31. MyNode temp = head.next;
  32. while (true){
  33. if(temp==null){
  34. break;
  35. }
  36. System.out.println(temp);
  37. //将temp后移
  38. temp=temp.next;
  39. }
  40. }

}

class MyLinkedList{ MyNode head = new MyNode(0,””); public MyNode getHead(){ return head; }

  1. public void add(MyNode myNode){
  2. MyNode temp = head.next;
  3. if(head.next==null){
  4. head.next=myNode;
  5. }else {
  6. myNode.next=temp;
  7. head.next=myNode;
  8. }
  9. }
  10. public void show(){
  11. if (head.next==null){
  12. System.out.println("链表为空");
  13. return;
  14. }
  15. MyNode temp = head.next;
  16. while (true){
  17. if(temp==null){
  18. break;
  19. }
  20. System.out.println(temp);
  21. //将temp后移
  22. temp=temp.next;
  23. }
  24. }

} class MyNode{ int no; String data; MyNode next;

  1. public MyNode(int no,String data){
  2. this.no=no;
  3. this.data=data;
  4. }
  5. @Override
  6. public String toString() {
  7. return "{" +
  8. "no=" + no +
  9. ", data='" + data + '\'' +
  10. '}';
  11. }

} ```