2

I made this program to learn about linked lists as I am just starting out with them. The program terminates immediately after the statement "Enter the amount of pesticide"(it is a school assignment). Also, I am unsure how do I keep the length of the list limited to the size input by the user.

#include<stdio.h>

struct plants{
    int val;
    struct plants *next;
};

void printlist();

int main(){
    struct plants* head = NULL;
    struct plants* current= head;
    head = malloc(sizeof(struct plants));
    int counter,size;

    printf("Enter the number of plants\n");

    scanf("%d",&size);

    printf("Enter the amount of pesticide each plant has.\n");

    while(current!=NULL){
        scanf("%d",current->val);
        current= current->next;
    }
    return 0;
}
9
  • 1
    your current is null when you start the loop, as the reason it won't even enter your loop Commented Feb 14, 2016 at 12:55
  • @sameera sy What do I initialise it to? I have tried 0 with the same result. Commented Feb 14, 2016 at 12:56
  • 1
    You need to make current point to head. As it is not pointing at anything when loop starts. And you have to allocate memory for every new node as loop iterates. Commented Feb 14, 2016 at 12:57
  • 'What do I initialise it to? I have tried 0 with the same result' Aaarrghhhh! Commented Feb 14, 2016 at 12:57
  • use this set of statements struct plants* head = NULL;head = malloc(sizeof(struct plants)); struct plants* current= head; Commented Feb 14, 2016 at 12:58

3 Answers 3

1
#include<stdio.h>
#include<malloc.h>

int main()
{
    int count = 0;
    int size = 0;        
    printf("Enter the number of plants\n");
    scanf("%d",&size);

    printf("Enter the amount of pesticide each plant has.\n");

You have to allocate the memory for each node inside the while loop. If you like to add the new node at the end of the list notice the end of the list by a pointer to the pointer at the end of the list. Apart form this you have to pass the addres of the value to be read to scanf:

    struct plants * head = NULL;
    struct plants ** current = &head; // current refers there, where next node has to be placed
    while( count < size ) // do it for "size" nodes
    {
        *current  = malloc(sizeof(struct plants)); // allocate memory for the node right to target 
        scanf( "%d", &((*current)->val));          // read the data
        (*current)->next = NULL;                   // node is last node in list, so its successor is NULL
        current = &((*current)->next);             // step on forward
        count ++;                                  // increment number of nodes
    }

Note since the type of current is struct plants ** this codes puts the new node to head for the first element of the list and to (*current)->next for all further nodes of the list.

It would be easier to add the new node at the head of the list:

    struct plants * head = NULL; // init head with NULL (this becomes end of the list)
    while( count < size ) // do it for "size" nodes
    {
        struct plants * current = malloc(sizeof(struct plants)); // allocate memory for the node 
        scanf( "%d", &current->val);       // read the data
        current->next = head;              // successor of node is head
        head = current;                    // new node is head of list
        count ++;                          // increment number of nodes
    }

This will print your list:

    struct plants *temp = head;
    while( temp != NULL )
    {
        printf( "%d ", temp->val );
        temp = temp->next;
    }

Don't forget to free the list at the end of your program:

    while ( head != NULL )
    {
        struct plants *next = head->next;
        free( head );
        head = next; 
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Why did you use the while loop at the end?
@cenobia The last while loop is to free the memory allocated by malloc. Ther has to be one free for each malloc.
I noticed you used &counter->val and *(current->val). Cant we use counter->val in both places?
@cenobia In the first case the type f current is struct plants **, but in the second case it is struct plants *. So the address of member val is &(*current)->val for the first case and &current->val for the second case.
Okay. And when I try to print the list using your code, it keeps on printing absurd integers(infinite loop),
|
0
struct plants* head = malloc(sizeof(struct plants));//declare an initialize in memory
struct plants* current= head;
int i = 0;
struct plants* aux = NULL;
while(i ++ < size)
{
    aux = malloc(sizeof(struct plants)); // used for new values
    scanf("%d", aux->val);
    aux->next = NULL;
    current->next = aux;
    current = aux;
}

Loop while you have less than the quantity required by user. Use an auxiliar node. Read the value, set the current next position to the aux node, the aux node to null and set the current node to aux to be sure you are at the last node from the list.

Comments

0

Lot of things to be corrected, check for the code below. and should be pretty easy for a beginner to understand. I haven't changed the format of your code for ease of understanding

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

struct plants{
    int val;
    struct plants *next;
};

void printlist();

int main(){
    int i;
    struct plants* head = NULL,*temp=NULL;
    struct plants* current= head;
    int counter,size;
    printf("Enter the number of plants\n"); 
    scanf("%d",&size);  
    printf("Enter the amount of pesticide each plant has.\n");
    for(i=0;i<size;i++){
        temp = malloc(sizeof(struct plants));
        scanf("%d",&temp->val);
        temp->next=NULL;
        if(head==NULL)
        {
            head=temp;
            current=head;
        }
        else
        {   
            current->next=temp;
            current=temp;
        }
    }
    current = head;
    while(current!=NULL){
        printf("%d\t",current->val);
        current= current->next;
    }
}

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.