| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1490 人关注过本帖
标题:循环队列想要插入一个元素结果变成了一个新的队列。为什么呀,求大神解答谢 ...
只看楼主 加入收藏
不要做咸鱼
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2018-3-5
结帖率:0
收藏
 问题点数:0 回复次数:2 
循环队列想要插入一个元素结果变成了一个新的队列。为什么呀,求大神解答谢谢。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
#define OK 1
#define NO 2
typedef int Status;
typedef char ElemType;
typedef struct Queue
{
    ElemType *base;//队列基址
    int front;
    int rear;
}Queue;
Status InitQueue(Queue *q)//初始化一个空队列
{
    q->base=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
    q->front=q->rear=0;
    return OK;
}
void EnQueue(Queue *q,ElemType e)//插入元素e为新的队尾元素
{
    if(((q->rear+1)%MAXSIZE)==q->front)
    {
        printf("\nThe queue is full!\n");
    }
    q->base[q->rear]=e;
    q->rear=(q->rear+1)% MAXSIZE;
}
Status DeQueue(Queue *q,ElemType *e)//删除队头元素用e返回其值
{
    if(q->front!=q->rear)
    {
        q->front=(q->front+1)%MAXSIZE;
        *e=q->base[q->front];
        return OK;
    }
    else
        return NO;
}
void Print(Queue *q)//打印整个队列
{
    if(((q->rear+1)%MAXSIZE)==q->front)
    {
        printf("\nThe queue is full!\n");
    }
    while(q->front!=q->rear)
    {
      printf("%c",q->base[q->front]);
      q->front=(q->front+1)% MAXSIZE;
    }
}
Status DestroyQueue(Queue *q)//删除队列
{
    if (q == NULL)
    return NO;
    if (q->base)
    {
        free(q->base);
    }
    printf("The queue has been destroyed\n");
    return OK;
}
int main()
{
    Queue q;
    ElemType e,a;
    InitQueue(&q);
    printf("Enter some characters:");
    e=getchar();
    while(e!='\n')
    {
        EnQueue(&q,e);
        e=getchar();
    }
    printf("Print this queue:");
    Print(&q);
    printf("\nEnter one character'x':");
    EnQueue(&q,'x');
    Print(&q);
    printf("\nDletes the first character\n");
    DeQueue(&q,&a);
    printf("%c\n",a);
    DestroyQueue(&q);
    return 0;
}
搜索更多相关主题的帖子: 队列 Queue base return printf 
2018-11-01 08:58
不要做咸鱼
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2018-3-5
收藏
得分:0 
回复 2楼 顾三白
执行插入x之后再打印整个队列就只有x了。然后取队头那个什么也没有输出。
2018-11-01 12:17
不要做咸鱼
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2018-3-5
收藏
得分:0 
啊我现在知道了原因了,因为我的打印函数把q的首地址改变了。
改变后的打印函数:
void Print(Queue &q)//打印整个队列
{
    int k=q.front;
    while(k%MAXSIZE!=q.rear)
    {
      printf("%c",q.base[k]);
      k++;
    }
}
2018-11-10 09:59
快速回复:循环队列想要插入一个元素结果变成了一个新的队列。为什么呀,求大神解 ...
数据加载中...
 
   



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

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