数据结构----队列
1.队列定义
static final int QUEUELEN=15;
class DATA3
{
String name;
int age;
}
class SQType
{
DATA3[] data = new DATA3[QUEUELEN];
int head;
int tail;
}
2.初始化队列
SQType SQTypeInit()
{
SQType q;
if((q=new SQType())!=null)
{
q.head=0;
q.tail=0;
return q;
}
else
{
return null;
}
}
3.判断空队列
int SQTypeIsEmpty(SQType q)
{
int temp=0;
if(q.head==q.tail)
temp=1;
return temp;
}
4.判断满队列
int SQTypeIsFull(SQType q)
{
int temp=0;
if(q.tail==QUEUELEN)
temp=1;
return temp;
}
5.清空队列
void SQTypeClear(SQType q)
{
q.head=0;
q.tail=0;
}
6.释放空间
void SQTypeFree(SQType q)
{
if(q!=null)
{
q=null;
}
}
7.入队列
int InSQType(SQType q,DATA3 data)
{
if(q.tail==QUEUELEN)
{
System.out.print("队列已满!操作失败!\n");
return 0;
}
else
{
q.data[q.tail++]=data; //将元素入队列
return 1;
}
}
8.出队列
DATA3 OutSQType(SQType q)
{
if(q.head==q.tail)
{
System.out.print("\n队列已空!操作失败!\n");
return 0;
}
else
{
return q.data[q.head++];
}
return null;
}
9.读结点数据
DATA3 PeekSQType(SQType q)
{
if(SQTypeIsEmpty(q)==1)
{
System.out.print("空队列“);
return 0;
}
else
{
return q.data[q.head];
}
}
10.计算队列长度
int SQTypeLen(SQType q)
{
int temp;
temp=q.tail-q.head;
return temp;
}
浙公网安备 33010602011771号