-1

I ran into a problem, I want to add item to the end of the linked list, but it seems that i am getting sucked in an infinite loop here.

void addCheckPoint(struct checkPoints **checkPoint) {
    struct checkPoints *checkPt = *checkPoint;

    while (checkPt->next != NULL) {
        checkPt->next;
        if (checkPt->next == NULL) {
            scanf("%c %d %d %d %d", &checkPt->dropOut, &checkPt->currentPoint, &checkPt->competitor, &checkPt->hour, &checkPt->minute);
        }
    }
}
Shepard
  • 1,111
  • 5
  • 16
  • 32

4 Answers4

3

You never update the value of checkPt in your loop. Change the line

checkPt->next;

to

checkPt = checkPt->next;

to fix this.

Note that there may be further problems with the function. Despite its name, it doesn't actually add anything to the list. It edits the contents of the tail item instead. If this isn't deliberate, you'll need to malloc a new element then add it to the tail.

simonc
  • 41,632
  • 12
  • 85
  • 103
2
void addCheckPoint(struct checkPoints **checkPoint) {
    struct checkPoints *checkPt = *checkPoint;

    while (checkPt != NULL) {
        if (checkPt->next == NULL) {
            scanf("%c %d %d %d %d", checkPt->dropOut, checkPt->currentPoint, checkPt->competitor, checkPt->hour, checkPt->minute);
        }
        checkPt = checkPt->next;
    }
}
Adeel Ahmed
  • 1,591
  • 8
  • 10
1

try this

 void addCheckPoint(struct checkPoints **checkPoint) {
        struct checkPoints *checkPt = *checkPoint;

        while (checkPt->next != NULL) {
             checkPt=checkPt->next;
            if (checkPt == NULL) {
                scanf("%c %d %d %d %d", &checkPt->dropOut, &checkPt->currentPoint, &checkPt->competitor, &checkPt->hour, &checkPt->minute);
            }

        }
    }
Charan Pai
  • 2,288
  • 5
  • 32
  • 44
0
#include <stdlib.h>
#include <stdio.h>

struct checkPoints
{
    char dropOut;
    int currentPoint;
    int competitor;
    int hour;
    int minute;

  struct checkPoints *next;
};

void addCheckPoint(struct checkPoints **checkPoint) {
    while (*checkPoint)
        checkPoint = &(*checkPoint)->next;

    /* FIXME: check malloc() return value */
    *checkPoint = malloc(sizeof (struct checkPoints));
    (*checkPoint)->next = 0;

    /* NOTE: the leading space in front of the %c */
    scanf(" %c %d %d %d %d",
          &(*checkPoint)->dropOut,
          &(*checkPoint)->currentPoint,
          &(*checkPoint)->competitor,
          &(*checkPoint)->hour,
          &(*checkPoint)->minute);
}

struct checkPoints *list = 0;

int
main ()
{
    addCheckPoint (&list);
    addCheckPoint (&list);
}
chill
  • 16,470
  • 2
  • 40
  • 44