【问题标题】:Implementing linked list inside linked list in c在c中的链表内实现链表
【发布时间】:2014-08-05 07:01:42
【问题描述】:

我正在开发一个更大的程序,并希望在一个链表中实现一个链表。这样我就有了一个主链表,其中的节点相互连接,但每个节点内部都有另一个链表。我写了一些我认为可以实现的代码,但是我无法将第二个链表链接到主链表的节点。以下是我的尝试,但我再次无法将它们全部连接在一起

struct node {
    char* course;
    struct node *next;
    struct linked *head;
};

struct linked{
    char* data;
    struct linked *next;

};

struct node* addnode(struct node *head,char* s);
struct node* addlinkednode(struct node *head,char* prereq);

int main()
{
    struct node *head = NULL;
    struct node *head2 = NULL;
    char* text[] = {"one", "two", "three",
                    "four", "five", "six"};

    char* text2[] = {"john","bravo","gabe"};
    int i, size = sizeof(text)/sizeof(text[0]);
    size2 = sizeof(text2)/sizeof(text2[0]);

    for(i = 0; i < size; i++)
        head = addNode(head, text[i]);
        head2 = head
        for(i = 0; i < size2; text2[i])
            head2 = addlinkednode(head2,text2[i]);


}

struct node* addNode(struct node* head, char* s)
{
    struct node* temp = malloc( sizeof(struct node) );
    strcpy(temp->course, s);
    temp->next = head;
    return temp;
}

struct node* addlinkednode(struct node *head,char* prereq)
{
    struct linked *temp = malloc( sizeof(struct node) );
    strcpy(temp->data, prereq);
    temp->next = head;
    return temp;
}

我希望这个示例代码输出一个链表,其中包含一、二、三.... 有自己的节点,但在“一”节点内,我们有一个“约翰、布拉沃、加贝”的链表, “两个”节点等也是如此......连接这些链表的任何帮助都会很棒。

【问题讨论】:

  • 基本上是将节点添加到列表中的链接列表中,您将执行head2-&gt;head = addlinkednode(head2-&gt;head, text2[i]);。但是,您的 addNote 函数中的 strcpy 可能无法执行您想要的操作(不要使用 strcpy),并且您在列表的开头而不是末尾添加节点。您还应该检查 malloc() 返回值,最后您需要在节点内分配字符串。
  • 确认@Optokopper 所说的,当addNode 中的temp-&gt;course 是一个不确定的目标时,strcpy(temp-&gt;course, s); 看起来并不太热。您从未为副本分配内存(您为 node 分配了空间,但指针 within 仍然不确定),因此该行调用未定义的行为。 strcpy(temp-&gt;data, prereq) 中的 addlinkednode 也是如此。
  • for(i = 0; i &lt; size2; text2[i]) 这里我认为你应该使用i++ 而不是text2[i]
  • 好的,我知道我需要如何为字符串分配内存。 Optokopper 将列表链接到每个节点的方式是否正确?谢谢
  • 我怀疑你想要something like this,但是对于发布的代码来说这有点难说。不管怎样,看看,我把列表和子列表清理(free()所有这些分配)留给你。祝你好运。

标签: c linked-list


【解决方案1】:

您的代码中有很多修复。查看以下更改并正确修复它们。我已经解决了您的主链表的创建问题。我不清楚您在链接列表中的链接列表。所以我创建了两个列表,你把它链接到你想要的地方!

我已经修改了您的逻辑,将节点添加到链表的末尾。希望对您有所帮助!

尝试以下更改-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct linked{
    char* data;
    struct linked *next;

};

struct node {
    char* course;
    struct node *next;
    struct linked *head;
};

void addNode(struct node **head,char* s);
void addlinkednode(struct linked **head,char* prereq);

int main()
{
    struct node *head = NULL,*temp;
    struct linked *head2 = NULL,*temp1;
    char* text[] = {"one", "two", "three",
            "four", "five", "six"};

    char* text2[] = {"john","bravo","gabe"};
    int i, size = sizeof(text)/sizeof(text[0]);
    int size2 = sizeof(text2)/sizeof(text2[0]);


    for(i = 0; i < size; i++)
            addNode(&head, text[i]);

    for(i = 0; i < size2; i++)
            addlinkednode(&head2,text2[i]);
    // For printing...
    temp=head;
    while(temp){
            printf("%s -> ",temp->course);
            temp=temp->next;
    }
    printf("NULL \n");

}

void addNode(struct node **head, char* s)
{
    struct node* temp = malloc( sizeof(struct node) );
    temp->course= malloc(sizeof(char)*10);
    strcpy(temp->course, s);
    if(*head == NULL){
    temp->next = *head;
    *head=temp;
    }
    else{
    struct node* temp2;
    temp2= *head;
    while(temp2->next)
            temp2=temp2->next;
    temp->next=temp2->next;
    temp2->next=temp;
    }
}

void addlinkednode(struct linked **head,char* prereq)
{
    struct linked *temp = malloc( sizeof(struct linked) );
    temp->data= malloc(sizeof(char)*10);
    strcpy(temp->data, prereq);
    if(*head == NULL){
    temp->next = *head;
    *head=temp;
    }
    else{
    struct linked* temp2;
    temp2= *head;
    while(temp2->next)
            temp2=temp2->next;
    temp->next=temp2->next;
    temp2->next=temp;
  }
}

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2021-11-13
    • 2021-08-21
    相关资源
    最近更新 更多