鱼C论坛

 找回密码
 立即注册
查看: 2867|回复: 2

为什么呀?scanf有问题吗?

[复制链接]
发表于 2022-12-16 18:58:53 | 显示全部楼层 |阅读模式
20鱼币
输入数字后就
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node{
  4.         char s;
  5.         struct Node *prior;
  6.         struct Node *next;
  7. };

  8. struct Node *gengerate_letter(void);
  9. struct Node *move(struct Node *head, int i);
  10. void print_Node(struct Node *head);
  11. void clerr(struct Node **head);

  12. int main(){
  13.         int i;
  14.         struct Node *head;

  15.         head = gengerate_letter();

  16.         printf("请输入移动的距离:");
  17.         printf("-----------\n");
  18.         scanf("%d", &i);
  19.         printf("-----------");

  20.         head = move(head, i);
  21.         print_Node(head);

  22.         clerr(&head);
  23. }

  24. struct Node *gengerate_letter(void){
  25.         int i;
  26.         struct Node *head = NULL;
  27.         struct Node *node, *now;

  28.         for (i = 0; i < 26; i++){
  29.                 now = (struct Node *)malloc(sizeof(struct Node));
  30.                 if(!now){
  31.                         exit(0);
  32.                 }
  33.                 now->s = 'A' + i;

  34.                 if (head == NULL){
  35.                         head = now;
  36.                         node = head;
  37.                 }
  38.                 else{
  39.                         node->next = now;
  40.                         now->prior = node;
  41.                         node = now;
  42.                 }
  43.         }
  44.         node->next = head;
  45.         head->prior = node;

  46.         return head;
  47. }

  48. struct Node *move(struct Node *head, int i){
  49.         int a;
  50.         struct Node *node;

  51.         node = head;
  52.         for (a = 0; a != i; ){
  53.                 if (a < i){
  54.                         node = node->next;
  55.                         a++;
  56.                 }
  57.                 else{
  58.                         node = node->prior;
  59.                         a--;
  60.                 }
  61.         }

  62.         return node;
  63. }

  64. void print_Node(struct Node *head){
  65.         struct Node *node;

  66.         node = head;
  67.         while (node->next != head){
  68.                 printf("%c", node->s);
  69.                 node = node->next;
  70.         }
  71. }

  72. void clerr(struct Node **head){//将单链表清空
  73.         struct Node *node, *temp;

  74.         node = *head;
  75.         while (node->next){
  76.                 temp = node;
  77.                 node = node->next;
  78.                 free(temp);
  79.         }
  80.         free(node);
  81. }

复制代码
卡了 CX0`71BTBF3]NR1T%VFFGSS.png

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-16 19:59:20 | 显示全部楼层
好像是清空链表出问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-16 20:36:05 | 显示全部楼层
没问题了,重新写了一遍删除函数 [J17X4J}KA[4A751H$CRJZW.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 22:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表