| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 576 人关注过本帖
标题:我都不好意思说自己学过链表了,帮我改下好吗?
只看楼主 加入收藏
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
结帖率:91.43%
收藏
 问题点数:0 回复次数:9 
我都不好意思说自己学过链表了,帮我改下好吗?
程序代码:
#include <stdio.h>
#include <string.h>

#define LENGTH 20

typedef struct SqList
{
    char name[LENGTH];
    struct SqList *next;
}SqList;

/*链表的初始化和输出*/
SqList *InitSqList(SqList *h);
void ShowList(SqList *h);

int main()
{
    SqList *head = NULL;
    head = InitSqList(head);
    ShowList(head);
    return 0;
}

/*链表的初始化*/
SqList *InitSqList(SqList *h)
{
    SqList *p1, *p2;
    p1 = p2 = (SqList *)malloc(sizeof(SqList));
    scanf("%s", p2->name);
    p2 = p2->next;
    while(!strcmp(p2->name, "#"))
    {
        if(h = NULL)
        {
            h = p2;
        }
        else
        {
            p1->next = p2;
        }
        p1 = p2;
        p2 = (SqList*)malloc(sizeof(SqList));
        if(p2 != NULL)
        {
            scanf("%s", p2->name);
            p2 = p2->next;
        }
    }
    p2->next = NULL;
    return h;
}

/*链表的输出*/
void ShowList(SqList *h)
{
    SqList *temp = h;
    while (temp->next != NULL)
    {
        printf("%s\t", temp->name);
        temp = temp->next;
    }
}
搜索更多相关主题的帖子: next color 
2011-04-16 16:28
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
错得很明显,所以这里我就解释错在哪儿了

o(∩∩)Linux & Python 群:187367181
2011-04-16 16:29
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
收藏
得分:0 
什么不好意思的,哪个不是这么过来的
程序代码:
#include <stdio.h>
#include <string.h>

#define LENGTH 20

typedef struct SqList
{
    char name[LENGTH];
    struct SqList *next;
}SqList;

/*链表的初始化和输出*/
SqList *InitSqList(SqList *h);
void ShowList(SqList *h);

int main()
{
    SqList *head = NULL;
    head = InitSqList(head);
    ShowList(head);
    system("pause");
    return 0;
}

/*链表的初始化*/
SqList *InitSqList(SqList *h)
{
    SqList *p1, *p2;
    while(1)
    {
        p2 = (SqList *)malloc(sizeof(SqList));
        scanf("%s", p2->name);
        if(strcmp(p2->name,"#") == 0)
        {
              break;
        }
        else
        {
            if(h == NULL) //<-----这里是==
            {
                 p2->next = NULL; //尾节点的next指针都等于NULL,这个你都漏了
                 h = p2;
                 p1 = p2;
            }
            else
            {
                 p1->next = p2;
                 p2->next = NULL;
                 p1 = p2;
            }
        }
    }
    return h;
}

/*链表的输出*/
void ShowList(SqList *h)
{
    SqList *temp = h;
    while (temp != NULL)  //<----- 按你的写法就会缺失最后一个节点
    {
        printf("%s\n", temp->name);
        temp = temp->next;
    }
}


 

我的地盘
2011-04-16 16:55
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
回复 3楼 hnuhsg1226
谢谢,已经改好了!

o(∩∩)Linux & Python 群:187367181
2011-04-16 17:22
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
程序代码:
/*
========================================================
我又改了下,改成这样子。编译的时候出现了两个警告
警告说temp->next = next和p1->next = p2;出现了
“不兼容的任务分配”
warning: assignment from incompatible pointer type
这是什么原因呢?
但是在typedef struct 后面加上SqList,警告就没了
=======================================================
*/
#include <stdio.h>
#include <stdlib.h>

#define LENGTH 20

typedef struct
{
    char name[LENGTH];
    struct SqList *next;
}SqList;

/*链表的初始化和输出*/
SqList *InitSqList(SqList *h);
void ShowList(SqList *h);

int main()
{
    SqList *head = NULL;
    head = InitSqList(head);
    ShowList(head);
    return 0;
}

