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

相关Java函数实现栈和队列操作

发布时间:2023-07-27 06:18:10

栈和队列是两种常见的数据结构,用于存储和管理数据。在Java中,可以使用数组或链表来实现栈和队列的操作。下面将分别介绍Java中如何实现栈和队列的常用操作函数。

1. 栈的实现

栈是一种先进后出(Last-in-First-out,LIFO)的数据结构,可以使用数组或链表来实现。主要的操作函数包括入栈(push)、出栈(pop)、栈顶元素获取(peek)以及判断栈是否为空(isEmpty)。

使用数组实现栈:

public class Stack {
    private int top;
    private int[] stack;
    
    public Stack(int capacity) {
        this.stack = new int[capacity];
        this.top = -1;
    }
    
    public void push(int value) {
        if (top < stack.length - 1) {
            stack[++top] = value;
        } else {
            System.out.println("Stack is full!");
        }
    }
    
    public int pop() {
        if (isEmpty()) {
            System.out.println("Stack is empty!");
            return -1;
        } else {
            return stack[top--];
        }
    }
    
    public int peek() {
        if (isEmpty()) {
            System.out.println("Stack is empty!");
            return -1;
        } else {
            return stack[top];
        }
    }
    
    public boolean isEmpty() {
        return top == -1;
    }
}

使用链表实现栈:

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

public class Stack {
    private Node top;
    
    public Stack() {
        this.top = null;
    }
    
    public void push(int value) {
        Node newNode = new Node(value);
        if (isEmpty()) {
            top = newNode;
        } else {
            newNode.next = top;
            top = newNode;
        }
    }
    
    public int pop() {
        if (isEmpty()) {
            System.out.println("Stack is empty!");
            return -1;
        } else {
            int value = top.val;
            top = top.next;
            return value;
        }
    }
    
    public int peek() {
        if (isEmpty()) {
            System.out.println("Stack is empty!");
            return -1;
        } else {
            return top.val;
        }
    }
    
    public boolean isEmpty() {
        return top == null;
    }
}

2. 队列的实现

队列是一种先进先出(First-in-First-out,FIFO)的数据结构,同样可以使用数组或链表来实现。主要的操作函数包括入队(enqueue)、出队(dequeue)、队首元素获取(peek)以及判断队列是否为空(isEmpty)。

使用数组实现队列:

public class Queue {
    private int front;
    private int rear;
    private int[] queue;
    
    public Queue(int capacity) {
        this.queue = new int[capacity];
        this.front = 0;
        this.rear = -1;
    }
    
    public void enqueue(int value) {
        if (rear < queue.length - 1) {
            queue[++rear] = value;
        } else {
            System.out.println("Queue is full!");
        }
    }
    
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty!");
            return -1;
        } else {
            int value = queue[front];
            front++;
            return value;
        }
    }
    
    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty!");
            return -1;
        } else {
            return queue[front];
        }
    }
    
    public boolean isEmpty() {
        return rear < front;
    }
}

使用链表实现队列:

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

public class Queue {
    private Node front;
    private Node rear;
    
    public Queue() {
        this.front = null;
        this.rear = null;
    }
    
    public void enqueue(int value) {
        Node newNode = new Node(value);
        if (isEmpty()) {
            front = newNode;
            rear = newNode;
        } else {
            rear.next = newNode;
            rear = newNode;
        }
    }
    
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty!");
            return -1;
        } else {
            int value = front.val;
            front = front.next;
            if (front == null) {
                rear = null;
            }
            return value;
        }
    }
    
    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty!");
            return -1;
        } else {
            return front.val;
        }
    }
    
    public boolean isEmpty() {
        return front == null;
    }
}

通过实现这些函数,可以在Java中方便地使用栈和队列来解决各种问题。