/*队列的链接存储结构及实现*/
//链队列的存储结构
typedef int DataType;
typedef struct Node
{
DataType data;
Node *next;
}Node;
typedef struct //定义链队列
{
Node *front,*cear;
}LinkQueue;
//链队列的初始化
void InitQueue(LinkQueue &Q)
{
s = (Node *)malloc(sizeof(Node));
s->next = NULL; //创建一个结点s
Q.front = Q.rear = s; //将队头指针和队尾指针均指向s
}
//入队操作:
void EnQueue(LinkQueue &Q,DataType)
{
s = (Node *)malloc(sizeof(Node));
s->data = x; //申请一个数据域作为x的结点s
s->next = NULL;
Q.rear->next = s; //将结点s插入队尾
Q.rear = s;
}
//链队列出队
DataType DeQueue(LinkQueue &Q)
{
if(Q.rear == Q.front)
{
printf("下溢");
exit(-1);
}
p = Q.front->next;
x = p->data //暂存队头元素
Q.front->next = p->next; //将队头元素所在的结点摘链
if(p->next == NULL)
Q.rear = Q.front; //判断出队前队列长度是否为1
free(p);
return x;
}
//取队头元素
DataType GetFront(LinkQueue &Q)
{
if(Q.rear == Q.front)
{
printf("下溢");
exit(-1);
}
return Q.front->next->data;
}
//判空操作
int Empty(LinkQueue &Q)
{
if(Q.rear == Q.front)
return 1; //队列为空返回1
else
return 0;
}
//链队列的存储结构
typedef int DataType;
typedef struct Node
{
DataType data;
Node *next;
}Node;
typedef struct //定义链队列
{
Node *front,*cear;
}LinkQueue;
//链队列的初始化
void InitQueue(LinkQueue &Q)
{
s = (Node *)malloc(sizeof(Node));
s->next = NULL; //创建一个结点s
Q.front = Q.rear = s; //将队头指针和队尾指针均指向s
}
//入队操作:
void EnQueue(LinkQueue &Q,DataType)
{
s = (Node *)malloc(sizeof(Node));
s->data = x; //申请一个数据域作为x的结点s
s->next = NULL;
Q.rear->next = s; //将结点s插入队尾
Q.rear = s;
}
//链队列出队
DataType DeQueue(LinkQueue &Q)
{
if(Q.rear == Q.front)
{
printf("下溢");
exit(-1);
}
p = Q.front->next;
x = p->data //暂存队头元素
Q.front->next = p->next; //将队头元素所在的结点摘链
if(p->next == NULL)
Q.rear = Q.front; //判断出队前队列长度是否为1
free(p);
return x;
}
//取队头元素
DataType GetFront(LinkQueue &Q)
{
if(Q.rear == Q.front)
{
printf("下溢");
exit(-1);
}
return Q.front->next->data;
}
//判空操作
int Empty(LinkQueue &Q)
{
if(Q.rear == Q.front)
return 1; //队列为空返回1
else
return 0;
}