/*链表的初始化*/
SqList *InitSqList(SqList *h)
{
    SqList *p1, *p2;
    p1 = p2 = (SqList *)malloc(sizeof(SqList));
    scanf("%s", p2->name);
    getchar();
    p2->next = NULL;
    while(p2->name[0] != '#')
    {
        if(h == NULL)
        {
            h = p2;
        }
        else
        {
            p1->next = p2;
        }
        p1 = p2;
        p2 = (SqList*)malloc(sizeof(SqList));
        if(p2 != NULL)
        {
            scanf("%s", p2->name);
            p2->next = NULL;
        }
    }
    p2->next = NULL;
    return h;
}

/*链表的输出*/
void ShowList(SqList *h)
{
    SqList *temp = h;
    while (temp != NULL)
    {
        printf("%s\n", temp->name);
        temp = temp->next;
    }
}


[ 本帖最后由 thlgood 于 2011-4-16 17:29 编辑 ]

o(∩∩)Linux & Python 群:187367181
2011-04-16 17:27
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
木有人回复啊,自己顶上去

o(∩∩)Linux & Python 群:187367181
2011-04-16 21:07
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
你链表类型没有定义

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

#define LENGTH 20

struct SqList  
 {
     char name[LENGTH];
     struct SqList *next;
 }SqList;

typedef struct SqList SqList ;

/*链表的初始化和输出*/
 SqList *InitSqList(SqList *h);
void ShowList(SqList *h);

int main()
 {
     SqList *head = NULL;
     head = InitSqList(head);
     ShowList(head);
     return 0;
 }

/*链表的初始化*/
 SqList *InitSqList(SqList *h)
 {
     SqList *p1, *p2;
     p1 = p2 = (SqList *)malloc(sizeof(SqList));
     scanf("%s", p2->name);
     getchar();
     p2->next = NULL;
     while(p2->name[0] != '#')
     {
         if(h == NULL)
         {
             h = p2;
         }
         else
         {
             p1->next = p2;
         }
         p1 = p2;
         p2 = (SqList*)malloc(sizeof(SqList));
         if(p2 != NULL)
         {
             scanf("%s", p2->name);
             p2->next = NULL;
         }
     }
     p2->next = NULL;
     return h;
 }

/*链表的输出*/
void ShowList(SqList *h)
 {
     SqList *temp = h;
     while (temp != NULL)
     {
         printf("%s\n", temp->name);
         temp = temp->next;
     }
 }

without further ado, let’s get started
2011-04-16 21:09
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
回复 7楼 iFreeBSD
还想请教下:
为什么
while(p2->name[0] != '#')


改成

while (! strcmp (p2->name, "#"))


不行?

o(∩∩)Linux & Python 群:187367181
2011-04-16 21:20
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
#include <stdio.h>
#include <string.h>
 #include <stdlib.h>

 #define LENGTH 20

 struct SqList  
 {
      char name[LENGTH];
      struct SqList *next;
 }SqList;

 typedef struct SqList SqList ;

 /*链表的初始化和输出*/
 SqList *InitSqList(SqList *h);
 void ShowList(SqList *h);

 int main()
 {
      SqList *head = NULL;
      head = InitSqList(head);
      ShowList(head);
      return 0;
 }

 /*链表的初始化*/
 SqList *InitSqList(SqList *h)
 {
      SqList *p1, *p2;
      p1 = p2 = (SqList *)malloc(sizeof(SqList));
      scanf("%s", p2->name);
      getchar();
      p2->next = NULL;
      while(strcmp(p2->name , "#"))
      {
          if(h == NULL)
          {
              h = p2;
          }
          else
          {
              p1->next = p2;
          }
          p1 = p2;
          p2 = (SqList*)malloc(sizeof(SqList));
          if(p2 != NULL)
          {
              scanf("%s", p2->name);
              p2->next = NULL;
          }
      }
      p2->next = NULL;
      return h;
 }

 /*链表的输出*/
 void ShowList(SqList *h)
 {
      SqList *temp = h;
      while (temp != NULL)
      {
          printf("%s\n", temp->name);
          temp = temp->next;
      }
 }

搞清楚strcmp的返回值

without further ado, let’s get started
2011-04-16 21:56
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
回复 9楼 iFreeBSD
饿~~我错了!!

o(∩∩)Linux & Python 群:187367181
2011-04-16 22:06
快速回复:我都不好意思说自己学过链表了,帮我改下好吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033086 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved