链式队列进队、出队操作有误,麻烦大佬帮忙看看
											麻烦大佬看看 ,一直输出“队列为空”感觉是front和rear的指向问题,但是又不看不出哪儿里错了
,一直输出“队列为空”感觉是front和rear的指向问题,但是又不看不出哪儿里错了
 程序代码:
程序代码:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef int datatype; 
typedef struct node{
    datatype data;
    struct node *next;
}Qnode; 
typedef struct{
    Qnode *rear;
    Qnode *front;
    int length;
}LinkQueue;
int main(int argc, char *argv[]) {
    void initQueue(LinkQueue *q);
    void enQueue(LinkQueue *q,datatype e);
    int outQueue(LinkQueue *q,datatype *e);
    
    LinkQueue a;
    datatype x;
    int i;
    initQueue(&a);
    enQueue(&a,1);
    enQueue(&a,3);
    enQueue(&a,5);
    enQueue(&a,6);
    printf("出队列:");
    for(i=0;i<3;i++){
        if(outQueue(&a,&x)==1){
            outQueue(&a,&x); 
            printf("%d  ",x);
        }
    
    }
    return 0;
}
void initQueue(LinkQueue *q){
    //没有头节点的链表
    // q->front=q->rear=(Qnode *)malloc(sizeof(Qnode));
    //q->front->data=NULL; 
    Qnode *head;
    head=(Qnode *)malloc(sizeof(Qnode));
    head->next=NULL;
    q->front=head;
    q->rear=q->front;
    q->length=0;
} 
void enQueue(LinkQueue *q,datatype e){//链式队列 进队列操作没有上限,可以返回值void 
    Qnode *s;
    s->data=e;
    s->next=NULL;
    q->rear->next=s;
    q->rear=s;
    q->length++;
}
int outQueue(LinkQueue *q,datatype *e){
    Qnode *s;
    if(q->rear==q->front){
        printf("队列已空\n");
        return 0;
    }
    else{
        s=q->front->next;
        *e=s->data;
        //如果没有头节点 s=q->front; 
        q->front->next=s->next;
        if(s->next==NULL){//队列只有一个元素的情况 
            q->rear=q->front;//防止q->rear成为野指针 
        } 
        free(s);
        q->length--; 
        return 1;
    }
    
    
}



 
											





 
	    

 
	
 
											

