就多一丶 2022-10-13 21:51 采纳率: 0%
浏览 15

头插法建立单链表,带头结点。输出不对,实在找不到错误的地方了

#include <stdio.h>
#include <malloc.h>
struct lnode *creat(int n);

struct lnode {
int data;
struct lnode *next;
};

int main() {
int n;
struct lnode *q;
printf("输入你想创建的节点数:\n");
scanf("%d", &n);
q = creat(n);
while (q) {
printf("%d", q->data);
q = q->next;
}

return 0;

}

struct lnode *creat(int n) {
int i;
struct lnode *p;
int a;
struct lnode *head = (struct lnode *)malloc(sizeof(struct lnode));
head->next = NULL;
for (i = n - 1; i >= 0; i--) {
p = (struct lnode *)malloc(sizeof(struct lnode));
scanf("%d", &a);
p->data = a;
p->next = head->next;
head->next = p;

}
return head;

}

  • 写回答

2条回答 默认 最新

  • 快乐鹦鹉 2022-10-13 21:55
    关注

    q = creat(n);
    while (q) {
    printf("%d", q->data);
    q = q->next;
    }
    这样把头节点的值都输出来了

    评论

报告相同问题?

问题事件

  • 创建了问题 10月13日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价