1

im working on a larger program and wanted to implement a linked list inside a linked list. So that i have a main linked list with nodes connecting to each other, but inside each node is another linked list. I wrote some code of the way i think it could be implemented but im having trouble linking the second linked list into the node of the main linked list. The following is my attempt but again im having trouble connecting them all together

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

i would like this example code to output a linked list with one,two,three.... being there own node, but inside the "one" node we have a linked list of "john,bravo,gabe", same goes for the "two" node etc... any help in connecting these linked list would be great.

5
  • 2
    essentially to add nodes to your linked list inside your list you would do head2->head = addlinkednode(head2->head, text2[i]);. However the strcpy in your addNote functions might not do what you want (don't use strcpy), and you are adding nodes at the beginning of the list instead of the end. Also you should check for malloc() return value, and finally you need to alloc your string inside your node. Commented Aug 5, 2014 at 7:13
  • Affirming what @Optokopper stated, strcpy(temp->course, s); isn't looking too hot when temp->course in addNode is an indeterminate target. You never allocated memory for the copy (you allocated space of for the node, but the pointer within is still indeterminate), so that line invokes undefined behavior. The same is true for strcpy(temp->data, prereq) in addlinkednode. Commented Aug 5, 2014 at 7:22
  • for(i = 0; i < size2; text2[i]) here I think you should use i++ instead of text2[i]. Commented Aug 5, 2014 at 7:35
  • okay i see how i need to alocate memory for the string. and is Optokopper correct in the way he links the list into each node? thanks Commented Aug 5, 2014 at 7:35
  • I suspect you want something like this, but its somewhat hard saying with the posted code. Anyway, have a look, and I leave the list and sublist cleanup (free() all those allocations) to you. Best of luck. Commented Aug 5, 2014 at 8:10

1 Answer 1

1

You have lot of fix's in your code. see the following changes and fix them correctly. I have solved the creation of your main linked list. I am not clear about your linked list inside a linked list. So i created two list, you link it appropriately where you want!

I have modified your logic to add the nodes at the end of the linked list. Hope this will help you!

Try the following changes-

#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;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.