| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2558 人关注过本帖
标题:求助: ubuntu 18.04 gcc 编译器 通过使用 顺序存储的循环队列 实现数据 ...
取消只看楼主 加入收藏
Jessica_Rong
Rank: 1
来 自:河北唐山
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-5-12
结帖率:83.33%
收藏
 问题点数:0 回复次数:1 
求助: ubuntu 18.04 gcc 编译器 通过使用 顺序存储的循环队列 实现数据的进队出队
我的代码已经在老师讲解之后修改过了, 但是仍然不能正确输出,请求指点


-------------     queue.h     ------------------
#ifndef QUEUE_H__
#define QUEUE_H__
#define QUEUESIZE    5

typedef struct
{
    int data[QUEUESIZE];
    int head;
    int tail;
}QUEUE;

QUEUE *queue_create();
void queue_create1(QUEUE **);
int queue_isempty(QUEUE *);
int queue_isfull(QUEUE *);
int queue_setempty(QUEUE *);
int queue_en(QUEUE *,int x);//进队
int queue_de(QUEUE *,int *x);//出队
void queue_show(QUEUE *);
void queue_destroy(QUEUE *);

#endif

-------------     queue.c     ------------------
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
QUEUE *queue_create()
{
    QUEUE *que;
    que = malloc(sizeof(*que));
    if(que == NULL)
        return NULL;
    que->head = que->tail = 0;//que->data[0];
    //使head,tail有合理指向
    return que;
}
void queue_create1(QUEUE **que)
{
    *que = malloc(sizeof(QUEUE));
    if(*que == NULL)
        return ;
    (*que)->head = (*que)->tail = 0;//(*que)->data[0];
    //取二级指针所指向的一级指针中的head
    return ;
}
int queue_isempty(QUEUE *que)
{
    if(que->head == que->tail)
        return 1;
    return 0;
}
int queue_isfull(QUEUE *que)
{
    if((que->tail + 1) % QUEUESIZE == que->head)//teacher
    //if(que->head == que->tail + 1) //me
        return 1;
    return 0;
}
int queue_setempty(QUEUE *que)
{
    que->head = que->tail;
    return 0;
}
int queue_en(QUEUE *que,int x)//进队
{
    if(queue_isfull(que))//1为真,队已满
    {
        printf("en_iffull error\n");
        return -1;
    }
//if((que->tail+1)%QUEUESIZE == 0)//好像并不需要
        que->tail = (que->tail + 1) % QUEUESIZE;
        //tail指向队列的最大长度时,更改指向,使队列构成循环队列
/*
   
    if((que->tail+1) % (QUEUESIZE-1) == 0)
        que->tail = (que->tail + 1) % QUEUESIZE;
  */
//  else
//  {
//      que->tail += 1;
        que->data[que->tail] = x;
        printf("测试 en_x %d\n",que->data[que->tail]);
        return 0;
//  }
}
int queue_de(QUEUE *que,int *x)//出队
{
    if(queue_isempty(que))//队已空
    {
        printf("de_isempty error \n");
        return -1;
    }
//  if((que->head+1)%QUEUESIZE == 0)//好像并不需要
        que->head = (que->head + 1) % QUEUESIZE;
        //head指向队列的最大长度时,更改指向,使队列构成循环队列
        *x = que->data[que->head-1];
//  que->head -= 1;//好像并不需要
    return 0;
}
void queue_show(QUEUE *que)
{
    int i = (que->head + 1 ) % QUEUESIZE;//
    if(queue_isempty(que))//队已空
    {
        printf("show_isempty error \n");
        return ;
    }
    for( ; i != que->tail;i = (i+1)%QUEUESIZE)//i++)  i++可能会造成数组越界
        printf("%d ",que->data[i]);
    printf("\n");
}
void queue_destroy(QUEUE *que)
{
    free(que);
}


-------------     main.c     ------------------

#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
int main()
{
    int arr[]={9,8,6,2};
    int i,ret=0;
    QUEUE *qe;
    qe = queue_create();
    printf("测试 create () \n\n");
    //queue_create1(&qe);
    if(qe == NULL)
    {
        printf("测试 Create() error!\n\n");
        exit(1);
    }
    for(i = 0;i < sizeof(arr) / sizeof(*arr);i++)
        ret = queue_en(qe,arr[i]);
    printf("测试 en () : %d\n\n",ret);
 
   printf("输出\n");
    queue_show(qe);
    printf("\n");

    printf("测试 show () \n\n");
}


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

搜索更多相关主题的帖子: queue int head return printf 
2018-08-07 16:08
Jessica_Rong
Rank: 1
来 自:河北唐山
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-5-12
收藏
得分:0 
已解决
1.queue_show()中
   
    for( ; i != que->tail;i = (i+1)%QUEUESIZE)//i++)  i++可能会造成数组越界
        printf("%d ",que->data[i]);
    printf("%d ",que->data[i]);//把最后一个i == que->tail 打印出来
    printf("\n");
2.后来在 queue_de()中也发现一个错误
   
//  if((que->head+1)%QUEUESIZE == 0)//好像并不需要
        que->head = (que->head + 1) % QUEUESIZE;
            //head指向队列的最大长度时,更改指向,使队列构成循环队列
        *x = que->data[que->head];//-1]; //如果-1 head就还是上一句代码的指向
//  que->head -= 1;//好像并不需要
    return 0;

有遇到相同问题的朋友可以参考一下
2018-08-07 16:50
快速回复:求助: ubuntu 18.04 gcc 编译器 通过使用 顺序存储的循环队列 实 ...
数据加载中...
 
   



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

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