I’m writing a blackjack program and would like to be able to write the top scores to a file. Obviously the first few times the program is run it will populate the high score list with every score, after which I’d like only scores that are greater than the number 10 score to be added, and the initial number 10 score (now number 11) to be deleted. I’ve been thinking of using a linked list like this:
struct highScore
{
char name;
int score;
highScore *next;
};
My knowledge of linked lists is pretty basic so I intend on doing my research before I can actually code it.
I’m wondering if I’m overcomplicating this and if there’s a simpler way to get the job done, or am I on the right track here?
I do not think that linked list is best approach. Do not take me wrong, but linked list is used to add and to remove at random position, most commonly at beginning or end. Problem whit linked list is that it has same inefficiency to find element as ordinary array, becuase it has to check each element until it finds one.
I think that using array will make your program having same efficiency and less complicated code.
Comparing both data structures (linked list and array):
Finding element is in both case proportional to length in average.
Inserting element at end is constant in both structures.
Linked lists are efficient to add element at any position, but position has to be found, so this compensates whit problem that array has, where you have to move right elements by one position on right.
I think that something like
Will make simple solution whit same efficiency as linked list.
If you implement your structure as linked list you will not be able to serialize this directly in file, because you can store only name and score, but not pointer on next highScore. Pointers can not be stored in file, because they are dynamical allocated and are valid only during program lifetime.
If you are disappointed whit this solution, you can check heap and tree for ultimate efficency for more than few (like in your case) scores.