c - 对于将字符串插入链表非常困惑

标签 c string linked-list

我已经在这个项目上工作了一天多了,我真的很困惑。我祈祷有人能帮助解决这个问题。我在使用 insertItemList 函数时遇到问题。我不知道如何将测试文件中的String放入链表中。这是一份家庭作业。该项目比这个大得多,但我将其简化为我需要帮助的内容。有人知道我做错了什么吗?

列表.h

       #ifndef _list_h
       #define _list_h

      /*
      * Defines a single list item.
      */

      typedef char *ListItemT;

      /*
      * Defines ListP to be a POINTER to a list struct.
      * (Define the list struct in the (.c) file.
      */

      typedef struct ListT *ListP;

      /*
      * Returns a pointer to a new empty list.
      * You may implement this list as a singly or doubly
      * linked list.
      */

      ListP newList();

      /*
      * Creates a new node, inserts the item into the
      * new node and inserts the new node into the list
      * after the current node.
      * Current in then moved to the newly inserted node.
      * NOTE: Impossible to insert at head of list.
      */

      void insertItemList(ListP, ListItemT);

      #endif

列表.c

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


      struct ListT
      { 
         char INFO[20]; 
         struct ListT *Next; 
      }*Head;



      /*
      * Returns a pointer to a new empty list.
      * You may implement this list as a singly or doubly
      * linked list.
      */

      ListP newList()

      {
      // allocate memory for new list 

      struct ListT *newnode = malloc(sizeof(struct ListT));

      return newnode; 
      }

      /*
      * Creates a new node, inserts the item into the
      * new node and inserts the new node into the list
      * after the current node.
      * Current in then moved to the newly inserted node.
      * NOTE: Impossible to insert at head of list.
      */

      //where I'm having trouble

     void insertItemList(ListP LOC, ListItemT DATA)

     {
     struct ListT *temp;

     temp=(struct ListT *)malloc(sizeof(struct ListT)); 

     strcpy(temp->INFO, DATA);
     }

ListTest.c

     #include <stdio.h>
     #include <string.h>
     #include "List.h"

     int main()
     {
     // Create two lists
     ListP list1 = newList();
     ListP list2 = newList();
     printf("\nList Creation Successful!\n");

     // Insert one name into the first list
     insertItemList( list1, "Alice" ); //Ive never seen a parameter like this before.
     }

最佳答案

您的代码中似乎没有任何内容可以设置列表节点的 Next 字段。

关于c - 对于将字符串插入链表非常困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11486776/

相关文章:

java - 将分割字符串分配给已解析的 int

c++ - 有限域上超定系统解的存在性

c - 为什么我的键盘记录器不起作用?

C 代码从文本文件中查找最大和最小数字

c - 如何编辑和使用包含双引号和转义字符的字符串?

c++ - 将 std::string 正确写入二进制文件

c++ - 单链表 C++

java - 如何从链表中删除节点?

c - 程序中使用unsigned long long int和long long int计算数字幂的问题

c++ - 寻找一种快速填充 std::list 的方法