| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2373 人关注过本帖
标题:求助: ubuntu 18.04 gcc 编译器 通过使用 顺序存储的循环队列 实现数据 ...
只看楼主 加入收藏
Jessica_Rong
Rank: 1
来 自:河北唐山
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-5-12
结帖率:83.33%
收藏
 问题点数:0 回复次数:3 
求助: 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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
程序代码:
int queue_en( QUEUE* que, int x )
{
    if( queue_isfull(que) )
    {
        printf("en_iffull error\n");
        return -1;
    }

    que->data[que->tail] = x;
    printf( "测试 en_x %d\n", que->data[que->tail] );

    que->tail = (que->tail + 1) % QUEUESIZE;
    return 0;
}

int queue_de( QUEUE* que, int* x )
{
    if( queue_isempty(que) )
    {
        printf("de_isempty error \n");
        return -1;
    }

    *x = que->data[que->head];
    que->head = (que->head + 1) % QUEUESIZE;

    return 0;
}

void queue_show( QUEUE* que )
{
    for( int i=que->head; i!=que->tail; i=(i+1)%QUEUESIZE )
        printf("%d ",que->data[i] );
    printf("\n");
}
2018-08-07 17:04
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
//自己写的

#include<stdio.h>
#define max 3

typedef struct
{
    int data[max];
    int front;
    int rear;
}SeqQueue;

void InitSeqQueue(SeqQueue *Q)
{
    Q->front=Q->rear=0;
}

void EnterQueue(SeqQueue *Q,int a[],int x)
{
    if((Q->rear+1)%max==Q->front)
    {
        printf("队满!\n");
        return;
    }
    else
    {
        for(int i=0;i<x;i++)
        {
            Q->data[Q->rear]=a[i];
            Q->rear=(Q->rear+1)%max;
        }
    }
}

void DeleteQueue(SeqQueue *Q)
{
    if(Q->rear==Q->front)
    {
        printf("队列为空!\n");
        return ;
    }
    else
    {
        Q->front=(Q->front+1)%max;
    }
}

void PrintQueue(SeqQueue *Q)
{
    for(int i=Q->front;i<Q->rear;i++)
    {
        printf("%-3d",Q->data[i]);
    }
    printf("\n");
}

int LengthSeqQueue(SeqQueue Q)
{
    return (Q.rear-Q.front+max)%max;
}

int main()
{
    SeqQueue Q;
    int x,length;
    InitSeqQueue(&Q);
    printf("请输入要入队的元素的个数:\n");
    scanf("%d",&x);
    int a[x];
    printf("请输入要入队的元素:\n") ;
    for(int i=0;i<x;i++)
        scanf("%d",&a[i]);
    EnterQueue(&Q,a,x);
    DeleteQueue(&Q);
    printf("队列中的元素为:\n");
    PrintQueue(&Q);
    printf("队列的长度为:\n");
    length=LengthSeqQueue(Q);
    printf("%-3d\n",length);
    return 0;
}
2018-08-07 18:34
快速回复:求助: ubuntu 18.04 gcc 编译器 通过使用 顺序存储的循环队列 实 ...
数据加载中...
 
   



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

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