Skip to content

Commit

Permalink
09_queue
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzheng committed Oct 9, 2018
1 parent 619caad commit 34b568f
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
File renamed without changes.
46 changes: 46 additions & 0 deletions java/09_queue/ArrayQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package queue;

/**
* Created by wangzheng on 2018/10/9.
*/
// 用数组实现的队列
public class ArrayQueue {
// 数组:items,数组大小:n
private String[] items;
private int n = 0;
// head表示队头下标,tail表示队尾下标
private int head = 0;
private int tail = 0;

// 申请一个大小为capacity的数组
public ArrayQueue(int capacity) {
items = new String[capacity];
n = capacity;
}

// 入队
public boolean enqueue(String item) {
// 如果tail == n 表示队列已经满了
if (tail == n) return false;
items[tail] = item;
++tail;
return true;
}

// 出队
public String dequeue() {
// 如果head == tail 表示队列为空
if (head == tail) return null;
// 为了让其他语言的同学看的更加明确,把--操作放到单独一行来写了
String ret = items[head];
++head;
return ret;
}

public void printAll() {
for (int i = head; i < tail; ++i) {
System.out.print(items[i] + " ");
}
System.out.println();
}
}
44 changes: 44 additions & 0 deletions java/09_queue/CircularQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package queue;

/**
* Created by wangzheng on 2018/10/9.
*/
public class CircularQueue {
// 数组:items,数组大小:n
private String[] items;
private int n = 0;
// head表示队头下标,tail表示队尾下标
private int head = 0;
private int tail = 0;

// 申请一个大小为capacity的数组
public CircularQueue(int capacity) {
items = new String[capacity];
n = capacity;
}

// 入队
public boolean enqueue(String item) {
// 队列满了
if ((tail + 1) % n == head) return false;
items[tail] = item;
tail = (tail + 1) % n;
return true;
}

// 出队
public String dequeue() {
// 如果head == tail 表示队列为空
if (head == tail) return null;
String ret = items[head];
head = (head + 1) % n;
return ret;
}

public void printAll() {
for (int i = head; i < tail; ++i) {
System.out.print(items[i] + " ");
}
System.out.println();
}
}
56 changes: 56 additions & 0 deletions java/09_queue/DynimacArrayQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package queue;

/**
* Created by wangzheng on 2018/10/9.
*/
public class DynimacArrayQueue {
// 数组:items,数组大小:n
private String[] items;
private int n = 0;
// head表示队头下标,tail表示队尾下标
private int head = 0;
private int tail = 0;

// 申请一个大小为capacity的数组
public DynimacArrayQueue(int capacity) {
items = new String[capacity];
n = capacity;
}

// 入队操作,将item放入队尾
public boolean enqueue(String item) {
// tail == n表示队列末尾没有空间了
if (tail == n) {
// tail ==n && head==0,表示整个队列都占满了
if (head == 0) return false;
// 数据搬移
for (int i = head; i < tail; ++i) {
items[i-head] = items[i];
}
// 搬移完之后重新更新head和tail
tail -= head;
head = 0;
}

items[tail] = item;
tail++;
return true;
}

// 出队
public String dequeue() {
// 如果head == tail 表示队列为空
if (head == tail) return null;
// 为了让其他语言的同学看的更加明确,把--操作放到单独一行来写了
String ret = items[head];
++head;
return ret;
}

public void printAll() {
for (int i = head; i < tail; ++i) {
System.out.print(items[i] + " ");
}
System.out.println();
}
}
61 changes: 61 additions & 0 deletions java/09_queue/QueueBasedOnLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package queue;

/**
* 基于链表实现的队列
*
* Author: Zheng
*/
public class QueueBasedOnLinkedList {

// 队列的队首和队尾
private Node head = null;
private Node tail = null;

// 入队
public void enqueue(String value) {
if (tail == null) {
Node newNode = new Node(value, null);
head = newNode;
tail = newNode;
} else {
tail.next = new Node(value, null);
tail = tail.next;
}
}

// 出队
public String dequeue() {
if (head == null) return null;

String value = head.data;
head = head.next;
if (head == null) {
tail = null;
}
return value;
}

public void printAll() {
Node p = head;
while (p != null) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.println();
}

private static class Node {
private String data;
private Node next;

public Node(String data, Node next) {
this.data = data;
this.next = next;
}

public String getData() {
return data;
}
}

}

0 comments on commit 34b568f

Please sign in to comment.