m0_67529443 2022-04-27 21:18 采纳率: 100%
浏览 17
已结题

[c语言]请问我写的链队列哪有问题,为什么输出的值不对

我出队列的值是对的,但out出来的值不对
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef int elemtype;
typedef struct Qnode
{
struct Qnode *next;
elemtype data;
}Qnode,*queue;
typedef struct
{
queue rear;
queue front;
}Lqueue;
Lqueue s;
void initqueue(Lqueue *s)
{
s->front=s->rear=(queue)malloc(sizeof(Qnode));
if(!s->front)
printf("\n分配失败");
s->front->next=NULL;
s->rear=s->front;
}
void out(Lqueue s)
{
Qnode *p;
elemtype e;
p=s.front->next;
while(p<=s.rear)
{

    printf("\n%4d",*p);
    p++;
    
}

}
void enqueue(Lqueue *s,elemtype e)
{
queue p;
p=(queue)malloc(sizeof(Qnode));
p->data=e;
p->next=NULL;
s->rear->next=p;
s->rear=p;
printf("\n入队元素:%d",*p);
}
elemtype dequeue(Lqueue *s)
{
queue p;
elemtype e;
p=s->front->next;
e=p->data;
s->front->next=p->next;
free(p);
return(e);
}
void main()
{

int k; elemtype e,x; char ch;

initqueue( &s);
do { printf("\n");
printf("\n 1. 数据元素 e 进队列 ");
printf("\n 2. 出队一个元素,返回其值");
printf("\n 3. 结束程序运行");
printf("\n 请输入您的选择 (1,2,3)");
scanf("%d",&k);
switch(k)
{ case 1:{ printf("\n 进队 e=?"); scanf("%d",&e);
enqueue(&s,e); out(s);
} break;
case 2:{ x= dequeue(&s);
printf("\n 出队元素 : %d", x);
out(s);
} break;
case 3: exit(0);
} /* switch */
}while(k>=1 && k<3);
printf("\n 再见!");
printf("\n 打回车键,返回。"); ch=getch();
}

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-04-28 00:23
    关注

    修改见注释,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    typedef int elemtype;
    typedef struct Qnode
    {
        struct Qnode *next;
        elemtype data;
    }Qnode,*queue;
    typedef struct
    {
        queue rear;
        queue front;
    }Lqueue;
    Lqueue s;
    void initqueue(Lqueue *s)
    {
        s->front=s->rear=(queue)malloc(sizeof(Qnode));
        if(!s->front)
           printf("\n分配失败");
        s->front->next=NULL;
        //s->rear=s->front;   修改
    }
    void out(Lqueue s)
    {
        Qnode *p;
        elemtype e;
        p=s.front;  //p=s.front->next; 修改
        while(p<s.rear)  //(p<=s.rear) 修改
        {
             printf("\n%4d",p->next->data);
             p = p->next;
             // printf("\n%4d",*p);  修改
             //p++;                  修改
        }
    }
    void enqueue(Lqueue *s,elemtype e)
    {
        queue p;
        p=(queue)malloc(sizeof(Qnode));
        p->data=e;
        p->next=NULL;
        s->rear->next=p;
        s->rear=p;
        printf("\n入队元素:%d",p->data);
        //printf("\n入队元素:%d",*p);  修改
    }
    elemtype dequeue(Lqueue *s)
    {
        queue p;
        elemtype e = -1;
        if (s->front == s->rear) return e;
        p=s->front;  //p=s->front->next;  修改
        e=p->next->data;  //e=p->data;    修改
        s->front=p->next; //s->front->next=p->next; 修改
        free(p);
        return(e);
    }
    void main()
    {
        int k;
        elemtype e,x;
        char ch;
        initqueue( &s);
        do {
             printf("\n");
             printf("\n 1. 数据元素 e 进队列 ");
             printf("\n 2. 出队一个元素,返回其值");
             printf("\n 3. 结束程序运行");
             printf("\n 请输入您的选择 (1,2,3)");
             scanf("%d",&k);
             switch(k)
             {
               case 1:{ printf("\n 进队 e=?");
                        scanf("%d",&e);
                        enqueue(&s,e);
                        out(s);
                       }
                       break;
               case 2:{ x= dequeue(&s);
                        printf("\n 出队元素 : %d", x);
                        out(s);
                      }
                      break;
               case 3: exit(0);
             } /* switch */
        }while(k>=1 && k<3);
        printf("\n 再见!");
        printf("\n 打回车键,返回。");
        ch=getch();
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月7日
  • 已采纳回答 4月29日
  • 创建了问题 4月27日

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