1. ## Linked Lists - Driving me insane

Hello all,

I am writing a function for a program which takes the first number from a linked list and uses it elsewhere in the program. It is meant to take the start pointer and if that number is the last number in the list, generate 10 more numbers.

But, it seems to take the LAST pointer from the list and it does not remove it from the list properly because the next time a number is needed it is the same one. The code is as follows:

Code:
```int removeFromList(NODE *startPtr)
{
int number, a;
NODE *currentPtr,*previousPtr;

previousPtr = currentPtr = startPtr;
currentPtr = startPtr->nextPtr;

if(currentPtr->nextPtr == NULL)
{

/* generate more numbers */

number = currentPtr->number;
for(a = 1; a <= 10; a++)
{
number = (number + 11) DIV 10;
number = createNewNumber(number);
}
}

else
{

/* I think the problem is here */

while(currentPtr->nextPtr != NULL)
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
number = currentPtr->number;
previousPtr->nextPtr = NULL;
}
return number;
}```

The 10 new numbers are generated successfully when required. The problem is just reading the first number and then removing it from the list.

*** Thanks for your help. ***

2. The removeFromList function is used to get the next number from a linked list and use it as an ID number. It automatically assigns this ID number and the user then enteres their name. The numbers have been added to the linked list by reading a file and getting the numbers from that.

The initialiseList and addToList functions are as follows:

Code:
```NODE *initialiseList()
{
NODE *startPtr;
startPtr = (NODE *)malloc(sizeof( NODE));
startPtr -> nextPtr = NULL;
return startPtr;
}

{
NODE *currentPtr,*previousPtr,*newPtr;
newPtr = (NODE *) malloc (sizeof(NODE));
newPtr -> number = number;
previousPtr = currentPtr = startPtr;
currentPtr = currentPtr->nextPtr;

while(currentPtr != NULL)
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}

newPtr -> nextPtr = currentPtr;
previousPtr->nextPtr = newPtr;
}```

I hope I've provided the information you need.

*** Thanks ***

3. Has anyone got any ideas about why the list prints backwards and does not remove the node? (I have to hand the assignment in an hour).

4. I noticed that InitializeList() is actually a perfect function for making new nodes, so I just renamed it "NewNode()" in this example:

Code:
```void addToList(NODE *startPtr, int number)
{
NODE *currentPtr, *newPtr;
newPtr = NewNode();
newPtr -> number = number;

currentPtr = startPtr;

while(currentPtr->nextPtr != NULL)
{
currentPtr = currentPtr->nextPtr;
}

currentPtr->nextPtr = newPtr;
}```

I hope that helps to clarify the simplicity of iterating through a list.
As to the other problem, I haven't quite looked through it all just yet.