| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1013 人关注过本帖
标题:链式队列进队、出队操作有误,麻烦大佬帮忙看看
只看楼主 加入收藏
楚川杉
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2020-3-10
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:5 
链式队列进队、出队操作有误,麻烦大佬帮忙看看
麻烦大佬看看,一直输出“队列为空”感觉是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;
    }

    
    
}













图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: 链式 next void 队列 int 
2020-10-15 13:36
楚川杉
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2020-3-10
收藏
得分:0 
这个是带头节点的链表队列
2020-10-15 13:37
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
随手写着玩的,可能有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 );
}
2020-10-15 15:45
楚川杉
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2020-3-10
收藏
得分:0 
回复 3楼 rjsp
  for( datatype x; outQueue(&a,&x); )
这是什么写法???
2020-10-15 19:00
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 4楼 楚川杉
这是C标准建议的做法,
假如你用的是上个世纪老旧的编译器,你可以将变量定义到前面去。
2020-10-15 19:03
楚川杉
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2020-3-10
收藏
得分:0 
回复 5楼 rjsp
非常感谢您
2020-10-15 23:42
快速回复:链式队列进队、出队操作有误,麻烦大佬帮忙看看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013303 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved