为什么链接列表向后打印?

时间:2019-07-26 13:21:39

标签: c data-structures printing linked-list insert

我要打印链接列表的内容,这些内容来自文件,如下所示:

float

因此,到目前为止,在我的程序中,我的输入已被正确读取,并且我已经调试了该部分,所以我知道这不是问题。我在打印链接列表时遇到问题。

我尝试编写一种1,Postit Notes,Sticky notes,3 2,Black pens,Gel pens with black ink,5 3,Blue pens, Gel pens with blue ink, 4 4, Red pens, Gel pens with red ink for grading, 3 5, Notecards,Ruled 3" by 5" notecards,2 7,Whiteout,For mistakes made when writting with ink,3 方法(如下所示),在该方法中,我为新节点分配内存,分配所需的数据,然后尝试添加它以添加链表的末尾。我也有一种方法来打印链接列表的内容,如下所示:

addRecord
static void *addRecord(List *list, int newID, char *newName, char *newSummary, int newCount) 
{
  //Allocate memory for the node
  Node *new = (Node *)malloc(sizeof(Node)); 

  //Add in data
  new->id = newID;
  strcpy(new->name, newName);
  strcpy(new->summary, newSummary);
  new->count = newCount;

  //Node gets data you added in
  new->next = list->head;
  list->head = new;
  return EXIT_SUCCESS;
}

这是我的链接列表的设置方式:

void print(List *list)
{
  printf("LIST IN FORWARD ORDER:\n");

  //Create a temporary node to traverse the list
  Node *temp = list->head;

  //Traverse the entire list
  while (temp != NULL) {
    printf("Item ID: %d\n", temp->id);
    printf("Name: %s\n", temp->name);
    printf("Summary: %s\n", temp->summary);
    printf("Count: %d\n", temp->count);
    printf("-----\n");
    temp = temp->next;
  }
}

我的预期输出应如下所示:

//struct for each office item
struct NodeStruct {
    int id;
    char name[MAX_NAME];
    char summary[MAX_SUM];
    int count;
    struct NodeStruct *next;
};


/** Structure for the whole list, including head and tail pointers. */
typedef struct {
  /** Pointer to the first node on the list (or NULL ). */
  Node *head;
} List;

但是,我的实际输出是:

LIST IN FORWARD ORDER:
Item ID: 1
Name: Postit Notes
Summary: Sticky notes
Count: 3
-----
Item ID: 2
Name: Black pens
Summary: Gel pens with black ink
Count: 5
-----
Item ID: 3
Name: Blue pens
Summary: Gel pens with blue ink
Count: 4
-----
Item ID: 4
Name: Red pens
Summary: Gel pens with red ink for grading
Count: 3
-----
Item ID: 5
Name: Notecards
Summary: Ruled 3" by 5" notecards
Count: 2
-----
Item ID: 7
Name: Whiteout
Summary: For mistakes made when writting with ink
Count: 3
-----

在我的打印方法中,当我尝试打印头节点的内容时,我从ID为7的项目中获取了数据/信息,而我应该获取ID为1的项目的信息。我的清单为什么要向后打印?我试图追踪它,但我有点困惑。我尝试了其他添加记录的方法,但是当我尝试添加记录时,却不断得到LIST IN FORWARD ORDER: Item ID: 7 Name: Whiteout Summary: For mistakes made when writting with ink Count: 3 ----- Item ID: 5 Name: Notecards Summary: Ruled 3" by 5" notecards Count: 2 ----- Item ID: 4 Name: Red pens Summary: Gel pens with red ink for grading Count: 3 ----- Item ID: 3 Name: Blue pens Summary: Gel pens with blue ink Count: 4 ----- Item ID: 2 Name: Black pens Summary: Gel pens with black ink Count: 5 ----- Item ID: 1 Name: Postit Notes Summary: Sticky notes Count: 3 -----

提前谢谢!

编辑:

在下面Thomas Jager的评论之后,我对addRecord方法进行了以下修改:

Segmentation Fault: 11

但是,现在,出现以下错误:

static void *addRecord(List *list, int newID, char *newName, char *newSummary, int newCount) 
{
  //Allocate memory for the node
  Node *new = (Node *)malloc(sizeof(Node)); 

  //Add in data
  new->id = newID;
  strcpy(new->name, newName);
  strcpy(new->summary, newSummary);
  new->count = newCount;

  //Special case: If the first node is null, add the data here
  if (list->head->next == NULL) {
    list->head->next = new;
  } else {
    Node *temp = new; 
    while (temp != NULL) {
      new = new->next; 
    }
  }

  return EXIT_SUCCESS;
}

我无法理解所发生的细分错误,有人可以向我解释一下吗?

1 个答案:

答案 0 :(得分:0)

问题在于您添加到列表中的方式。您总是在列表的开头而不是结尾处添加

解决此问题的一种方法是更改​​您的addRecord函数:

static void addRecord(List *list, int newID, char *newName, char *newSummary, int newCount) 
{
  //Allocate memory for the node
  Node *new = malloc(sizeof(Node));

  //Add in data
  new->id = newID;
  strcpy(new->name, newName);
  strcpy(new->summary, newSummary);
  new->count = newCount;

  //New node has no next, yet
  new->next = NULL;

  //Add new node to the end of the list
  Node **next_p = &list->head;
  while (*next_p)
    next_p = &(*next_p)->next;
  *next_p = new;
}

有很多方法可以添加到列表的末尾。我在这里选择的方式使用指向Node *的指针。这样可以以相同的方式表示head中的Listnext中的Node

另一种实现方式是稍微不太优雅,但更具可读性。可以像在已编辑问题中一样,避免使用双指针,但可以修改以遍历列表的next

  //Add new node to the end of the list
  if (!list->head->next) {
    list->head->next = new;
  } else {
    Node *temp = list->head->next; 
    while (temp->next) {
      temp = temp->next; 
    }
    temp->next = new;
  }

这两种情况都要求初始list->headNULL,但考虑到您的打印情况,情况似乎已经如此。

我还做了几件事。您不应该在C中转换malloc的返回值。而且,您的addRecord的返回类型为void *,并且您返回EXIT_SUCCESS。如果您尝试返回成功值,那么void *可能不是正确的方法。