顺序循环队列
class SeqQueue
{
static final int defaultSize=10;
int front;
int rear;
int maxSize;
Object[] value;
int count;
public SeqQueue()
{
initiate(defaultSize);
}
public SeqQueue(int size)
{
initiate(size);
}
public void initiate(int size)
{
front=rear=0;
value=new Object[size];
count=0;
maxSize=size;
}
public boolean offer(Object o) throws Exception
{
if(count>0&&front==rear)
throw new Exception("堆栈已满,无法插入新数据");
else
{
value[rear]=o;
rear=(rear+1)%maxSize;
count++;
return true;
}
}
public Object poll()
{
if(count==0)
return null;
else
{
Object o=value[front];
front=(front+1)%maxSize;
count--;
return o;
}
}
public Object peek()
{
if(count==0)
return null;
else
{
return value[front];
}
}
public boolean notEmpty()
{
return count>0;
}
}
顺序优先级队列
class Element{
private Object ele;
private int priority;
public Object getEle() {
return ele;
}
public void setEle(Object ele) {
this.ele = ele;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
Element(Object ele,int priority)
{
this.ele=ele;
this.priority=priority;
}
}
class SeqPQueue
{
static final int defaultSize=10;
int front;
int rear;
int count;
Element[] e;
int maxSize;
public SeqPQueue()
{
this.initiate(defaultSize);
}
public SeqPQueue(int size)
{
this.initiate(size);
}
private void initiate(int sz)
{
front=0;
rear=0;
count=0;
maxSize=sz;
e=new Element[sz];
}
public boolean offer(Element ele)
{
if(count>=maxSize)
return false;
e[rear]=ele;
rear++;
count++;
return true;
}
public Element poll()//删除优先级最大的元素
{
if(count==0)
return null;
int maxindex=0;
Element max=e[0];
for(int i=0;i<count;i++)
{
if(max.getPriority()>e[i].getPriority())
{
maxindex=i;
max=e[i];
}
}
for(int k=maxindex;k<count-1;k++)
{
e[k]=e[k+1];
}
rear--;
count--;
return max;
}
public Element peek()
{
if(count==0)
return null;
Element max=e[0];
int maxindex=0;
for(int i=0;i<count;i++)
{
if(e[i].getPriority()<max.getPriority())
{
maxindex=i;
max=e[i];
}
}
return max;
}
public boolean notEmpty()
{
return count>0;
}
}
测试主函数
public static void main(String[] args) throws Exception
{
SeqPQueue spq=new SeqPQueue(20);
spq.offer(new Element(new Integer(10),1));
spq.offer(new Element(new Integer(20),9));
spq.offer(new Element(new Integer(30),6));
spq.offer(new Element(new Integer(40),8));
spq.offer(new Element(new Integer(50),2));
spq.offer(new Element(new Integer(60),3));
spq.offer(new Element(new Integer(70),1));
spq.offer(new Element(new Integer(80),7));
spq.offer(new Element(new Integer(90),1));
spq.offer(new Element(new Integer(00),1));
spq.offer(new Element(new Integer(80),7));
System.out.println("进程号 优先级");
while(spq.notEmpty())
{
Element temp=spq.poll();
System.out.print(temp.getEle()+" ");
System.out.println(temp.getPriority());
}
}
public boolean huiwen(String str) throws Exception
{
SeqQueue sq=new SeqQueue(20);
Stack st=new Stack();
for(int i=0;i<str.length();i++)
{
sq.offer(str.charAt(i));
st.add(str.charAt(i));
}
for(int i=0;i<str.length();i++)
{
if(!sq.poll().equals(st.pop()))
return false;
}
return true;
}
}