链队根本就是链栈的亲兄弟哇,做了栈.队.链表,现在还会怕它吗?
题目声明的定义:
typedef int ElemType;
struct node;
typedef struct node Node;
struct queue;
typedef struct queue * Queue;
struct node
{
ElemType data;
Node * next;
};
struct queue
{
Node * front; //队首
Node * rear; //队尾
int size; //队列中数据数
};
2306 链队入队 和前面差不多分情况讨论,一种是空,一种是非空
void Enqueue(Queue q, ElemType x)
{
Node * p = (Node *)malloc(sizeof(node));
p->data = x;
if (q->size == 0)
{
q->front = p;
q->rear = p;
p->next = NULL;
}
else {
q->rear->next = p;
q->rear = p;
p->next = NULL;
}
q->size++;
}
void Dequeue(Queue q)
{
Node *p = q->front;
if (q->size == 1)
{
free(p);
q->front = NULL;
q->rear = NULL;
}
else {
q->front = p->next;
free(p);
}
q->size--;
}