【问题标题】:What's wrong with my "Create new nodes" function & my "add node as last node" function?我的“创建新节点”功能和“添加节点作为最后一个节点”功能有什么问题?
【发布时间】:2014-11-14 07:33:19
【问题描述】:

我有 2 个函数,CreateNewNode,一个创建新节点的函数,以及 Add_as_Last_Node,一个将 CreateNewNode 中创建的节点添加为链表中最后一个节点的函数。在运行我使用这两个函数制作的程序时,输出结果不是我所期望的。

例如,如果我是用户,我想做一个 5 个节点的链表,我的输入是“apple”、“bag”、“car”、“dress”和“elephant”,当我遍历并打印输出的节点是:

大象 大象 大象 大象 大象

什么时候应该:

苹果 包 车 裙子 大象

在研究我的代码几个小时后,我发现我的程序的问题在于 CreateNewNode 或 Add_as_Last_Node。或者两者兼而有之。我试过追踪它,但没有找到它的问题所在。有人可以帮我找出问题所在吗?谢谢! :)

typedef char Str30[31];

struct nodeTag {
   char *data;
   struct nodeTag *pNext;
};

void Traverse(struct nodeTag *pCurrent)
{
   while(pCurrent){  // while there's a node
      printf("%s\n", pCurrent->data);
      pCurrent = pCurrent->pNext; // move to the next node
   }
}

struct nodeTag *CreateNewNode(Str30 data)
{
   struct nodeTag *pTemp;

   //create and initialize a new node
   pTemp = malloc(sizeof(struct nodeTag));
   pTemp->data = data;
   pTemp->pNext = NULL;

   return pTemp;
}

struct nodeTag *Add_as_Last_Node(struct nodeTag *pFirst, struct nodeTag *pTemp)
{
    struct nodeTag *pCurrent;

    if(pFirst == NULL){ // list is empty
       pFirst = pTemp;
    }
    else {
       pCurrent = pFirst;

       while(pCurrent->pNext != NULL)
          pCurrent = pCurrent->pNext;

       pCurrent->pNext = pTemp;
    }

    return pFirst;
}

int main()
{
   Str30 data;
   struct nodeTag *pFirst = NULL;
   struct nodeTag *pTemp;

   while(scanf("%s", data) != -1){
      pTemp = CreateNewNode(data);
      pFirst = Add_as_Last_Node(pFirst, pTemp);
   }

   Traverse(pFirst);

   return 0;
}

这是我正在使用的实际代码:

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

typedef char Str30[31];

struct nodeTag {
   char *data;
   struct nodeTag *pNext;
};

void Traverse(struct nodeTag *pCurrent)
{
   // traverse the linked list
   while(pCurrent){  // while there's a node
     printf("%s\n", pCurrent->data);
     pCurrent = pCurrent->pNext; // move to the next node
   }
}

struct nodeTag *CreateNewNode(Str30 data)
{
   struct nodeTag *pTemp;

   //create and initialize a new node
   pTemp = malloc(sizeof(struct nodeTag *));
   //strcpy(pTemp->data, data);
   pTemp->data = data;
   pTemp->pNext = NULL;

   return pTemp;
}

struct nodeTag *Add_as_Last_Node(struct nodeTag *pFirst,
                             struct nodeTag *pTemp)
{
  struct nodeTag *pCurrent;

  if(pFirst == NULL){ // list is empty
     pFirst = pTemp;
  }
  else {
     pCurrent = pFirst;

     while(pCurrent->pNext != NULL)
        pCurrent = pCurrent->pNext;

     pCurrent->pNext = pTemp;
  }

  return pFirst;
}

struct nodeTag *Insert_Sort(struct nodeTag *pFirst,
                        struct nodeTag *pTemp)
{
    struct nodeTag *pTrail;
    struct nodeTag *pCurrent;

    if(pFirst == NULL)
        pFirst = pTemp;
    else{
        pTrail = NULL;
        pCurrent = pFirst;

        while(pCurrent->pNext != NULL){
            pTrail = pCurrent;
            pCurrent = pCurrent->pNext;
        }

        pTemp->pNext = pCurrent;

        if(pTrail != NULL)
            pTrail->pNext = pTemp;
        else
            pFirst = pTemp;
    }

