Kris
Kris

Reputation: 23

Linked List counting to 20

I'm trying to count to 20 using linked lists. I have the code written up, with 2 functions. A function that adds the node to the linked list and one that prints the whole thing. however it's in an infinite loop and I have no idea why.

I tried debugging multiple times and I believe its the add function, possibly the printlist while is never getting to null and therefore its printing endlessly? I think the add function is not implementing the nodes correctly but I'm unsure how to fix.

This is my struct:

struct node
{
int number;
struct node *next;
};
typedef struct node NODE;

This is main, which calles the function add and passes it i (0-20) along with the list.

int main(){

int i, size=20;
struct node* mylist = malloc(sizeof(struct node));
mylist->number = NULL;
mylist->next = NULL;


for(i=0;i<size;i++){
   mylist = add(mylist,i);
   }   
  printList(mylist);


return 0;
}

add function, which takes temp as the data passed, and iter to iterate through the linked list to the end. When I take away the (&& iter->next->number < number) in the while loop, it prints out nothing but with it, it has the infinite loop

struct node* add(struct node *first, int number){
  struct node* temp;
  struct node* iter;

  temp= malloc(sizeof(struct node));
  temp->number=number;
  temp->next=NULL;

  if(first==NULL)
    return temp;

  iter=first;
  while(iter->next!=NULL  && iter->next->number < number){
    iter=iter->next;}

  temp->next=iter->next;
  iter->next=temp;

  return first;
}

This is the printlist function which I believe is correct, I think the problem lies in the Add node function.

struct node* printList(struct node *mylist){

struct node *helpptr;
helpptr = mylist;

while (helpptr != NULL){
 printf("%d", helpptr->number);
 helpptr = helpptr->next;
                      }
 return 0;
}

Thank you for your help in advance

Upvotes: 0

Views: 122

Answers (2)

jboockmann
jboockmann

Reputation: 1025

Your struct definition is fine, altough you you could leave out the typedef since you do not use it later on.

struct node
{
    int number;
    struct node *next;
};
// typedef struct node NODE;

You made two mistakes in the main part of you program. First - as already mentioned by @MikeCAT - you have to change the struct created using malloc to avoid undefined behaviour. You can do this by simply setting myList to NULL, since your add function will look after it. As you to add the items 0-20 you have to adapt the condition of your for loop to i<=size.

int main(void){
    int i, size=20;
    struct node* mylist = NULL;
    for(i=0;i=<size;i++){
       mylist = add(mylist,i);
    }   
    printList(mylist);
    return 0;
}

Your add function is fine, nothing to be changed here.

The printList function is fine as well. You might want to change the return type to void as this function does not return any meaningfull information so far. You actually do not need to create a the new pointer variable helpptr since parameters in C are called by value. Therefore changing the myList parameter will not change the value of it in the main function.

void printList(struct node *mylist){
    while (mylist != NULL){
        printf("%d, ", mylist->number);
        mylist = mylist->next;
    }
}

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

You invoked undefined behavior by using value in buffer that is allocated via malloc() and not initialized.

You should initialize mylist in main() with NULL, not what is returned from malloc().

Upvotes: 1

Related Questions