鱼C论坛

 找回密码
 立即注册
查看: 2268|回复: 3

[已解决]要疯了,求助大神一个简单的问题,为什么我这个算法在vc6.0里运行不了

[复制链接]
发表于 2019-6-3 12:41:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
        int num;
        char name;
        struct list *next;
}node;//定义一个链表存储结构;

void init_list(node *L,node *temp)
{       
        int item;
        char item1;
        int n=1;
        while(1)
        {       
                printf("请输入学生学号(输入0表示结束):\n");
                scanf("%d",&item);
                if(item == 0)
                        break;
                printf("请输入学生姓名:\n");
                scanf("%s",&item1);
                L = (node*)malloc(sizeof(struct list));
                L->num = item;
                L->name = item1;
                if(n==1)
                {       
                        temp = (node*)malloc(sizeof(struct list));
                        temp = L;
                        n++;
                }
                L = L->next;
        }
        L = NULL;
}

void print_list(node *temp)
{       
        while(temp != NULL)
        {
        printf("%d", temp->num);
        printf("%c", temp->name);
        temp = temp->next;
        }
}

int main()
{
node *L;
node *temp;
init_list(L,temp);
print_list(temp);
return 0;
}
求助大神帮帮忙,一直改都是错的。要疯了。
最佳答案
2019-6-4 00:05:49
星星没有泪 发表于 2019-6-3 16:28
就是语法上没有错误,逻辑上错误了,程序可以运行,但是要的结果与我不符,我是想创建一个单链表,然后输 ...

用字符数组
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct node
  5. {
  6.         int num;
  7.         char name[64];
  8.         struct node *next;
  9. } node, list_t;

  10. list_t *init_list(void)
  11. {
  12.         node *head = (node *)malloc(sizeof(node));
  13.         memset(head, 0, sizeof(node));

  14.         node *p = head;
  15.         while(1)
  16.         {
  17.                 p->next = (node *)malloc(sizeof(node));
  18.                 printf("请输入学生学号(输入0表示结束): ");
  19.                 scanf("%d", &p->next->num);
  20.                 if(p->next->num == 0)
  21.                         break;
  22.                 printf("请输入学生姓名: ");
  23.                 scanf("%s", p->next->name);

  24.                 p = p->next;
  25.         }
  26.         free(p->next);
  27.         p->next = NULL;
  28.         return head;
  29. }

  30. void deinit_list(list_t *head)
  31. {
  32.         node *p = head;
  33.         while(p)
  34.         {
  35.                 node *temp = p;
  36.                 p = p->next;
  37.                 free(temp);
  38.         }
  39. }

  40. void print_list(list_t *head)
  41. {
  42.         node *p = head->next;
  43.         while(p)
  44.         {
  45.                 printf("%d %s\n", p->num, p->name);
  46.                 p = p->next;
  47.         }
  48. }

  49. int main(void)
  50. {
  51.         list_t *head = init_list();
  52.         print_list(head);
  53.         deinit_list(head);
  54.         return 0;
  55. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-3 13:37:54 | 显示全部楼层
1.什么错?你指望我复制你的代码,然后粘贴到vc6看报错信息吗?
我没有vc6,所以不要对别人的环境做任何假设,把你的问题描述清楚

2.贴报错信息
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-3 16:28:21 | 显示全部楼层
人造人 发表于 2019-6-3 13:37
1.什么错?你指望我复制你的代码,然后粘贴到vc6看报错信息吗?
我没有vc6,所以不要对别人的环境做任何假 ...

就是语法上没有错误,逻辑上错误了,程序可以运行,但是要的结果与我不符,我是想创建一个单链表,然后输入学号和对应的名字,并把这些学号名字都打印出来。我刚刚又仔细研究了一下,发现应该是数组那边出错了,我那边定义的是一个字符型变量,而输入输出的是字符串变量,这边应该是错误的。
然后我想向您请教一下,c语言中怎么把输入的字符串赋值到L->name里面去,因为c语言中字符串的表示方法好像只有字符型数组与字符型指针的形式,而两者都没有办法实现这个。   说的更直白一点,就是我输入张三,然后怎么赋值给name里去呢?
不好意思麻烦大神啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-4 00:05:49 | 显示全部楼层    本楼为最佳答案   
星星没有泪 发表于 2019-6-3 16:28
就是语法上没有错误,逻辑上错误了,程序可以运行,但是要的结果与我不符,我是想创建一个单链表,然后输 ...

用字符数组
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct node
  5. {
  6.         int num;
  7.         char name[64];
  8.         struct node *next;
  9. } node, list_t;

  10. list_t *init_list(void)
  11. {
  12.         node *head = (node *)malloc(sizeof(node));
  13.         memset(head, 0, sizeof(node));

  14.         node *p = head;
  15.         while(1)
  16.         {
  17.                 p->next = (node *)malloc(sizeof(node));
  18.                 printf("请输入学生学号(输入0表示结束): ");
  19.                 scanf("%d", &p->next->num);
  20.                 if(p->next->num == 0)
  21.                         break;
  22.                 printf("请输入学生姓名: ");
  23.                 scanf("%s", p->next->name);

  24.                 p = p->next;
  25.         }
  26.         free(p->next);
  27.         p->next = NULL;
  28.         return head;
  29. }

  30. void deinit_list(list_t *head)
  31. {
  32.         node *p = head;
  33.         while(p)
  34.         {
  35.                 node *temp = p;
  36.                 p = p->next;
  37.                 free(temp);
  38.         }
  39. }

  40. void print_list(list_t *head)
  41. {
  42.         node *p = head->next;
  43.         while(p)
  44.         {
  45.                 printf("%d %s\n", p->num, p->name);
  46.                 p = p->next;
  47.         }
  48. }

  49. int main(void)
  50. {
  51.         list_t *head = init_list();
  52.         print_list(head);
  53.         deinit_list(head);
  54.         return 0;
  55. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 16:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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