求用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);
}
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);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询