求用C语言做队列的出对和入队

如题所述

//定义队列结构体
typedef struct Qnode
{
int data;
struct Qnode *next;
} Queue , *QueuePtr;

typedef struct
{
QueuePtr front;
QueuePtr rear;
} linkQnode;
//创建一个队列
initQueue (linkQnode *q)
{
q -> front = q -> rear = (QueuePtr) malloc (sizeof (Queue));
if (!q -> front) exit (0);
q -> front -> next = NULL;
}
//入队列
EnterQueue (linkQnode *q , int item)
{
QueuePtr p;
p = (QueuePtr) malloc (sizeof (Queue));
if (!p) exit (0);
p -> data = item;
p -> next = NULL;
q -> rear -> next = p;
q -> rear = p;
}
//出队列
DelQueue (linkQnode *q , int *item)
{
QueuePtr p;
if (q -> front = q -> rear) return;
p = q -> front -> next;
*item = p -> data;
q -> front -> next = p -> next;
if (q -> rear == p)
q -> rear = q -> front;
free (p);
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答