Big thanks in advance and sorry to post here in pieces; since I am new here, I was having problem posting it (sorry for the inconvenience).

Perhaps my problem is not in the code but the way I designed it.

I have some knowledge of C/C++ and am taking College Online classes. I have to/planning to take sometime next year an Advanced Data Structure class and since I think that it's not an easy subject, then I am starting to working/learning on it right now. The project I am working on it has the following requirements:

I am suppose to use struct (no STL, no classes) and implement a double linked list as an array of pointers.
My code has to read one integer at a time from the input file (integers will be separated by whitespace and the text file has 5-digit integers (10000 – 99999) ).

MY PROBLEMS (Assumption):

1) It will outputs only the last 5-digit integers (with digit 3).

In other words, it outputs data for the last/unique 5-digit integers from the file (in this case digit 3) and it's not outputing the other unique 5-digit integers data from the file such as those with starting digit 1 and 2 as it should be, based on the output (posted earlier in this post here) from the req/specs.

2) My ReadInFile function calls and check for duplicates (it's working) then, I am not sure what I do next if I read the whole data from the file and closed it, and then I will go to the list again, count it based on the first digit and display it HOWEVER I am confusing how I am going to do it. I have been trying for hours but with no success. As I mentioned before, perhaps my problem is more a matter of design then code but please let me know where are my mistakes.

I'm not sure where to start so I'll start from the beginning and type as I go through your code.

When checking your command-line parameters you have a series of if statements, but entry of one does not lock out the rest; this is undesire in your case as it will continue to attempt opening a file and perform other activities. You also don't need the argv[1] check that way. I am also not sure why the entire construct is in a do-while loop. Could you explain this? I've removed it for now as I didn't see the need for it. This means I've modified main to the following:

The next point of interest seems to be the "ReadInFile" function. In the first part you're reading integers from the file, and for every integer you obtain the first digit which serves as the index for the array of linked lists you supplied to the function. You loop condition is the stream itself which will fail after it read something. So you should change it to read or otherwise change the loop itself. The "10000" you us in there is a "magic number"; I'd let that be based on a define. You defined a constant called "UNIQUE_FIVE_DIGIT" but you didn't use it yet. You could replace it with something like "pow(10, UNIQUE_FIVE_DIGIT - 1)" or something, casted to an integer. I've kept it the same for now. You also don't initialize "count" (the reference parameter) in your function. It's initialized to -1 outside, which is likely to cause errors in counting. Initialize it to 0 in the function.

You check if the number is already in the list for the list for numbers starting with that digit with the function isDuplicate, and that function seems fine. (I've only added some const keywords to the function signature)

If it doesn't exist, you add it to the list for that digit, which is fine, but i think from there things go wrong. If it is already in the list you basically skip it. But you want your program to only count unique numbers, so something will have to happen there. You could remove the number from it's digit-list as soon as you find a duplicate, but then you have the "problem" of having to remember which numbers are blacklisted. Another approach you could take is to just read every integer and put it in the correct list first, and remove duplicates from the lists after reading. I went for the first approach because I think this is what you were trying to do with 'p'. If we find a duplicate we remove it from its digit list, and add it to the list 'p' which is basically a blacklist. I've renamed 'p' to 'blacklist' to make its purpose more clear.

This means your read loop transforms as follows:

// As long as something can be read from the file..
while (inFile >> number)
{
// Obtain the first digit of the read number.
newNumber = number / 10000;
// Only process the number if it's not in the blacklist.
if (!isDuplicate(blacklist, number))
{
// This one is a duplicate of a number we thought was unique.
if (isDuplicate(arrayPtr[newNumber], number))
{
// Remove the number from "arrayPtr" as it is no longer unique.
arrayPtr[newNumber] = removeNode(arrayPtr[newNumber], number);
// Add this number to the blacklist.
blacklist = prepend(blacklist, number);
}
// It is a new number.
else
{
// Add this number to the digit-list.
arrayPtr[newNumber] = prepend(arrayPtr[newNumber], number);
}
}
// Increase the total amount of numbers read.
count++;
}

Note that I had to add a "removeNode" function which removed an item from a list. I've tried to keep it like prepend in the sense that it returns a pointer to the new list:

// Removes the first node in "list" that has the value "value", if it exists.
// Returns a pointer to the head of the new list. (prepend-style)
node* removeNode(node* list, const int value)
{
if (list != NULL)
{
// We've found the node.
if (list->data == value)
{
node* rest = list->next;
// Set the pointers of the nodes before and after the found node, if present.
if (list->prev != NULL)
{
list->prev->next = list->next;
}
if (list->next != NULL)
{
list->next->prev = list->prev;
}
// Delete the current node.
delete list;
list = rest;
}
// Look for the node in the rest.
else
{
list->next = removeNode(list->next, value);
}
}
return list;
}

What you're doing next, is counting the unique values. You can do this while reading the integers, but I'll keep it the way it is now, and just "fix" it. The counter you use for the loop is called "counter" and the function also has a parameter called "counter". I recommend to avoid conflicts like these even if it goes right in this case. Also you're using '10' which is again a magic number. use the MAX_CELLS constant you defined.

You want to display the amount of elements in every list as well as their contents and you want to do that sequentially. Adding counters for the things you want to display isn't that difficult once you have the rest.

I don't know what happened but I just wrote you a lot and did a reply but all of the sudden it disappeared.

I will do it again -:)

Big thanks for your help. You are right about the loop inside main; I don't know why I did that -:)

It's kinda of late too me right now, about 3 am but I will reread your answers/comments again tomorrow and apply the changes where necessary/need to be.

I was able to work on it today and my main problem was that inside my loop I have arrayPtr[count] = p being overwritten instead of using p = arrayPtr[count]; and also I moved my call to displayIt inside the loop and etc.