#include
#include
typedef struct Node {
int number;
char name[20];
Node *next;
}Qnode;
typedef struct {
Qnode *front;
Qnode *rear;
}LinkQueue;
void Enqueue(LinkQueue *q){
int n;
scanf_s("%d", &n);
for (int i = 1;i <= n;i++) {
Qnode p;
scanf_s("%d %s", &p.number, p.name, 20);
if (q->front== q->rear) {
q->front = &p;
q->rear = q->front->next=NULL;
}
else {
q->rear = &p;
q->rear = q->rear->next=NULL;
}
}
}
void Dequeue(LinkQueue q) {
//if (q->front == q->rear) printf("empty");
printf("%d %s\n", q->front->number, q->front->name);
q->front = q->front->next;
}
int main() {
LinkQueue *q;
q = (LinkQueue)malloc(sizeof(LinkQueue));
q->front = q->rear = NULL;
q->front=q->rear= (Qnode*)malloc(sizeof(Qnode));
q->front->next = q->rear->next = NULL;
Enqueue(q);
Dequeue(q);
}
初學寫的有點亂
Qnode p;
這個肯定不行,必須用malloc動態分配,否則每次都使用同一個存儲。
參考
http://blog.csdn.net/zhangfei0554/article/details/4809156