baskon1
baskon1

Reputation: 330

Create a linked list in C, after creating the array that holds the structure

So what I want to do in brief is to create a structure, fill it in, and after that create a linked list. What I have done so far is this: I have defined a linked list as below. Then I have filled it in with about 40 variables for PASSENGERS

typedef struct list1               
{
char var1[10];
char var2[10];     
struct list1 *next;
}LIST1; 

Then I have dynamically allocated some memory to it (to host for example 40 structure items)

list  = (LIST1 *) malloc ((40)*sizeof(LIST1));

Filled it in with about 40 variables for PASSENGERS by using for example

for (i=0;i<40;i++)
{
strcpy(list[i].var1,"AAAAAAAA");
strcpy(list[i].var2,"BBBBBBBB");
}

And what I need to do now is connect all these values, that are already there in a linked list, and print the result by using this linked list.

I am trying something like :

LINK1 *link, *start=NULL, *tmp;

for (i=0;i<40;i++)
    {link->next = NULL;
    if (start==NULL)
        start = link;
    else{
        tmp=start;
        while (tmp->next !=NULL) tmp=tmp->next;
        tmp->next=link;
        }

     }

and then to print the result of var1

while (tmp!=NULL)
{
    printf("%s",tmp->var1);
    tmp=tmp->next;
}

The code runs, but it doesn't print out anything. This is just a part of an exercise I am trying to solve, and I started with something simple to see how it works. I don't want to allocate memory only for one element at a time. I am asked to allocate all the memory necessary, fill in the structure and then create the linked list.

Upvotes: 1

Views: 166

Answers (1)

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26681

You are on the right way. You're not forced to use a typedef, you may use just a plain struct for your list and clearly use the name node so that you don't mix up nodes and lists. A node followed by a node is a list.

struct node {
    char * var1;
    char * var2;
    struct node *next;
};

Then you might use a helper function to push a node.

void pushvar1(struct node **head_ref, char *new_data) {
    struct node *new_node = malloc(sizeof(struct node));
    new_node->var1 = strdup(new_data);
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

And a function to print the nodes

void printListvar1(struct node *node) {
    while (node != NULL) {
        printf(" %s ", node->var1);
        node = node->next;
    }
}

Now you're just a main function short of success.

int main() {
    struct node *head = NULL;
    int i = 0;
    for (i=0;i<40;i++)
    {
        pushvar1(&head, "AAAAAAAA");
    }

    puts("Created Linked List: ");
    printListvar1(head);
    return 0;
}

Complete program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
    char *var1;
    char *var2;
    struct node *next;
};

void pushvar1(struct node **head_ref, char *new_data) {
    struct node *new_node = malloc(sizeof(struct node));
    new_node->var1 = strdup(new_data);
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

void printListvar1(struct node *node) {
    while (node != NULL) {
        printf(" %s ", node->var1);
        node = node->next;
    }
}
void freeList(struct node* head)
{
    struct node* tmp;

    while (head != NULL)
    {
        tmp = head;
        head = head->next;
        free(tmp->var1);
        free(tmp);
    }

}
int main() {
    struct node *head = NULL;
    int i = 0;
    for (i = 0; i < 40; i++) {
        pushvar1(&head, "AAAAAAAA");
    }

    puts("Created Linked List: ");
    printListvar1(head);
    freeList(head);
    return 0;
}

Test

./a.out 
Created Linked List: 
 AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA  AAAAAAAA 

Upvotes: 4

Related Questions