数据结构与算法-栈-java源码(数组和链表分别实现)


栈的实现(数组)

一、思路

在这里插入图片描述

1.Stack

代码如下(示例):

package Stack;

//用数组来模拟栈
public class Stack {
    
    
    private int maxsize; //数组最大容量
    private int top; //初始化栈顶 初始值为-1
    private int[] Stackarray;

    public Stack(int arraymaxsize) {
    
    
        maxsize = arraymaxsize;
        Stackarray = new int[maxsize];
        top = -1;
    }

    public boolean isFull() {
    
    
        return top == maxsize - 1;
    }

    public boolean isEmpty() {
    
    
        return top == -1;
    }

    public void push(int num) {
    
    
        if (isFull()) {
    
    
            System.out.println("栈已满,无法压栈");
            return;
        }
        top++;
        Stackarray[top] = num;
    }

    public int pop() {
    
    
        if (isEmpty()) {
    
    
            throw new RuntimeException("栈空,没有数据");
        }
        int value = Stackarray[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, Stackarray[i]);
        }
    }

}

2.StackDemo

代码如下(示例):

package Stack;

import java.util.Scanner;

public class StackDemo {
    
    
    public static void main(String[] args) {
    
    
        Stack stack = new Stack(4);
        String key;
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop)
        {
    
    
            System.out.println("show:显示栈");
            System.out.println("exit:退出程序");
            System.out.println("push:压栈");
            System.out.println("pop:出栈");
            System.out.println("输入你想要的功能");
            key = scanner.next();
            switch (key) {
    
    
                case "show":
                    stack.list();
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                case "push":
                    System.out.println("请输入一个数据");
                    int i = scanner.nextInt();
                    stack.push(i);
                    break;
                case "pop":
                    try {
    
    
                        int value = stack.pop();
                        System.out.printf("出栈的数据为%d\n", value);
                    } catch (Exception e) {
    
    
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}



栈的实现(单向链表)

一、思路

头插法

1.Node类

代码如下(示例):

package Stack;

public class Node {
    
    
    int num;
    Node next;
    public Node(int num){
    
    
        this.num = num;
    }
    @Override
    public String toString() {
    
    
        return "Node{" +
                "num=" + num +
                '}';
    }
}

2.LinkedStack类

代码如下(示例):

package Stack;

//单链表实现栈 头插入法
public class LinkedStack {
    
    
    int length;
    Node head = new Node(0);
    

    public LinkedStack() {
    
    
        length = 0;
    }

    public boolean isEmpty() {
    
    
        return length == 0;
    }

    public int getLength() {
    
    
        return length;
    }

    //入栈
    public void push(Node node) {
    
    
        node.next = head.next;
        head.next = node;
        length++;
    }

    //出栈
    public Node pop() {
    
    
        if (isEmpty()) {
    
    
            throw new RuntimeException("链表为空");
        }
        Node node = head.next;
        head.next = head.next.next;
        length--;
        return node;
    }
    //遍历栈

    public void list() {
    
    
        if (isEmpty()) {
    
    
            System.out.println("链表为空,无法遍历");
            return;
        }
        Node cur = head;
        for (int i = 0; i < length; i++) {
    
    
            Node next = cur.next;
            System.out.println(next);
            cur = cur.next;
        }
    }
}

3.LinkedStackDemo

代码如下(示例):

package Stack;

public class LinkedStackDemo {
    
    
    public static void main(String[] args) {
    
    
        LinkedStack linkedStack= new LinkedStack();
        Node node1 = new Node(10);
        Node node2 = new Node(20);
        Node node3 = new Node(30);
        Node node4 = new Node(40);

        linkedStack.push(node1);
        linkedStack.push(node2);
        linkedStack.push(node3);
        linkedStack.push(node4);

        linkedStack.list();
        System.out.println("=======");
        Node pop = linkedStack.pop();
        System.out.println(pop);
        int length = linkedStack.getLength();
        System.out.println(length);
        linkedStack.list();
        linkedStack.pop();
        linkedStack.pop();
        linkedStack.pop();
        System.out.println("=");
        linkedStack.list();
        linkedStack.push(node1);
        linkedStack.list();

    }

}

总结

两种实现方法都可以。数组好理解,链表头插法也好理解。

猜你喜欢

转载自blog.csdn.net/slighting1128/article/details/111316611