using c struct to implement linked list

You are not creating the new Box elements correctly in the loop. You have one struct Box that goes out of scope every pass through the loop. You would need to allocate each one dynamically via malloc() or else allocate an array that you draw from. Something like this:

   struct Box listOfBoxes[3];
   struct Box *addressKeeper;
   addressKeeper = &listOfBoxes[0];

   for (int i = 1; i < 3; i++)
   {
       scanf("%d", &(listOfBoxes[i].amount));
       listOfBoxes[i].previous = addressKeeper;
       addressKeeper->next = &listOfBoxes[i];
       addressKeeper = &listOfBoxes[i];
   }

However, you need to examine the next and previous pointer assignments carefully. There is still something wrong there that I didn’t change.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top