キューエントリとコードの特定の実装

チーム

1.コンセプト

  • キュー:一方の端でのみデータを挿入し、もう一方の端でデータを削除できる特別な線形テーブル。キューにはFIFO(First In First Out)があります。挿入操作の終わりはテール/リアと呼ばれます。キューの:削除操作の終了は、キューの先頭(Head / Front)と呼ばれます。

ここに画像の説明を挿入

2.実現する

  • 単一リンクリストでキューを実現
2.1チームに参加する
  • アイデア:
  1. 最初に、前に要素があるかどうかを判断します。要素がない場合は、直接ヘッドとして使用されます。
  2. 前に要素がある場合、それはテール補間と同等です。
//添加(入队)
    public void offer(int val) {
    
    
        Node node = new Node(val);
        if(this.head == null) {
    
    
            this.head = node;
            this.tail = node;
            return;
        }
        this.tail.next = node;
        this.tail = node;
    }
2.2出発
  • アイデア:
  1. まず、キューに要素があるかどうかを確認します。
  2. 要素がある場合、キューは先入れ先出しであるため、ヘッドノードが新しいヘッドとして直接使用されます。
 //出队
    public int poll() {
    
    
        if(this.head == null) {
    
    
            throw new RuntimeException("队列为空\n");
        }
        int oldData = this.head.val;
        if(this.head.next == null) {
    
    
            this.head = null;
            this.tail = null;
        }else {
    
    
            this.head = this.head.next;
        }
        return oldData;
    }

2.3ヘッド要素を取得する
  • アイデア:チームのヘッド要素を取得するのは比較的簡単です。ヘッドがある限り、ヘッドノードの値を直接返します。
//获取队头元素但是不删除
    public int peek() {
    
    
        if(this.head == null) {
    
    
            throw new RuntimeException("队列为空\n");
        }
        return this.head.val;
    }
    //判断是否为空
    public boolean isEmpty() {
    
    
        if(this.head == null) {
    
    
            return true;
        }
        return false;
    }

3.完全なソースコード

import java.util.LinkedList;
import java.util.Queue;

//顺序栈 +  链式队列
class Node {
    
    
    public int val;
    public Node next;

    public Node(int val) {
    
    
        this.val = val;
    }
}
//链式(单链表)队列
class MyQueue {
    
    
    public Node head;//头
    public Node tail;//尾
    //添加(入队)
    public void offer(int val) {
    
    
        Node node = new Node(val);
        if(this.head == null) {
    
    
            this.head = node;
            this.tail = node;
            return;
        }
        this.tail.next = node;
        this.tail = node;
    }
    //出队
    public int poll() {
    
    
        if(this.head == null) {
    
    
            throw new RuntimeException("队列为空\n");
        }
        int oldData = this.head.val;
        if(this.head.next == null) {
    
    
            this.head = null;
            this.tail = null;
        }else {
    
    
            this.head = this.head.next;
        }
        return oldData;
    }
    //获取队头元素但是不删除
    public int peek() {
    
    
        if(this.head == null) {
    
    
            throw new RuntimeException("队列为空\n");
        }
        return this.head.val;
    }
    //判断是否为空
    public boolean isEmpty() {
    
    
        if(this.head == null) {
    
    
            return true;
        }
        return false;
    }

}


public class TestDemo3 {
    
    

    public static void main(String[] args) {
    
    
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);//入队
        myQueue.offer(2);
        myQueue.offer(3);
        myQueue.offer(4);
        System.out.println(myQueue.peek());//获取队头元素
        System.out.println(myQueue.poll());//出栈
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());

    }
    }

おすすめ

転載: blog.csdn.net/qq_45665172/article/details/110207244