我的“创建新节点”功能有什么问题&我的“添加节点作为最后一个节点”功能?

时间:2014-11-14 07:33:20

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

我有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;
}

现在,我正在使用它来测试我的代码有什么问题。

4 个答案:

答案 0 :(得分:1)

  1. 将您的pTemp->data = data;更改为strcpy(pTemp->data, data)
  2. while(scanf("%s", data) != -1) ..看起来很可疑。查看scanf()
  3. 的手册页

    有关详细信息,请发布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;
    }
    

答案 1 :(得分:0)

您使用的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;
}

答案 2 :(得分: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("%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;
       }  

答案 3 :(得分:0)

更新:

我的代码终于有效了。你们对strcpy(temp->data, data)是正确的。它不起作用的真正原因是因为代码中的其他部分。

无论如何,谢谢你的帮助!我会在感谢中检查其他人。丹科! :d