栈和队列的实现

在Java在Java中,栈(中,栈(Stack)和队列(Queue)是Stack)和队列(Queue)是两种基本的数据结构,它们在处理数据集合时遵循不同的原则。两种基本的数据结构,它们在处理数据集合时遵循不同的原则。栈遵循后进先出(LIFO, Last In First Out)的原则,而栈遵循后进先出(LIFO, Last In First Out)的原则,而队列遵循先进先出(FIFO, First In First Out)的原则。队列遵循先进先出(FIFO, First In First Out)的原则。下面我将简要介绍顺序栈、链队以及循环队列的实现方式,并下面我将简要介绍顺序栈、链队以及循环队列的实现方式,并附上一些基本的代码示例。附上一些基本的代码示例。

### 1. 顺序栈的实现

顺序栈通常使用数组来实现,其中

### 1. 顺序栈的实现

顺序栈通常使用数组来实现,其中栈顶的位置是动态变化的。栈顶的位置是动态变化的。

```java
public class ArrayStack {
    private int[] stack;
    private int

```java
public class ArrayStack {
    private int[] stack;
    private int maxSize;
    private int top; // 栈顶指针

    public ArrayStack maxSize;
    private int top; // 栈顶指针

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[this.maxSize];
        this.top = -1; = new int[this.maxSize];
        this.top = -1; // 初始化栈为空
    }

    // 判断栈满
    public boolean is // 初始化栈为空
    }

    // 判断栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //Full() {
        return top == maxSize - 1;
    }

    // 判断栈空
    public boolean isEmpty() {
        return top == -1; 判断栈空
    public boolean isEmpty() {
        return top == -1;
    }

    // 入栈
    public void push(int value) {
        
    }

    // 入栈
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满,if (isFull()) {
            System.out.println("栈满,无法添加");
        } else {
            top++;
            stack[top] =无法添加");
        } else {
            top++;
            stack[top] = value;
        }
    }

    // 出栈
    public int pop() { value;
        }
    }

    // 出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空,无法取出
        if (isEmpty()) {
            throw new RuntimeException("栈空,无法取出");
        }
        int value = stack[top];
        top--;
        return");
        }
        int value = stack[top];
        top--;
        return value;
    }

    // 查看栈顶元素
    public int peek() { value;
    }

    // 查看栈顶元素
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        return stack[top];
    }
}
```

### 2. 链
        return stack[top];
    }
}
```

### 2. 链队的实现

链队通常使用链表来实现,其中队尾的位置是动态变化的队的实现

链队通常使用链表来实现,其中队尾的位置是动态变化的。。

```java
class Node {
    int data;
    Node next;

    Node(

```java
class Node {
    int data;
    Node next;

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

public class LinkedQueue {
    private Node front; // null;
    }
}

public class LinkedQueue {
    private Node front; // 队头
    private Node rear; // 队尾

    public LinkedQueue 队头
    private Node rear; // 队尾

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

    () {
        this.front = this.rear = null;
    }

    // 判断队列是否为空
    public boolean isEmpty() {
        return front == null// 判断队列是否为空
    public boolean isEmpty() {
        return front == null;
    }

    // 入队
    public void enqueue(int value) {;
    }

    // 入队
    public void enqueue(int value) {
        Node newNode = new Node(value);
        if (rear == null)
        Node newNode = new Node(value);
        if (rear == null) { // 队列为空
            front = rear = newNode;
        } else { { // 队列为空
            front = rear = newNode;
        } else {
            rear.next = newNode;
            rear = newNode;
        }
    }
            rear.next = newNode;
            rear = newNode;
        }
    }

    // 出队
    public int dequeue() {
        if (isEmpty()) {

    // 出队
    public int dequeue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int value = front.
            throw new RuntimeException("队列为空");
        }
        int value = front.data;
        front = front.next;
        if (front == null)data;
        front = front.next;
        if (front == null) { // 如果队列中只有一个元素
            rear = null;
        }
        return { // 如果队列中只有一个元素
            rear = null;
        }
        return value;
    }

    // 查看队首元素
    public int getFront() value;
    }

    // 查看队首元素
    public int getFront() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
         {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return front.data;
    }
}
```

### 3. 循环}
        return front.data;
    }
}
```

### 3. 循环队列的实现

循环队列使用固定大小的数组,通过两个指针(队头front队列的实现

循环队列使用固定大小的数组,通过两个指针(队头front和队尾rear)的循环移动来实现队列的循环使用。和队尾rear)的循环移动来实现队列的循环使用。

```java
public class CircularQueue {
    private int[] queue;
    private int

```java
public class CircularQueue {
    private int[] queue;
    private int front;
    private int rear;
    private int capacity;

    public CircularQueue front;
    private int rear;
    private int capacity;

    public CircularQueue(int capacity) {
        this.capacity = capacity + 1; //(int capacity) {
        this.capacity = capacity + 1; // 数组多出一个空间用于判断队列满
        this.queue = new int 数组多出一个空间用于判断队列满
        this.queue = new int[this.capacity];
        this.front = this.rear = 0[this.capacity];
        this.front = this.rear = 0;
    }

    // 判断队列满
    public boolean isFull() {
        ;
    }

    // 判断队列满
    public boolean isFull() {
        return (rear + 1) % capacity == front;
    }

    //return (rear + 1) % capacity == front;
    }

    // 判断队列空
    public boolean isEmpty() {
        return front == rear;
     判断队列空
    public boolean isEmpty() {
        return front == rear;
    }

    // 入队
    public void enqueue(int value) {
        if}

    // 入队
    public void enqueue(int value) {
        if (isFull()) {
            System.out.println("队列满,无法 (isFull()) {
            System.out.println("队列满,无法添加");
        } else {
            queue[rear] = value;
            rear添加");
        } else {
            queue[rear] = value;
            rear = (rear + 1) % capacity;
        }
    } = (rear + 1) % capacity;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值