【问题标题】:Queue Circular Array loop队列圆形数组循环
【发布时间】:2014-03-16 22:26:19
【问题描述】:

假设我有一个大小为 [10] 的数组,当该数组被填满时,我想实现一个 FIFO 结构,而不是仅仅将其填满,因此无法向数组中添加新内容并丢弃老的。

例如,如果我有一个包含汽车制造商的字符串数组,并且当我的数组中有 10 个制造商时,我希望删除最旧的条目并添加最新的条目,但要牢记 FIFO。我将如何在这样的方法中实现它:

public void insert(String name)
{
    int a;
    a = (rear + 1) % names.length;

    if(a == front)
    {
        System.out.println("Full Queue!");
    }
    else
    {
        rear = a;
        names[rear] = name;

        if(front == -1)
        {
            front = 0;
        }

    }
}

【问题讨论】:

  • 一个队列已经 FIFO,因此如果队列已满,您只需“获取”一个元素并“插入”一个元素。 java中有一个可用的队列:easywayserver.com/blog/java-queue-example
  • 你需要自己实现逻辑,还是允许在Java中使用队列结构?
  • 我需要自己实现逻辑。所以没有预定义的结构
  • @user2298680 查看下面实现的LinkedList。 Java Queue 实际上是基于 LinkedList 引擎盖下的

标签: java arrays loops queue


【解决方案1】:

我建议使用 LinkedList 的实现

public class LinkedList {
  Node head;
  Node tail;
  final int MAX_SIZE;
  int currentSize; 

  public LinkedList(int MAX_SIZE) {
    this.head = null;
    this.tail = null;
    this.MAX_SIZE = MAX_SIZE;
    this.currentSize = 0;
  }

  public void append(String val) {
    Node n = new Node(val);

    if (currentSize < MAX_SIZE) {   
      if (head == null) {
        head = n;
        tail = n;
        return;
      }

      Node current = head;
      while (current.next != null) {
        current = current.next;
      }

      current.next = n;
      currentSize++;
    }
    else {
      head = head.next;
      currentSize--;
      append(val);
    }
  }

public class Node {
  String val;
  Node next;

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

基本上你持有MAX_SIZE 和currentSize。当您的 currentSize 达到最大值时,您删除 LinkedList 的头部并将值附加到末尾。

【讨论】:

    【解决方案2】:

    我尝试像您一样使用后排和前排来构建队列。稍微修改了您的插入。我写了一个带有一些测试的主要方法。

    我还必须添加一个“空”标志来检查后==前是因为是空的还是因为是满的。

    public class DummyQueue {
        int rear, front;
        String[] names;
        boolean empty = true;
    
        public DummyQueue(int size) {
            names = new String[size];
        }
    
        public void insert(String name)
        {
            if(!empty && rear == front )
            {
                System.out.println("Full Queue!");
            }
            else
            {
                names[rear] = name;
                rear = (rear+1) % names.length;
            }
            empty = false;
        }
    
        public String deque()
        {
            if (empty) {
                System.out.println("Empty Queue!");
                return null; // demo 
            } else { 
                 String response = names[front % names.length];
                 front = (front + 1) % names.length;
                 if (front == rear) empty = true;
                 return response;
            }
        }
    
        public static void main(String[] args) {
            DummyQueue d = new DummyQueue(10);
            System.out.println(d.deque());
            d.insert("Element");
            System.out.println(d.deque());
            System.out.println(d.deque());
            for (int i = 0; i < 12; i++) {
                System.out.println("Adding: "+i);
                d.insert("Element "+ i);
            }
            for (int i = 0; i < 12; i++) {
                System.out.println(d.deque());
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      相关资源
      最近更新 更多