C 问题 - 不知道如何将指针分配给列表的开头

标签 c pointers

我有一个教授要我们做的简单作业。 基本上是从文本文件中提取一些数字并加载到链表中。 我不想深入细节,但我有一个基本问题。

他给我们提供了这样一个函数:

INTLIST* init_intlist( int n ) 
{
INTLIST *lst;
lst = (INTLIST *)malloc(sizeof(INTLIST));
lst->datum = n;
lst->next = NULL;
return lst;
}

此函数用于用第一个元素初始化链表。然后他要求我们用这个签名定义一个函数:

int insert_intlist( INTLIST *lst, int n )

所以我假设他只是想让我们添加到链接列表中,所以我尝试了这个:

int insert_intlist( INTLIST *lst, int n )
 {
 INTLIST* lstTemp;
 lstTemp = (INTLIST *)malloc(sizeof(INTLIST));
 lstTemp->datum = n;
 lstTemp->next = lst;
 lst = lstTemp;       
 free(lstTemp);          
 }

所以我的想法是创建一个临时节点,分配数据值(Datum)并将下一个指针分配给当前指针指向的位置。然后我将主指针重新分配给这个新创建的临时节点。

这样我们就有了例如 2 个节点:

[新临时节点] -> [上一个初始化节点]

当我单步执行代码时,它看起来很棒......

然后回到 main 我只有一个函数来打印列表:

                   while (lst!=NULL)
                      {
                       printf("The value is:%d", lst->datum);
                       lst=lst->next;
                      }

问题是这似乎只打印一个数字(即我从文件中读取的第一个数字,我认为它是列表中的最后一个或者至少我认为它是列表中的最后一个).

但它应该继续通过,因为我在文件中有 10 位数字。我知道代码很脏,我会清理它...如果有人需要更多信息,这是我的整个主要功能:

#include <stdio.h>
#include <stdlib.h>
#include "intlist.h"

int main(int argc, char *argv[])
{
  char c;    /* Character read from the file. */
  FILE* ptr;   /* Pointer to the file. FILE is a
       structure  defined in <stdio.h> */
  int index=0;
  //INTLIST* aList[10]; //will use later

    /* Open the file - no error checking done */
  ptr = fopen("1.txt","r");
    /* Read one character at a time, checking 
       for the End of File. EOF is defined 
      in <stdio.h>  as -1    */

  if(ptr==NULL) {
    printf("Error: can't open file.\n");
    /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    return 1;
  }

  //aList[index] = malloc(sizeof(INTLIST)); WE NEED THIS LATER ON....
  INTLIST *lst=NULL;

  while ((c = fgetc(ptr)) != EOF)
  {
        if (c != ' ') 
        {
         //make sure it isnt a space
         int i = c - '0'; //get the value from the text file
             if(c=='\n') 
                 {
                      // aList[index]=lst;
                      // index++;
                      // aList[index] = malloc(sizeof(INTLIST));

                           while (lst!=NULL)
                              {
                               printf("The value is:%d", lst->datum);
                               lst=lst->next;
                              }

                           free(lst);
                           free(aList[index]);
                           return 0;
                          //new line in the file 
                         //create another linked list
                 }

            if (lst==NULL)
             lst = init_intlist(i);
            else
             insert_intlist( lst, i); 
        }
  }

  fclose(ptr);
  system("PAUSE"); 
  return 0;
}

这里是 intlist.h 供任何可能需要它的人使用:

#ifndef __intlist_h__
#define __intlist_h__
/* each entry in the list contains an int */
typedef struct intlist {
int datum;
struct intlist *next;
} INTLIST;
INTLIST *init_intlist( int n ); /* initializes the intlist with initial datum n */
int insert_intlist( INTLIST *lst, int n ); /* Inserts an int (n) into an intlist from the beginning*/
void list_append(INTLIST *list, void *datum); /* Inserts entry to the end of the list */
INTLIST* list_front(INTLIST *list); /*return the element at the front of the list, and remove it 
from the list*/
void list_map( INTLIST *list, void (*f)(void *) ); /*Applies a function to each element of the list */
void list_delete( INTLIST *list ); /* Deletes (and frees) all entries in the list */
#endif

最佳答案

这里有几个问题。

我将从一个糟糕的错误开始:

int insert_intlist( INTLIST *lst, int n )
 {
 INTLIST* lstTemp;
 lstTemp = (INTLIST *)malloc(sizeof(INTLIST));
 lstTemp->datum = n;
 lstTemp->next = lst;
 lst = lstTemp;       
 free(lstTemp);             //   <<<<<  NO!
 }

您仍在使用该内存,因此无法释放它。


其次,提供给您用于插入的原型(prototype)无法返回列表的新前端,因此您无法更改列表的前端。这意味着您必须将新节点添加到后面,而不是像您所做的那样添加到前面。

此外,提供的 int 返回类型可能意味着他期望列表中的节点数,这没问题,因为您将不得不遍历列表以找到无论如何回来。

再试一次,你做的还不错。

关于C 问题 - 不知道如何将指针分配给列表的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2106691/

相关文章:

c - 了解分数背包、数组和指针

c - 从函数返回数组地址无效

c - 为什么程序在递归地从数组添加到队列时崩溃?

c - SYNC13C SPOJ 错误答案

c - 如何检查整数是否是数组中元素的线性组合?

C++ 我可以创建一个指向数组的指针吗?

c - C 中宏和增量运算符的行为

c - 为什么我的数组打印不正确?

从 const int* 到 int* 的 C++ 转换处理意外结果

c++ - 总不会打印为双