在c中实现链表内的链表

时间:2014-08-05 07:01:42

标签: c linked-list

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

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;
}

我希望这个示例代码输出一个,两个,三个链接列表....有自己的节点,但在&#34;一个&#34;节点我们有一个链接列表&#34; john,bravo,gabe&#34;同样适用于&#34; two&#34;节点等...连接这些链表的任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

您的代码中有很多修复方法。请参阅以下更改并正确修复它们。我已经解决了主链表的创建问题。我不清楚链表中的链表。所以我创建了两个列表,您可以根据需要将其链接到适当的位置!

我修改了你的逻辑,在链表的末尾添加节点。希望这会对你有所帮助!

尝试以下更改 -

#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;
  }
}