java数据结构和算法------队列

 1 // 普通队列
 2 package iYou.neugle.list;
 3 
 4 public class MySeqQueue<T> {
 5     private SeqQueue queue = new SeqQueue();
 6 
 7     class SeqQueue {
 8         public int maxSize = 10;
 9         public T[] data = (T[]) new Object[this.maxSize];
10         public int head = 0;// 头指针
11         public int tail = 0;// 尾指针
12     }
13 
14     // 初始化队列
15     public void SeqQueueInit() {
16         this.queue.head = 0;
17         this.queue.tail = 0;
18         System.out.println("队列初始化成功!");
19     }
20 
21     // 出队
22     public T SeqQueueOut() {
23         if (this.queue.head == this.queue.tail) {
24             System.out.println("队列已空,无法出队!");
25             return null;
26         }
27         T data = this.queue.data[this.queue.head];
28         this.queue.data[this.queue.head] = null;
29         this.queue.head++;
30         return data;
31     }
32 
33     // 入队
34     public void SeqQueueIn(T data) {
35         if (this.queue.tail == this.queue.maxSize) {
36             System.out.println("队列已满,无法入队!");
37             return;
38         }
39         this.queue.data[this.queue.tail++] = data;
40     }
41 
42     // 获取队头元素
43     public T SeqQueuePeek() {
44         if (this.queue.head == this.queue.tail) {
45             System.out.println("队列已空,无法获取列头元素!");
46             return null;
47         }
48 
49         return this.queue.data[this.queue.head];
50     }
51 
52     // 获取队列长度
53     public int SeqQueueLen() {
54         return this.queue.tail - this.queue.head;
55     }
56 }
 1 // 循环队列
 2 package iYou.neugle.list;
 3 
 4 public class MySeqLoopQueue<T> {
 5     private SeqQueue queue = new SeqQueue();
 6 
 7     class SeqQueue {
 8         public int maxSize = 10;
 9         public T[] data = (T[]) new Object[this.maxSize];
10         public int head = 0;// 头指针
11         public int tail = 0;// 尾指针
12         public int size = 0;// 实际数
13     }
14 
15     // 初始化队列
16     public void SeqQueueInit() {
17         this.queue.head = 0;
18         this.queue.tail = 0;
19         this.queue.size = 0;
20         System.out.println("循环队列初始化成功!");
21     }
22 
23     // 出队
24     public T SeqQueueOut() {
25         if (this.queue.size == 0) {
26             System.out.println("循环队列已空,无法出队!");
27             return null;
28         }
29         T data = this.queue.data[this.queue.head];
30         this.queue.data[this.queue.head] = null;
31         this.queue.size--;
32         this.queue.head = (this.queue.head + 1) % this.queue.maxSize;
33         return data;
34     }
35 
36     // 入队
37     public void SeqQueueIn(T data) {
38         if (this.queue.size == this.queue.maxSize) {
39             System.out.println("循环队列已满,无法入队!");
40             return;
41         }
42         this.queue.data[this.queue.tail] = data;
43         this.queue.size++;
44         this.queue.tail = (this.queue.tail + 1) % this.queue.maxSize;
45     }
46 
47     // 获取队头元素
48     public T SeqQueuePeek() {
49         if (this.queue.size == 0) {
50             System.out.println("循环队列已空,无法获取列头元素!");
51             return null;
52         }
53 
54         return this.queue.data[this.queue.head];
55     }
56 
57     // 获取队列长度
58     public int SeqQueueLen() {
59         return this.queue.size;
60     }
61 }

 

你可能感兴趣的:(java)