簡體   English   中英

在C中的鏈表中實現鏈表

[英]Implementing linked list inside linked list in c

我正在開發一個更大的程序,並且想在一個鏈表中實現一個鏈表。 這樣我就有了一個主鏈表,其中的節點相互連接,但是在每個節點的內部是另一個鏈表。 我寫了一些我認為可以實現的方式的代碼,但是在將第二個鏈接列表鏈接到主鏈接列表的節點時遇到了麻煩。 以下是我的嘗試,但再次無法將它們連接在一起

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

我希望此示例代碼輸出具有一個,兩個,三個...的鏈表。但是,在“一個”節點內,我們有一個“ john,bravo,gabe”鏈表,對於“兩個”節點等...連接這些鏈表的任何幫助都將非常有用。

您的代碼中有很多修復程序。 請查看以下更改並正確修復。 我已經解決了您的主要鏈接列表的創建問題。 我不清楚您在鏈接列表中的鏈接列表。 所以我創建了兩個列表,您可以在所需的位置適當地鏈接它!

我已經修改了您的邏輯,以將節點添加到鏈表的末尾。 希望這個能對您有所幫助!

嘗試以下更改-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM