【问题标题】:Printing a Linked List, doesn't print anything打印链接列表,不打印任何内容
【发布时间】:2018-06-12 10:42:59
【问题描述】:

我正在尝试创建学生及其 ID 的链接列表。 我认为一切都很好,除了函数中的 List* 头。 我不擅长函数所以我不知道它是否正确。

当我尝试打印所有学生时,它没有给出任何输出,它只是返回到指令(再次通过主循环而不打印名称)。 你能帮帮我吗?

在我看来,头部在每个功能和主要功能中都没有保持相同,因此不会发生打印,但我不知道如何解决这个问题。

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



   typedef struct node
    {
      char name[50];
      int ID;
      node *next;
    }
      List;

   void Linked_insert(char givenname[50], int givenID, int start, List* head)
    {
       //if its the users first time inserting start becomes 0 and if it isnt start is 1 so for both cases i made that condition 
      if(start==0){    
       strcpy(head->name, givenname);
       head->ID = givenID;
       head->next = NULL;
       return head;
        }
      if(start==1){
       //end of list

        List* current = head;
           while(current->next != NULL){
           current = current->next;
           }
        current->next = malloc(sizeof(List));
        strcpy(current->next->name, givenname);
        current->next->ID = givenID;
        current->next->next = NULL;
        return current->next;
        }

     }



    void Linked_destroy()
     {

     }

    void Print_student(List* head)
     {
     }

    void Print_all(List* head)
       {    
         List* current = head;
           while(current->next != NULL){
           printf("Student ID [%d] has name [%s]\n", current->ID, current->name);
           current = current->next;    
            }
        }

     int main()
     {
        int loop=0, start=0;
          while(loop != 1)
            {
              printf("\n\n\n\n");
              printf("Data Structures - Linked List and Binary Tree\n");
              printf("Choose one Option:\n\n");
              printf("1.Insert Student\n");
              printf("2.Remove Student\n");
              printf("3.Print 1 student\n");
              printf("4.Print all student\n");
              printf("5.Exit\n\n");
              int option=0, inputID;
              char inputname[50];
               List* head = malloc(sizeof(List));
               scanf("%d", &option);
                 switch(option)
                   {
                     case 1:
                      printf("Enter Student name:   ");
                      scanf("%s", inputname);
                      printf("Enter Student ID:     ");
                      scanf("%d", &inputID);
                      Linked_insert(inputname, inputID, start, head);
                      start = 1;
                       break;

                      case 2:

                        break;

                      case 3:

                        break;

                      case 4:
                        Print_all(head);
                        break;

                      case 5:
                        loop =1;
                        break;

                      default:
                        loop =1;
                        break;

                  }//end of switch

           }//end of infinte loop
    }//end of main

【问题讨论】:

  • 也不编译
  • 我不知道该放什么来代替 void。我也不确定如何更新头部,这就是我在这里问的原因
  • 很多很多问题都是从地面开始的。来自错误的类型名称(不是 List,建议使用 ListItem)。破碎列表实现(head 角色难以理解)

标签: c linked-list


【解决方案1】:

出现问题是因为您在while 循环的每次迭代中都将内存分配给列表的head

while(loop != 1)
.....
.....
    List* head = malloc(sizeof(List));

在 while 循环的每次迭代中,都会创建新的列表 head,而旧的 head 引用会丢失(内存泄漏)。 在进入 while 循环之前移动 head 节点内存分配。

另一个问题是函数Print_all() 不会打印列表的最后一个节点:

while(current->next != NULL){

这应该是

while(current != NULL){

您的代码中还有一些问题,例如 Linked_insert() 返回类型是 void 但它返回了一些值。编译器必须在那些 return 语句上给出错误。

此外,您不会释放动态分配给列表节点的内存。遵循良好的编程习惯,一旦你完成了列表,你应该释放列表的所有节点。

【讨论】:

  • @Gerhardh 哪个函数?
  • @Gerhardh 内存到headwhile 循环内的main() 中分配,然后传递给Linked_insert() --> Linked_insert(inputname, inputID, start, head);。我不同意你的这种说法--> 你分配的东西在函数离开后都会丢失。
  • 对不起。我将main 中的循环与Linked_insert 中的循环混淆了。你是对的。
  • @Gerhardh 您可能还想更正您对 OP 问题的评论 - 插入也不起作用....
  • @EEECS 您的 Linked_insert() 函数有语句 - return head;return current-&gt;next; 这意味着您正试图从 Linked_insert() 函数返回 List * 类型。由于您将head 的引用传递给Linked_insert() 函数并在列表末尾添加新元素,因此您不需要从函数中返回List *,因为列表的head 永远不会改变。考虑要按排序顺序插入链接列表的情况。 ...继续下一条评论
猜你喜欢
  • 1970-01-01
  • 2017-08-16
  • 2019-05-26
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多