9Cookiee
9Cookiee

Reputation: 45

C infinite loop with linked list

I am trying to make a program that initialize a singular linked list and then ask user for more data and add them at the end of current linked list. However, my programming is trapped in infinite loop.

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


typedef struct linked_list
{
int data;
struct linked_list *next;

}element;

typedef element *elementptr;


void addend(elementptr *,int);
void traverse(elementptr);


int main()
{
// create a linked list
elementptr first = NULL,
last = NULL;

int input,num;

printf("Please enter first integer data: \n");
scanf("%d",&input);

//Create a linked list with user initialized data


first = (elementptr)malloc(sizeof(element));
last = first;
last -> data = input;
last -> next = NULL;



printf("Please enter number of data you want to add in the end of linked list\n");
scanf("%d",&num);
addend(&last,num);
traverse(first);



return 0;
}


void addend(elementptr *l, int num)
{

int i = 0,extradata;

while(i<num)
{
    i++;


    printf("Please enter a data: \n");
    scanf("%d",&extradata);

    (*l) -> next = (elementptr)malloc(sizeof(element));
    *l = (*l) ->next;
    (*l) -> data = extradata;
    (*l) -> next = NULL;

}

printf("The data sets are added\n");
}



void traverse(elementptr f)
{
elementptr current;
int count = 0;

current = f;
while (current != NULL)

{
    count++;
    printf("The data is %d \n", current->data);


}

printf("The linked list has %d elements \n",count);


}

Upvotes: 1

Views: 1405

Answers (3)

ani627
ani627

Reputation: 6067

In your traverse function, the current node is not pointing to next node. So the while condition while (current != NULL) will not be true and hence the while loop is running infinitely.

To fix it, current node should point to the next node (current = current->next) in the list after printing the data.


For example:

void traverse(elementptr f)
{
    elementptr current;
    int count = 0;

    current = f;
    while (current != NULL)
    {
        count++;
        printf("The data is %d \n", current->data);
        // Fix 
        current = current->next;  // Point to next node in link list
    }
}

Upvotes: 0

Durwasa Chakraborty
Durwasa Chakraborty

Reputation: 374

current = current -> next; you are missing this statement in the while loop. Although I would suggest :

#include<stdio.h>
#include<stdlib.h>
struct linked_list
{
    int number;
    struct linked_list *next;
    struct linked_list *prev;
};
typedef struct linked_list node;
void create(node *p);
void print(node *p);
int main()
{
    node *head;
    printf("---Start of the list ---\n");
    printf("enter -1 to exit\n");
    head = (node*)malloc(sizeof(node));
    create(head);
    print(head);
}
void create(node *list)
{
     printf("enter the data");
     scanf(" %d",&list->number);
     if(list->number == -1)//Enter -1 to exit untill then keep on taking values
     {
         list->next=NULL;
     }
     else
     {
         list->next = (node*)malloc(sizeof(node));
         create(list->next);
     }
}

void print(node *list)
{
    if(list->next != NULL)
    {
        printf("%d-->",list->number);
        if((list->next->next == NULL) && (list->next->number!=-1))
        printf("%d -->End of the List",list->next->number);
        print(list->next);
    }
}

Upvotes: 2

Eric J.
Eric J.

Reputation: 150228

In your code

while (current != NULL)
{
    count++;
    printf("The data is %d \n", current->data);
}

you never change the value of current. Either it starts as NULL and the loop never executes, or it starts non-NULL and the loop runs forever.

You should add

current = current->next

after your printf to advance to the next element in the linked list.

Upvotes: 2

Related Questions