Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 浏览链接列表_C_Pointers_Linked List - Fatal编程技术网

C 浏览链接列表

C 浏览链接列表,c,pointers,linked-list,C,Pointers,Linked List,我试图创建一个稀疏(链接)结构,每个节点指向它的所有子节点(总共5个)。 到目前为止,我只创建了第一个节点(称为“根”)。我试图遍历链接结构,希望程序返回“root”。 这给了我一个分段错误 主类 Item n; newDirectory(); printf("Folder root has been created."); printf("Enter the name of the directory you want to traverse: "); scanf("%s", n.name

我试图创建一个稀疏(链接)结构,每个节点指向它的所有子节点(总共5个)。 到目前为止,我只创建了第一个节点(称为“根”)。我试图遍历链接结构,希望程序返回“root”。 这给了我一个分段错误

主类

Item n;

newDirectory();
printf("Folder root has been created.");

printf("Enter the name of the directory you want to traverse: ");
scanf("%s", n.name);
browseItem(n);
我创建的结构

typedef struct Directory{
   //name of the file
   char name[16];
   //file content
   char value[80];
   _Bool isLeaf;

   //if folder status = 1, if txt status = 2, if empty status = 0
   int status;
   struct Directory *firstchild;
   struct Directory *secondchild;
   struct Directory *thirdchild;
   struct Directory *fourthchild;
   struct Directory *fifthchild;
}Item;
我还包括了结构所在的类函数

//points to the first node: node "root" 
Item *head;

//creates the first (head) node: "root"
void newDirectory(){
  head = (Item *)malloc(sizeof(Item));

  if(head == NULL){
      printf("Unable to allocate memory.");
  }else{
      strcpy(head->name,"root");
      head->status = 1;
      head->firstchild = NULL;
      head->secondchild = NULL;
      head->thirdchild = NULL;
      head->fourthchild= NULL;
      head->fifthchild = NULL;
  }
}


void browseItem(Item n) {
   //how do I find the location of n
   Item *tmp;
   tmp = (Item *)malloc(sizeof(Item));
   if(head == NULL){
       printf("List is empty!");
   }else{
      tmp = **location of n**;
      while(tmp!=NULL){
          printf("%s", tmp->name);
          tmp = tmp->firstchild;
          tmp = tmp->secondchild;
          tmp = tmp->thirdchild;
          tmp = tmp->fourthchild;
          tmp = tmp->fifthchild;
      }
   }
}
我的问题是如何首先搜索n的位置,以便 程序从该节点开始遍历。如果我有 来自根的节点越多,子节点也会被遍历吗


非常感谢

SEGFULT通常是由于数据传输错误造成的。您的代码和逻辑有几个问题。让我尽量回答你的所有问题:

首先,你的链表,不是一个真正的链表,它是一棵树。链接列表将指向下一个节点。这是一个单链接列表,双链接列表将指向下一个节点和上一个节点。它们都是一样的,只是树有更多的子元素,而链表只有1个子元素,双链表也有指向父元素(或上一个元素)的指针

你的第二个问题似乎是,我怎样才能找到根

“我正在尝试遍历链接结构,希望程序返回‘root’。”

当前,您的代码是这样的:
root-->子1、子2、子3
(这是一个树)。如果要将代码改为链表,则必须是
根-->child1-->child2-->child3
。。。等等但是,您的链接列表是单链接列表,这意味着您只能向前,不能向后。如果您想返回到root,它将是
null
,依此类推(您必须有一个指针,指向上一个节点,就像一个双链接列表一样)

那么你的问题是我如何找到n

//如何找到n的位置

只有从根开始,并以这种方式遍历链表,才能执行此操作。使用链表进行此操作的最简单方法是:

Item tmp = head->child1;
while (tmp != null)
{
    if (tmp -> name == n)
    {
         print "Found n!" + tmp->name
         break;
    }
    tmp = tmp -> nextChild;

}
此代码只是一个伪代码,以使其看起来更简单


如果它是一棵树,那么我们必须使用广度优先算法或深度优先算法来查找n。如果您的代码试图模拟文件结构,则应使用树,而不是链表。

SEGFULT通常是由数据传输错误引起的。您的代码和逻辑有几个问题。让我尽量回答你的所有问题:

首先,你的链表,不是一个真正的链表,它是一棵树。链接列表将指向下一个节点。这是一个单链接列表,双链接列表将指向下一个节点和上一个节点。它们都是一样的,只是树有更多的子元素,而链表只有1个子元素,双链表也有指向父元素(或上一个元素)的指针

你的第二个问题似乎是,我怎样才能找到根

“我正在尝试遍历链接结构,希望程序返回‘root’。”

当前,您的代码是这样的:
root-->子1、子2、子3
(这是一个树)。如果要将代码改为链表,则必须是
根-->child1-->child2-->child3
。。。等等但是,您的链接列表是单链接列表,这意味着您只能向前,不能向后。如果您想返回到root,它将是
null
,依此类推(您必须有一个指针,指向上一个节点,就像一个双链接列表一样)

那么你的问题是我如何找到n

//如何找到n的位置

只有从根开始,并以这种方式遍历链表,才能执行此操作。使用链表进行此操作的最简单方法是:

Item tmp = head->child1;
while (tmp != null)
{
    if (tmp -> name == n)
    {
         print "Found n!" + tmp->name
         break;
    }
    tmp = tmp -> nextChild;

}
此代码只是一个伪代码,以使其看起来更简单


如果它是一棵树,那么我们必须使用广度优先算法或深度优先算法来查找n。如果您的代码试图模拟文件结构,则应使用树,而不是链表。

browseItem(…)的预期行为是什么?我看不到返回值。此外,这不是一个链表,而是一棵树。此外,在这种连接结构中,通过递归进行搜索更容易。browseItem应该有两个参数。1要搜索的元素,2。要开始搜索的(临时)根节点。所以第一次调用browseItem(n,head);其他时间浏览项目(n,头部->儿童);browseItem(…)的期望行为是什么?我看不到返回值。此外,这不是一个链表,而是一棵树。此外,在这种连接结构中,通过递归进行搜索更容易。browseItem应该有两个参数。1要搜索的元素,2。要开始搜索的(临时)根节点。所以第一次调用browseItem(n,head);其他时间浏览项目(n,头部->儿童);