    return pFirst;
}

int main()
{
    int ctr = 0;
    Str30 data;
    // Str30 universe;
    struct nodeTag *pFirst = NULL;
    struct nodeTag *pTemp;

    while(ctr != 3 && scanf("%s", data) != -1){
        printf("\ndata = %s\n", data);
        pTemp = CreateNewNode(data);
        printf("\nAfter CreateNewNode: \n\n");
        Traverse(pFirst);
        pFirst = Add_as_Last_Node(pFirst, pTemp);
        printf("\nAfter Add_as_Last_Node: \n\n");
        Traverse(pFirst);
        ctr++;

    }

    Traverse(pFirst);

    printf("\n%d\n", ctr);

    return 0;
}

现在这就是我用来测试我的代码有什么问题的方法。

【问题讨论】:

  • 您的编译器应该抱怨pTemp-&gt;data = data;:您是否按照建议尝试过strcpy?它对我有用。
  • 等等!!! 请勿在没有编辑注释的情况下编辑代码。并且您的最新编辑 [char * data] 是错误的。可能的传入分段错误。
  • 编辑它,应该是'char *data',就像在我的实际程序中一样。
  • 那你也需要malloc
  • 哦抱歉,为什么char *data; 错了?它没有产生语法错误。

标签: c linked-list nodes singly-linked-list


【解决方案1】:
  1. 将您的 pTemp-&gt;data = data; 更改为 strcpy(pTemp-&gt;data, data)
  2. while(scanf("%s", data) != -1) ..看起来很可疑。查看scanf() 的手册页

欲了解更多信息,请发帖MCVE


编辑:

请检查以下代码。

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


typedef char Str30[31];

struct nodeTag {
        Str30 data;
        struct nodeTag *pNext;
};
#if 1
void Traverse(struct nodeTag *pCurrent)
{
   while(pCurrent){  // while there's a node
      printf("%s\n", pCurrent->data);
      pCurrent = pCurrent->pNext; // move to the next node
   }
}

struct nodeTag *CreateNewNode(Str30 data)
{
        struct nodeTag *pTemp;

        //create and initialize a new node
        pTemp = malloc(sizeof(struct nodeTag));
        strcpy(pTemp->data, data);
        pTemp->pNext = NULL;

        return pTemp;
}

struct nodeTag *Add_as_Last_Node(struct nodeTag *pFirst, struct nodeTag *pTemp)
{
        struct nodeTag *pCurrent;

        if(pFirst == NULL){ // list is empty
                pFirst = pTemp;
        }
        else {
                pCurrent = pFirst;

                while(pCurrent->pNext != NULL)
                        pCurrent = pCurrent->pNext;

                pCurrent->pNext = pTemp;

        }
                return pFirst;
}

int main()
{
        Str30 data;
        struct nodeTag *pFirst = NULL;
        struct nodeTag *pTemp = NULL;
        int count = 5;

        while(count--)
        {
                memset(data, 0, sizeof(data));
                scanf("%s", data);
                pTemp = CreateNewNode(data);
                if (!pTemp)
                {
                    printf("pTemp is NULL\n");
                }
                pFirst = Add_as_Last_Node(pFirst, pTemp);
        }
        Traverse(pFirst);

        return 0;
}

【讨论】:

  • 输出是一样的,没有任何改变。
  • @FaithClarisse 你可以编辑问题并添加Traverse()函数的定义吗?
  • 哦,好的,请稍候。
  • 编辑了,while(scanf("%s", data) != -1) 也是正确的。我的教授教过我。
  • 你可以通过输入Ctrl-D来中断while,然后scanf返回-1
【解决方案2】:

您使用的 typedef 导致数组类型。 尝试更改它 (typedef char Str30[31];) 以使用结构

typedef struct Str30Type { char value[30]; } Str30Type;

当用作函数参数时,ArrayTypes 将通过引用而不是值传递。 因此,我认为您将同一对象的引用复制了 5 次,并使用了最后更新的值。

typedef char Str30[31];

