user10591439
user10591439

Reputation:

Linked Lists user insert a node (C)

i want to make a linked list where a user insert nodes. In this code i will use tables (but this is not my problem now) and a print method (which is not my problem for now too).

I want to save in every node the following data :

1) ROW (int)

2) COLUMN (int)

3) VALUE (float)

I want every time a node is inserted (it will be inserted in the first position), the head will be the reference to the first node (which it was inserted).

For instance i want to be something like this:

Enter the row : 2

Enter the column : 3

Enter the value : 7

Now the head should refer to this node like this :

HEAD -> 237 -> NULL.

And then i use again the same method to insert a new node with the following data:

row: 1

column: 2

value: 9

And now my result should be : HEAD -> 129 -> 237 -> NULL.

How can i do this?

I have the following code :

typedef struct node     {
int     row;            /* element' s row */
int     column;         /* element's column*/
float   value;          /* Value of element*/
struct  node * next; } node;    /* next element */

typedef struct table{   
node *  head;                   
int     number_of_rows;         
int     number_of_columns;}table;




void insertNode(table * input) {

struct node *nod;

printf("Enter the number of rows: "); /*Nevermind for this */
scanf("%d" , &input->number_of_rows);

printf("Enter the number of columns : "); /*Nevermind for this */
scanf("%d" , &input->number_of_columns);

/* My question is about the following code */ 

printf("\n Enter the row : ");
scanf("%d" , &nod->row);

printf("Enter the column : ");
scanf("%d" , &nod->column);

printf("Enter the value : ");
scanf("%f" , &nod->value); }

I dont know how to use the head variable and how i can refer to the first node so i didnt use other variables.

Upvotes: 0

Views: 154

Answers (2)

Ian Abbott
Ian Abbott

Reputation: 17413

It looks like you want to input the entire table in insertNode since you are reading in values for input->number_of_rows and input->number_of_columns. As part of the table initialization you should mark the list as empty by setting head to NULL:

    input->head = NULL;

For each new node to be added to the list, you need to allocate storage dynamically:

    nod = malloc(sizeof(struct node));

For robustness, you should check for successful memory allocation and take appropriate action if it failed (nod will be NULL if the allocation failed):

    if (nod == NULL) {
        /* just an example of one way to handle the error */
        fprintf(stderr, "Node allocation failure\n");
        exit(EXIT_FAILURE);
    }

Use the following code to add the new node to the head of the list:

    nod->next = input->head;
    input->head = nod;

Upvotes: 0

NoShady420
NoShady420

Reputation: 941

You declared a struct pointer but didn't make it point to a valid memory address. Change struct node *nod; to struct node *nod=malloc(sizeof(node));

Upvotes: 0

Related Questions