如何跟踪C中的初始头指针?

时间:2018-04-05 06:24:48

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

我试图在列表末尾附加并在此函数结束时返回 head

这是一个单链表。所以,经过一段时间后,我已经失去了理智。

file.c

int main(void)
{
   struct equipment *e_list = NULL;
   e_list = append_to_list(e_list);

}

在这个int main函数中, e_list 应在调用 append_to_list(e_list)后指向链接列表的头部。

{{1}}

如何创建引用并遍历该虚拟头?这样我就不会失去原来的头脑。

3 个答案:

答案 0 :(得分:0)

您需要else添加

中的临时变量
struct equipment *tmp;
tmp = list;

并替换list中出现的所有else。最后返回list变量。

答案 1 :(得分:0)

由于您已将新创建的structure temp放在结束。您的不会在main中的不同来电之间发生变化,因为位于结束的对面。

您需要一个静态变量来跟踪头部。这是经过测试的代码:

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

#define NAME_LEN 30

struct equipment
{
  char type[NAME_LEN + 1];
  char description[NAME_LEN + 1];
  int quantity;
  struct equipment *next;
};

struct equipment *
append_to_list (struct equipment *list)
{
  char type[NAME_LEN + 1], description[NAME_LEN + 1];
  int quantity;

  printf ("Enter equipment type: ");
  fgets (type, NAME_LEN, stdin);

  printf ("Enter description of the equipment: ");
  fgets (description, NAME_LEN, stdin);

  printf ("Enter quantity: ");
  scanf ("%d", &quantity);

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

  strcpy (temp->type, type);
  strcpy (temp->description, description);
  temp->quantity = quantity;
  temp->next = NULL;

  bool doesExist = false;
  static struct equipment * head = NULL;  //New variable

  if (list == NULL)
    {
      list = temp;
      head = list;
    }
  else
    {
      while (list->next != NULL)
    {
      if (list == temp)
        {
          printf ("This equipment is already in the list\n");
        }
      list = list->next;
    }
      list->next = temp;
    }
    return head;        //return head here
}

int main()
{
    struct equipment *e_list = NULL;
    e_list = append_to_list(e_list);
    e_list = append_to_list(e_list);
    e_list = append_to_list(e_list);
    return 0;
}

答案 2 :(得分:0)

你真的可以在这做一些事情。

首先,您的代码相当不安全。您的缓冲区太小,不使用scanf转换整数(它不会检查失败),也不使用strcpy(特别是当缓冲区很小时)。另外,如果您只是使用文字字符串,则调用printf有点无意义 - 应该考虑puts() - 并且不要从malloc()转换返回。这可能有点安全。

#include <bsd/bsd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define NAME_LEN 32
#define nputs(STR) fputs((STR), stdout)

struct equipment {
    char type[NAME_LEN];
    char description[NAME_LEN];
    int quantity;
    struct equipment *next;
};

struct equipment *
append_to_list(struct equipment *list)
{
    char type[BUFSIZ], description[BUFSIZ], quantity_string[BUFSIZ];
    int quantity;

    nputs("Enter equipment type: ");
    fgets(type, BUFSIZ, stdin);

    nputs("Enter description of the equipment: ");
    fgets(description, BUFSIZ, stdin);

    nputs("Enter quantity: ");
    fgets(quantity_string, BUFSIZ, stdin);

    char *endptr;
    quantity = strtol(quantity_string, &endptr, 10);
    if (quantity_string == endptr) {
        fprintf(stderr, "Error: invalid integer input '%s'\n", quantity_string);
        exit(1);
    }

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

    strlcpy(temp->type, type, NAME_LEN);
    strlcpy(temp->description, description, NAME_LEN);
    temp->quantity = quantity;
    temp->next     = NULL;

    bool doesExist = false;

    if (list == NULL) {
        list = temp;
    } else {
        while (list->next != NULL) {
            if (list == temp)
                puts("This equipment is already in the list");
            list = list->next;
        }
        list->next = temp;
    }
    // return head of this list here;
}

至于跟踪头部,你可以使用不同的函数来初始化树而不是附加到它,从而为你提供一个稍微独特的根对象。您可以向存储根位置的每个结构添加一个字段,或者您可以完全创建一个单独的结构来保存根,也可以包含一些关于它的元数据。有很多选择。我同意上面的一些评论,并不完全清楚你在这里要求的是什么。