1

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.

5
  • 3
    "But it doesn't work out." — How so? Please edit your question and be specific. Please also include any debugging you have done. Commented Dec 20, 2016 at 20:19
  • 3
    The daily linked list question finally arrived. Commented Dec 20, 2016 at 20:21
  • 1
    @SanchkeDellowar 10 in 4 days. Commented Dec 20, 2016 at 20:25
  • 1
    Please don't cast the result of malloc Commented Dec 20, 2016 at 20:31
  • 1
    In link->next = NULL; the local variable LINK1 *link is uninitialised. Commented Dec 20, 2016 at 20:32

1 Answer 1

4

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

3 Comments

Thank you, it was very clear! ANd also helped me in getting a new idea for solving this exercise.Instead of converting the structure to a linked list, which is what I was thinking (which is obviously not a good idea), I think it is better to copy the values of this structure, that already holds some data, to a similar structured - NEW linked list, by using the code you suggested with small modifications!
@baskon1 I'm glad you liked the answer. I think that C sometimes is easier than other languages because you can be concise with C and you're not forced to do encapsulation all the time as in Java.
@DacSaunders don't forget to free memory

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.