1 package cn.njupt.mdj.queue; 2 3 4 /* 5 * 好好理解思路,才是最重要的,画图可以帮助理解! 6 * 7 */ 8 9 //定义好一个队列的结构,此种队列采用的是this.tail=-1;完成的,也可以用this.tail=0;完成 10 class Queue{ 11 //首先定义队列的大小,默认大小 12 private int maxSize = 10; 13 //为了能够储存任何元素 ,所以采用Object 14 private Object[] data; 15 //队列的头 16 private int head; 17 //队列的尾 18 private int tail; 19 //统计元素的个数 20 private int count; 21 22 //初始化队列 23 public Queue(int size){ 24 this.maxSize = size; 25 this.data = new Object[maxSize]; 26 this.head = 0; 27 //这里为-1,是因为,向队尾添加元素后,队尾会+1,所以就指向了一个元素的位置,所以是要前置++;即++this.tail,而不是this.tail++ 28 //如果是this.tail++,则会在-1的数组中添加元素,那么就数组越界了,所以明白原理是非常重要的,那么就需要是this.tail=0;来初始化了。 29 this.tail = -1; 30 this.count = 0; 31 } 32 33 //添加元素,即向队列中增加元素, 34 public void insert(Object data){ 35 //添加元素之前,首先要判断的是队列中是否还有空间来储存元素,所以首先判读队列是否已满 36 if(ifFull()){ 37 return; 38 } 39 //如果没有满,即可以填加元素,但是另外需要注意的如果队尾已经到达了数组的顶部,那么为了下次添加元素,那么 40 //tail队尾,应该回到第一元素之前的位置,即复原。 41 if(this.tail == maxSize-1){//注意是maxSize-1; 而不是maxSize ,因为this.tail是++this.tail 42 //因为此时的this.tail所指向的单元中已经存放了元素了 43 this.tail = -1; 44 } 45 //否则即未满,也没有到达数组尾部则正常添加 46 this.data[++this.tail] = data; 47 //同时元素个数+1; 48 this.count++; 49 50 } 51 52 //判读元素是否满了 53 public boolean ifFull(){ 54 //当this.count 元素个数 增加到 和 元素储存空间一致的时候,即满了。 55 return this.count == this.maxSize; 56 } 57 58 //可以添加当然就可以删除元素,所以删除元素,并返回删除的元素 59 public Object remove(){ 60 if(isEmpty()){//如果元素没有了,即返回null 61 return null; 62 } 63 64 Object temp = this.data[this.head++];//head++;先取元素,然后在向后移动 65 //删除元素,同样如果删除的话,如果删除的head的指针到了数组顶部,那么也要还原 66 if(this.head == maxSize){//因为是先放后++,所以当this.head元素取走后,然后++,就到到达了末尾 67 this.head =0; 68 } 69 this.count--; 70 return temp; 71 } 72 73 //判断元素是否为空 74 public boolean isEmpty(){ 75 return this.count == 0; 76 } 77 78 //获取元素个数 79 public int getSize(){ 80 return this.count; 81 } 82 83 //获取head的元素 84 public Object peekHead(){ 85 return this.data[this.head]; 86 } 87 88 public String toString(){ 89 if(isEmpty()){ 90 return "[]"; 91 }else{ 92 StringBuffer s = new StringBuffer(); 93 s.append("["); 94 for(int i=this.head;i<this.tail;i++){ 95 s.append(this.data[i]+","); 96 } 97 s.append("]"); 98 return s.toString(); 99 } 100 101 } 102 103 } 104 105 /* 106 * 注:当如果初始化,采用的是this.head =0 和 this.tail=0 完成,则 107 */ 108 class Queue2{ 109 private int maxSize; 110 private Object[] data; 111 private int head; 112 private int tail; 113 114 public Queue2(int size){ 115 this.maxSize = size; 116 this.data = new Object[this.maxSize]; 117 //开始初始化 118 this.head = 0; 119 this.tail = 0; 120 } 121 122 public void insert(Object data){ 123 //增加元素,首先要判断元素是否已满了。 124 if(isFull()){ 125 return; 126 } 127 //元素为满,但如果尾指针已经到了最后一个元素,即最后一个元素已经存放了元素 128 if(this.tail == maxSize){ 129 //只有当this.tail==maxSize,那么说maxSize-1 处存放了元素了。 130 //回原 131 this.tail = 0; 132 } 133 //没有,则继续添加元素 134 this.data[this.tail++] = data; 135 136 } 137 138 public boolean isFull(){ 139 //原因为,采用this.head++ this.tail++; 这样每当添加一个元素的时候是,先放元素,然后在加+1,即指向空处 140 //如:this.head=0,this.tail=0;,添加一个元素后,this.head=0;不变,this.tail++;则先 141 //把元素放在了0处,然后+1;即this.tail=1; 那么 1-0 = 1 元素 142 int size = (this.tail-this.head); 143 return size == this.maxSize; 144 } 145 146 //删除元素 147 public Object remove(){ 148 //首先判读是否为空 149 if(isEmpty()){ 150 return null; 151 } 152 Object temp = this.data[this.head++];//取出元素 153 if(this.head == maxSize){//则最后一个元素也取出来了,还原 154 this.head = 0; 155 } 156 return temp; 157 } 158 159 public boolean isEmpty(){ 160 //要判断为空与否,从开始看 this.tail = 0 = this.head 即为空, 161 //当添加元素后 this.tail++ -->先存放后加 162 //删除元素的时候 this.head++ -->先删除,在加 163 //画图可以看出,只要当两个相同的时候即为空 164 return this.head == this.tail; 165 } 166 167 //获取元素个数 168 public int getSize(){ 169 return this.tail-this.head; 170 } 171 172 //获取head的元素 173 public Object peekHead(){ 174 return this.data[this.head]; 175 } 176 177 public String toString(){ 178 if(isEmpty()){ 179 return "[]"; 180 }else{ 181 StringBuffer s = new StringBuffer(); 182 s.append("["); 183 for(int i=this.head;i<this.tail;i++){ 184 s.append(this.data[i]+","); 185 } 186 s.append("]"); 187 return s.toString(); 188 } 189 190 } 191 } 192 193 194 public class QueueDemo { 195 196 /** 197 * @param args 198 */ 199 public static void main(String[] args) { 200 // TODO Auto-generated method stub 201 Queue2 q = new Queue2(10); 202 q.insert("1"); 203 q.insert("2"); 204 q.insert("3"); 205 q.insert("4"); 206 q.insert("5"); 207 q.insert("6"); 208 System.out.println("删除的元素是:"+q.remove()); 209 System.out.println("获取头元素:"+q.peekHead()); 210 System.out.println(q.toString()); 211 } 212 213 }
参考文章:http://zengzhaoshuai.iteye.com/blog/1171716
1.队列的顺序存储结构及实现
2.循环队列(顺序结构存储实现)
3.队列的链式存储结构及实现
相关文章: