#include<stdio.h> #include<stdlib.h> typedef struct LinkQueue { int data; struct LinkQueue *next; }LinkQueue,*LinkStr; typedef struct{ LinkStr front; LinkStr rear; }Link; void InitQueue(Link* q); void EnterQueue(Link* q); void DeleteQueue(Link* q); int main(void) { Link queue; InitQueue(&queue); EnterQueue(&queue); DeleteQueue(&queue); //disqueue(queue); return 0; } //初始化 void InitQueue(Link* q) { q->front=q->rear=(LinkStr)malloc(sizeof(LinkQueue)); if ((q->front = q->rear) == NULL) { printf("分配不成功"); } else { q->front->next=q->rear->next= NULL; } } //入队列 void EnterQueue(Link* q) { LinkQueue *number = (LinkQueue*)malloc(sizeof(LinkQueue)); if (number != NULL) { int n = 0; int flag = 1; printf("请输入元素:"); while (flag) { scanf("%d", &n); if (n != -1) { number->data = n; number->next = NULL; q->rear->next = number; q->rear = number; } else { flag = 0; } } } } //出队列 void DeleteQueue(Link* q) { int* x; x = (int*)malloc(sizeof(int)); if (q->front == q->rear) { printf("该队列为空队列"); } LinkQueue *p; p = (LinkQueue*)malloc(sizeof(LinkQueue)); p = q->front->next; *x = p->data; printf("出队列的元素为:%d", *x); q->front->next = p->next; if (q->rear == p) { q->rear = q->front; } free(p); }
我输入1 3 2
出队的元素为何是2,不是1
顺序问题吧,rear和front某个位置写反了,至于具体的话我代码再具体看
可能是这里的问题?
p = q->front->next; //改为p=q->rear
*x = p->data;
printf("出队列的元素为:%d", *x);
还是不行