*****用途*****
可以自由控制队列的空间
*****用途*****
#include <stdio.h>
#include <stdlib.h>#define MAXSIZE 20
typedef int datatype;
typedef struct node {
datatype data;
struct node *next;
}QNode;
typedef struct {
QNode *front;
QNode *rear;
}LQueue;
LQueue *Init_LQueue(); //建立链队列
void InLQueue(); //入队
int Empty_LQueue(); //判队空
int Out_LQueue(); //出队
void main () {
LQueue *sq;
datatype x1 = 10;
datatype x2;
sq = Init_LQueue();
InLQueue(sq,x1);
Empty_LQueue(sq);
Out_LQueue(sq,x2);
}
//建立一个带头节点的空队
LQueue* Init_LQueue () {
LQueue *q;
QNode *p;
q = malloc(sizeof(LQueue));
p = malloc(sizeof(QNode));
p->next = NULL;
q->front = q->rear = p;
printf("入队数据 : %d\n",q->front->next);
return q;
}
//入队
void InLQueue (LQueue *q, datatype x) {
QNode *p;
p = malloc(sizeof(QNode));
p->data = x;
p->next = NULL;
q->front->next = p;
q->rear = p;
printf("入队数据 : %d\n",q->front->next->data);
}
//判队空
int Empty_LQueue(LQueue *q, datatype x) {
if (q->front == q->rear) {
printf("队空\n");
return 1;
}
else {
printf("队不空\n");
return 0;
}
}
//出队
int Out_LQueue (LQueue *q, datatype *x) {
QNode *p;
if (Empty_LQueue(q)) {
printf("队空");
return -1;
}
else {
p = q->front->next;
q->front->next = p->next;
x = p->data; //队头元素放在x中 error
// free(p);
if (q->front->next == NULL)
q->rear = q->front; //只有一个元素时,出队后队空,修改队尾元素
printf("出队操作结束\n");
printf("入队数据 : %d\n",q->front->next);
return 1;
}
}
*****用途*****
可以自由控制队列的空间
*****用途*****