注册 登录
编程论坛 C语言论坛

链式队列进队、出队操作有误,麻烦大佬帮忙看看

楚川杉 发布于 2020-10-15 13:36, 1253 次点击
麻烦大佬看看,一直输出“队列为空”感觉是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;
    }

   
   
}













只有本站会员才能查看附件,请 登录

5 回复
#2
楚川杉2020-10-15 13:37
这个是带头节点的链表队列
#3
rjsp2020-10-15 15:45
随手写着玩的,可能有bug,仅供参考

程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef int datatype;

typedef struct node
{
    datatype data;
    struct node* next;
} Qnode;

typedef struct
{
    Qnode* front;
    Qnode* rear;
    size_t length;
} LinkQueue;

void initQueue( LinkQueue* q )
{
    q->front = NULL;
    q->rear = NULL;
    q->length = 0;
}

void enQueue( LinkQueue* q, datatype e )
{
    Qnode* s = malloc(sizeof(Qnode));
    s->data = e;
    s->next=NULL;

    if( !q->front )
        q->front = s;
    if( q->rear )
        q->rear->next = s;
    q->rear = s;
    ++q->length;
}

int outQueue( LinkQueue* q, datatype* e )
{
    Qnode* t = q->front;
    if( !t )
        return 0;

    *e = t->data;
   
    q->front = t->next;
    free( t );
    if( !q->front )
        q->rear = NULL;
    --q->length;
    return 1;
}

int main( void )
{
    LinkQueue a;
    initQueue( &a );

    enQueue(&a,1);
    enQueue(&a,3);
    enQueue(&a,5);
    enQueue(&a,6);

    printf( "出队列: " );
    for( datatype x; outQueue(&a,&x); )
        printf( "%d  ", x );
}
#4
楚川杉2020-10-15 19:00
回复 3楼 rjsp
  for( datatype x; outQueue(&a,&x); )
这是什么写法???
#5
rjsp2020-10-15 19:03
回复 4楼 楚川杉
这是C标准建议的做法,
假如你用的是上个世纪老旧的编译器,你可以将变量定义到前面去。
#6
楚川杉2020-10-15 23:42
回复 5楼 rjsp
非常感谢您
1