您的位置:首页 > 其它

链式队列操作

2011-07-30 17:57 218 查看
#include<stdio.h>
#include<stdlib.h>

/*链队列的结点定义*/
typedef struct node
{
int data;
struct node *next;
}Qnode;

/*队列链式结构定义*/
typedef struct LinkQueue
{
Qnode *front, *rear;
} LQueue;

/*入队操作*/
void En_LQueue(LQueue *HQ, int x)
{
Qnode *p = (Qnode *)malloc(sizeof(Qnode)); //新生成的结点
p->data = x;
p->next = NULL;

if(NULL == HQ->rear)
{
HQ->front = HQ->rear = p;
}
else
{
HQ->rear->next = p;
HQ->rear = p;
}
}

/*出队操作*/
void De_LQueue(LQueue *HQ)
{
Qnode *p;
int x;
if(HQ->front == NULL)
{
printf("队列为空");
return ;
}else
{
p = HQ->front;    //取出对头给p
x = p->data;      //对头数据元素存入x
HQ->front = p->next;  //对头指向下一个元素
if(NULL == HQ->front)  //队列为空时,队尾也置空
{
HQ->rear = NULL;
}
free(p);
printf("出队列的元素是:%d",x);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: