【数据结构】队列-数组的实现
发布时间:2020-12-15 06:04:49 所属栏目:安全 来源:网络整理
导读:首先定义队列的基本结构,队列和栈不同之处在于队列需要两个指针,一个指向头,一个指向尾 String[] queue;int front = 0;int rear = 0; 构造方法 public QueueOfStrings(int capacity) {queue = new String[capacity];} 进队列 public void enqueue(String
首先定义队列的基本结构,队列和栈不同之处在于队列需要两个指针,一个指向头,一个指向尾 String[] queue; int front = 0; int rear = 0; 构造方法 public QueueOfStrings(int capacity) { queue = new String[capacity]; } 进队列 public void enqueue(String str) { queue[rear++] = str; if (rear == queue.length) resize(2 * queue.length); } 出队列 public String dequeue() { return queue[front++]; } 判空 public boolean isEmpty() { return front == rear; } 判满 public boolean isFull() { return rear == queue.length; } 尺寸 public int size() { return rear - front; } 最后附上resize public void resize(int capacity) { String[] copy = new String[capacity]; for (int i = 0; i < rear; i++) copy[i] = queue[i]; queue = copy; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |