欢迎访问宙启技术站
智能推送

Java函数如何实现队列和栈的数据结构?

发布时间:2023-07-06 02:40:28

在Java中,可以使用数组或链表来实现队列和栈的数据结构。下面分别介绍一下如何实现这两种数据结构。

1. 队列

队列是一种先进先出(FIFO)的数据结构。在Java中,可以使用数组或链表来实现队列。

使用数组实现队列的关键是维护队列的头部和尾部指针。头部指针指向队列中的 个元素,尾部指针指向队列中最后一个元素的下一个位置。通过不断移动头部和尾部指针,就可以实现入队和出队操作。

以下是一个使用数组实现队列的示例代码:

public class ArrayQueue {
    private int[] queue;
    private int front;
    private int rear;

    public ArrayQueue(int capacity) {
        queue = new int[capacity];
        front = 0;
        rear = 0;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return rear == queue.length;
    }

    public void enqueue(int item) {
        if (isFull()) {
            throw new IllegalStateException("Queue is full");
        }
        queue[rear++] = item;
    }

    public int dequeue() {
        if (isEmpty()) {
            throw new NoSuchElementException("Queue is empty");
        }
        return queue[front++];
    }

    public int peek() {
        if (isEmpty()) {
            throw new NoSuchElementException("Queue is empty");
        }
        return queue[front];
    }
}

使用链表实现队列的关键在于维护队列的头部和尾部节点。通过操作头部和尾部节点的引用,可以实现入队和出队操作。

以下是一个使用链表实现队列的示例代码:

public class LinkedQueue {
    private Node front;
    private Node rear;

    public LinkedQueue() {
        front = null;
        rear = null;
    }

    public boolean isEmpty() {
        return front == null;
    }

    public void enqueue(int item) {
        Node newNode = new Node(item);
        if (isEmpty()) {
            front = newNode;
            rear = newNode;
        } else {
            rear.next = newNode;
            rear = newNode;
        }
    }

    public int dequeue() {
        if (isEmpty()) {
            throw new NoSuchElementException("Queue is empty");
        }
        int item = front.data;
        front = front.next;
        if (front == null) {
            rear = null;
        }
        return item;
    }

    public int peek() {
        if (isEmpty()) {
            throw new NoSuchElementException("Queue is empty");
        }
        return front.data;
    }

    private class Node {
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
}

2. 栈

栈是一种后进先出(LIFO)的数据结构。在Java中,同样可以使用数组或链表来实现栈。

使用数组实现栈的关键是维护栈顶指针。栈顶指针指向栈中当前的顶部元素。通过不断移动栈顶指针,就可以实现入栈和出栈操作。

以下是一个使用数组实现栈的示例代码:

public class ArrayStack {
    private int[] stack;
    private int top;
    private int capacity;

    public ArrayStack(int capacity) {
        stack = new int[capacity];
        top = -1;
        this.capacity = capacity;
    }

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

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

    public void push(int item) {
        if (isFull()) {
            throw new IllegalStateException("Stack is full");
        }
        stack[++top] = item;
    }

    public int pop() {
        if (isEmpty()) {
            throw new NoSuchElementException("Stack is empty");
        }
        return stack[top--];
    }

    public int peek() {
        if (isEmpty()) {
            throw new NoSuchElementException("Stack is empty");
        }
        return stack[top];
    }
}

使用链表实现栈的关键在于维护栈顶节点。通过操作栈顶节点的引用,可以实现入栈和出栈操作。

以下是一个使用链表实现栈的示例代码:

public class LinkedStack {
    private Node top;

    public LinkedStack() {
        top = null;
    }

    public boolean isEmpty() {
        return top == null;
    }

    public void push(int item) {
        Node newNode = new Node(item);
        newNode.next = top;
        top = newNode;
    }

    public int pop() {
        if (isEmpty()) {
            throw new NoSuchElementException("Stack is empty");
        }
        int item = top.data;
        top = top.next;
        return item;
    }

    public int peek() {
        if (isEmpty()) {
            throw new NoSuchElementException("Stack is empty");
        }
        return top.data;
    }

    private class Node {
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
}

以上就是用Java实现队列和栈数据结构的详细介绍。无论是使用数组还是链表,关键是要维护好相关指针或引用,确保能够正确地进行数据插入和删除操作。