java实现循环队列

头几天面试一些打公司,发现他们对项目经验看的不是很重,他们很注重数据结构和逻辑思维和数据库,以为以前对数据结构的不够重视,

所以碰了壁,这几天学习了一下,自己重新写了一遍,发现数据结构真的很有吸引力。下面我贴出代码,

/**
* 文件名:Circularqueue.java
* 全路径:cdu.edu.queue.Circularqueue
*/
package cdu.edu.queue;

/**
* 功能: chengdu university
* 作者: (daibing)
* 时间:2013-12-4
* 版本:2013-12-4
*
*/
public class Circularqueue {
private int maxSize = 8;
private int[] a = new int[maxSize]; // 初始化循环队列
private int font = 0; // 头指针
private int rear = 0; // 尾指针

/**
* 进入队列
*
* @param data:添加的元素
*/
public void addElement(int data) {
// 判断循环队列是否满员
if (!isFull()) {
a[rear] = data;
rear = (rear + 1) % maxSize; //尾指针加一
} else {
System.out.println("队列已满!!退出操作!!!");
return;
}
}

/**
* 删除队列头元素
*/
public void delElement() {
if (!isEmpty()) {
a[font] = 0;
font = (font + 1) % maxSize; //头指针加一
} else {
System.out.println("队列为空!!退出操作!!!");
}
}

/**
* 找到队列头元素
* @return
*/
public int getFont() {
if (!isEmpty()) {
return a[font];
} else {
return 0;
}
}

/**
* 获取队列元素的个数
* @return
*/
public int getSize(){
return (rear-font+maxSize)%maxSize;
}

/**
* 循环队列是否为空
*
* @return
*/
public boolean isEmpty() {
return rear == font;
}

/**
* 循环队列是否满
*/
public boolean isFull() {
return (rear + 1) % maxSize == font;
}

public static void main(String[] args) {

Circularqueue queue = new Circularqueue();
queue.addElement(23);
queue.addElement(73);
queue.addElement(56);
queue.addElement(89);
queue.delElement();

for (int i = 0; i < queue.a.length; i++) {
System.out.println(queue.a[i]);
}

System.out.println(queue.getSize());
}

}

 

 

 

上面写得也有可能有不足和错误,希望大家共同探讨!!!!!

posted @ 2013-12-04 09:50  口哨声轻  阅读(1162)  评论(0)    收藏  举报