struct nodeTag {
   Str30 data;
   struct nodeTag *pNext;
};

void Traverse(struct nodeTag *pCurrent)
{
   while(pCurrent){  // while there's a node
      printf("%s\n", pCurrent->data);
      pCurrent = pCurrent->pNext; // move to the next node
   }
}

struct nodeTag *CreateNewNode(Str30 data)
{
   struct nodeTag *pTemp;

   //create and initialize a new node
   pTemp = malloc(sizeof(struct nodeTag));
   memcpy(pTemp->data,data,strlen(data));
   pTemp->pNext = NULL;

   return pTemp;
}

struct nodeTag *Add_as_Last_Node(struct nodeTag *pFirst, struct nodeTag *pTemp)
{
    struct nodeTag *pCurrent;

    if(pFirst == NULL){ // list is empty
       pFirst = pTemp;
    }
    else {
       pCurrent = pFirst;

    while(pCurrent->pNext != NULL)
       pCurrent = pCurrent->pNext;

    pCurrent->pNext = pTemp;

    return pFirst;
    }
}

int main()
{
   Str30 data;
   struct nodeTag *pFirst = NULL;
   struct nodeTag *pTemp;

   while(scanf("%s", data) != -1){
      pTemp = CreateNewNode(data, strlen(data));
      pFirst = Add_as_Last_Node(pFirst, pTemp);
   }

   Traverse(pFirst);

   return 0;
}

【讨论】:

  • typedef 应该是这样的。
  • 那么您将不得不更改复制数据的方式,而不仅仅是复制引用。 pTemp->数据=数据;
  • 我已经通过修改 Struct 来编辑答案。你可以这样做吗?
【解决方案3】:

这里有多处错误:

pTemp->data = data;

这是错误的,你需要使用 strcpy()。

检查下面的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
   typedef   char Str30[31];

 struct nodeTag {
      Str30 data;
      struct nodeTag *pNext;
   }; 
    void Traverse(struct nodeTag *p)
    {
       struct nodeTag *t = p;
       while(t != NULL)
       {   
          printf("%s\n",t->data);
          t = t->pNext;
       }   
       return;
    }
    struct nodeTag *CreateNewNode(Str30 data)
    {
       struct nodeTag *pTemp;


       pTemp = malloc(sizeof(struct nodeTag));
       strcpy(pTemp->data, data);
       pTemp->pNext = NULL;

       return pTemp;
    }

    void Add_as_Last_Node(struct nodeTag *pFirst, struct nodeTag *pTemp)
    {
       struct nodeTag *pCurrent;

          pCurrent = pFirst;

          while(pCurrent->pNext != NULL)
             pCurrent = pCurrent->pNext;

          pCurrent->pNext = pTemp;
    }
       int main()
       {   
          Str30 data;
          int i=0;
          struct nodeTag *pFirst = NULL;
          struct nodeTag *pTemp;
          for(i=0;i<3;i++)
          {   
             scanf("%s",data);
             pTemp = CreateNewNode(data);
             if(pFirst == NULL)
             {   
                pFirst = pTemp;
             }   
             else
             {   
                Add_as_Last_Node(pFirst, pTemp);
             }   
          }   
          Traverse(pFirst);

          return 0;
       }  

【讨论】:

  • 抱歉,您的代码不起作用。当我使用 strcpy 时,我的程序也不起作用。
  • @FaithClarisse 什么不起作用?这对我来说可以。我这里只取 3 个字符,你可以改变它
  • @FaithClarisse 尝试输入几个字符串。它将等待 scanf()
【解决方案4】:

更新:

我的代码终于可以工作了。你们对strcpy(temp-&gt;data, data) 的看法是对的。它不起作用的真正原因是代码中的其他部分。

无论如何,感谢您的帮助!我会检查其他人的感谢。丹克! :D

【讨论】:

  • 很高兴您能够解决您的问题。每次从函数Add_as_Last_Node返回pFirst有什么用,为什么返回类型不能为void?
猜你喜欢
  • 2019-12-15
  • 1970-01-01
  • 2011-04-26
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 2020-10-25
相关资源
最近更新 更多