I have a program that is supposed to read integers from a file and then create a link where the numbers would be in the correct order. If the node all ready exists, then I should increment the count of the matching node by one to count how many times the number appears in the file. The sorting and counting are obviously not correct by looking at the input and the output received. I would appreciate any help or suggestions anyone might can offer. Thanks in advance for your time!
//Node does not exist so create a new node
if((N != p->value))
{
c = p->linky;
temp = NewNode();
temp->value = N;
p->linky = temp;
temp->linky = c;
}
Try this change and see if it helps, I have not tested it but reading through your code it doesn't look like your connecting temp to the next node.
EDIT Added another change. You should note you dont take into consideration if the new node to add needs to be inserted as the new head.
Thanks! It gave me some ideas so I changed my code around a bit. I didn't have it linked together properly. It is now sorting and printing, but the count is wrong. It lists each number separately in a sorted order instead of grouping them. Any more suggestions or ideas are greatly appreciated! Thanks in advance for your time!
void Insert(LList *c, int N)
{
//Set the variables
Node p, temp;
c = ListHead;
p = NULL;
//Determine where variable needs to go
while(c != NULL && c->value < N)
{
p = c;
c = c->linky;
}
if(p == NULL)
{
//New Head Placement
temp = NewNode();
temp->value = N;
temp->linky = c;
ListHead = temp;
}
elseif((N != p->value))
{
//Create a new node
temp = NewNode();
temp->value = N;
p->linky = temp;
temp->linky = c;
}
else
{
//Not working properly, retains the 1 count from the node creation
//Does not create a new node, Should add 1 to the count of the matching value node
p->count = p->count + 1;
}
}