鱼C论坛

 找回密码
 立即注册
查看: 659|回复: 6

[已解决]请问关于小甲鱼链表创建的问题

[复制链接]
发表于 2020-5-7 21:00:57 | 显示全部楼层 |阅读模式

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

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

x
大家都知道,小甲鱼创建链表是以输入0为结束标志的。而且是学号。于是我想模仿建立一个以输入“over”为结束标志的,用名字字符串表示。那么问题是,为什么我每次输出结果,name全都是最后一个成员的nam?
最佳答案
2020-5-7 23:18:12
本帖最后由 superbe 于 2020-5-7 23:22 编辑

char* name;
name成员只是一个char *的指针,而你并没有给输入的名字字符串分配内存空间。
这是修改过的代码,包括其它一些修改。
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<malloc.h>

  5. #define LEN sizeof(struct general)  //general结构的大小
  6. #define NLEN 30  //名字最大长度

  7. struct general *creatList();  //创建链表;
  8. void printList(struct general* head);  //打印列表

  9. struct general
  10. {
  11.     char* name;
  12.     int force;
  13.     int intelligence;
  14.     struct general *next;  //指向下一节点
  15. };

  16. int n = 0;

  17. struct general *creatList()  //创建链表
  18. {
  19.     struct general *head = NULL;
  20.     struct general *p1, *p2;
  21.     char sname[NLEN];

  22.     printf("Enter the name: ");
  23.     scanf("%s", sname);
  24.     while (strcmp(sname, "over") != 0)
  25.     {
  26.         p1 = (struct general *)malloc(LEN);
  27.         p1->name = (char *)malloc(strlen(sname) + 1);  //删除节点时要释放分配的内存
  28.         strcpy(p1->name, sname);
  29.         printf("Enter the force: ");
  30.         scanf("%d", &p1->force);
  31.         printf("Enter the intelligence: ");
  32.         scanf("%d", &p1->intelligence);
  33.         p1->next = NULL;
  34.         if (n == 0)
  35.             head = p1;
  36.         else
  37.             p2->next = p1;
  38.         p2 = p1;
  39.         n++;
  40.         printf("\nEnter the name: ");
  41.         scanf("%s", sname);
  42.     }

  43.     return head;
  44. }

  45. void printList(struct general* head)
  46. {
  47.     struct  general* pMove;

  48.     pMove = head;
  49.     while (pMove)
  50.     {
  51.         printf("name:%s   武力:%d  智力:%d\n", pMove->name, pMove->force, pMove->intelligence);
  52.         pMove = pMove->next;
  53.     }
  54.     printf("总人数 = %d\n", n);
  55. }

  56. int main()
  57. {
  58.     struct general *gen;

  59.     gen = creatList();
  60.     printList(gen);
  61.     system("pause");
  62.     return 0;
  63. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-7 21:02:02 | 显示全部楼层
本帖最后由 zqdlly 于 2020-5-7 23:21 编辑

源码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

#define LEN sizeof(struct general) //general结构的大小

struct general *creatList();//创建链表;
void printList(struct general* head);//打印列表
struct general
{
    char* name;
    int force;
    int intelligence;
    struct general *next;//对链表很重要
};
int n;
struct general *creatList()//创建链表;
{
    struct general *head;
    struct general *p1,*p2;

    p1=p2=(struct general *)malloc(LEN);//LEN general结构的大小,这里p1和p2都指向第一个申请的新节点

    printf("Enter the name:");
    scanf("%s",p1->name);
    printf("Enter the force:");
    scanf("%d",&p1->force);
    printf("Enter the intelligence:");
    scanf("%d",&p1->intelligence);

    head=NULL;
    n=0;//计数器,记录存放个数

    while(strcmp(p1->name,"over")!=0 )//当输入0的时候退出循环
    {
        n++;
        if(n==1)//只有第一次有效
        {
            head=p1;
        }
        else//
        {
            p2->next=p1;
        }
        p2=p1;

        p1=(struct general *)malloc(LEN);//p1再申请一个节点

        printf("\nEnter the name:");
        scanf("%s",p1->name);
        printf("Enter the force:");
        scanf("%d",&p1->force);
        printf("Enter the intelligence:");
        scanf("%d",&p1->intelligence);

    }
    p2->next=NULL;

    return head;//返回链表头结点的指针,相当于数组的首地址。链表就是强化版的数组,比如插入等操作,数组很难,链表容易ccccc
};
void printList(struct general* head)
{
        struct  general* pMove;
        pMove = head;
    if(head)
        {
             do
                {
                    printf("name:%s   武力:%d  智力:%d\n", pMove->name, pMove->force, pMove->intelligence);
                    pMove = pMove->next;
                }while(pMove);
        }
    printf("总人数=%d\n",n);
}

main()
{
    struct general *gen;

    gen=creatList();
    printList(gen);//打印链表需要自定义函数,c没有内置
    system("pause");

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 22:44:01 | 显示全部楼层
D:\123.png
这样写(略了一些功能),你这看的有点头疼,数据结构还是看书和博客比较好。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 22:45:49 | 显示全部楼层
// 下面是链表的结构
struct node {
        char* name;
    int force;
    int intelligence;
    struct general *next;
};

struct list{
        node* head;
        int count;// 记录存放个数
};

//----------
//↓维护链表的函数

void CreateList();
void DestroyList();
void makeNode();
void AddNode();

//---------------------
//伪代码输入过程↓
while scanf x true
        addNode(x)
else break

void addNode(参数列表x) {
        makeNode();
        //判空 和 next等操作
}

我图片呢。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-5-7 23:18:12 | 显示全部楼层    本楼为最佳答案   
本帖最后由 superbe 于 2020-5-7 23:22 编辑

char* name;
name成员只是一个char *的指针,而你并没有给输入的名字字符串分配内存空间。
这是修改过的代码,包括其它一些修改。
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<malloc.h>

  5. #define LEN sizeof(struct general)  //general结构的大小
  6. #define NLEN 30  //名字最大长度

  7. struct general *creatList();  //创建链表;
  8. void printList(struct general* head);  //打印列表

  9. struct general
  10. {
  11.     char* name;
  12.     int force;
  13.     int intelligence;
  14.     struct general *next;  //指向下一节点
  15. };

  16. int n = 0;

  17. struct general *creatList()  //创建链表
  18. {
  19.     struct general *head = NULL;
  20.     struct general *p1, *p2;
  21.     char sname[NLEN];

  22.     printf("Enter the name: ");
  23.     scanf("%s", sname);
  24.     while (strcmp(sname, "over") != 0)
  25.     {
  26.         p1 = (struct general *)malloc(LEN);
  27.         p1->name = (char *)malloc(strlen(sname) + 1);  //删除节点时要释放分配的内存
  28.         strcpy(p1->name, sname);
  29.         printf("Enter the force: ");
  30.         scanf("%d", &p1->force);
  31.         printf("Enter the intelligence: ");
  32.         scanf("%d", &p1->intelligence);
  33.         p1->next = NULL;
  34.         if (n == 0)
  35.             head = p1;
  36.         else
  37.             p2->next = p1;
  38.         p2 = p1;
  39.         n++;
  40.         printf("\nEnter the name: ");
  41.         scanf("%s", sname);
  42.     }

  43.     return head;
  44. }

  45. void printList(struct general* head)
  46. {
  47.     struct  general* pMove;

  48.     pMove = head;
  49.     while (pMove)
  50.     {
  51.         printf("name:%s   武力:%d  智力:%d\n", pMove->name, pMove->force, pMove->intelligence);
  52.         pMove = pMove->next;
  53.     }
  54.     printf("总人数 = %d\n", n);
  55. }

  56. int main()
  57. {
  58.     struct general *gen;

  59.     gen = creatList();
  60.     printList(gen);
  61.     system("pause");
  62.     return 0;
  63. }
复制代码

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

使用道具 举报

 楼主| 发表于 2020-5-7 23:24:34 | 显示全部楼层
pupil337 发表于 2020-5-7 22:45
// 下面是链表的结构
struct node {
        char* name;

谢谢,更规范的模式,学习了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-7 23:47:02 | 显示全部楼层
superbe 发表于 2020-5-7 23:18
char* name;
name成员只是一个char *的指针,而你并没有给输入的名字字符串分配内存空间。
这是修改过的 ...


感谢大佬细心指教!好好研究一下改动的细节
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-5 07:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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