c语言 - 我的"Create new nodes"函数和"add node as last node"函数有什么问题?


我有 2 个函数,CreateNewNode,一个创建新节点的函数

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

例如,如果我是用户,我想制作一个包含 5 个节点的链接列表,并且我的输入是"苹果"、"包"、"汽车"、"衣服"和"大象",当我遍历和打印节点时,输出是:

象 象 象 象 象

什么时候应该:

苹果 袋 汽车 连衣裙 象

在研究了几个小时的代码后,我已经破译了我的程序的问题在于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("%sn", 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("%sn", 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 = %sn", data);
        pTemp = CreateNewNode(data);
        printf("nAfter CreateNewNode: nn");
        Traverse(pFirst);
        pFirst = Add_as_Last_Node(pFirst, pTemp);
        printf("nAfter Add_as_Last_Node: nn");
        Traverse(pFirst);
        ctr++;
    }
    Traverse(pFirst);
    printf("n%dn", ctr);
    return 0;
}

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

  1. 将您的pTemp->data = data;更改为 strcpy(pTemp->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("%sn", 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 NULLn");
                }
                pFirst = Add_as_Last_Node(pFirst, pTemp);
        }
        Traverse(pFirst);
        return 0;
}

您使用的 typedef 会生成数组类型。尝试将其更改为 ( typedef char Str30[31]; ) 以使用结构

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

当用作函数参数时,数组类型将通过引用而不是按值传递。所以我认为您正在复制同一对象的引用 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("%sn", 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;
}

这里有很多错误:

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("%sn",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(temp->data, data)的看法是对的.它不起作用的真正原因是代码中的其他部分。

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

相关内容

  • 没有找到相关文章

最新更新