I am having a problem with my linked list. I’m pretty sure it is my pointers being off, or I didn’t pass a pointer in the correct way as I am new to c. Structs are also new to me, and c++ is the language I am used to and there are more differences than I was aware of. I could do this program in c++ in no time but anyway here is my code.
void add_process(struct process new_process, struct process *head, struct process *current){
new_process.next = NULL;
if(head == NULL){
head = &new_process;
current = head;
head->next = NULL;
}
else if(new_process.timeNeeded < head->timeNeeded){
temp = head->next;
head = &new_process;
new_process.next = temp;
}
else{
current = head;
while(new_process.timeNeeded > current->timeNeeded){
temp = current;
current = current->next;
}
temp->next = &new_process;
new_process.next = current;
}
}
I am reading values from a file into the process, the only one I am currently using is timeNeeded which is an int. And I am trying to order the list by shortest timeNeeded first.
int main(){
FILE *readfile;
readfile = fopen("data.txt","r");
head = NULL;
current = NULL;
while(fscanf(readfile, "%s %i %i %i",
&new_process.processName, &new_process.arrivalTime,
&new_process.timeNeeded, &new_process.priority) != EOF) {
add_process(new_process, head, current);
}
current = head;
while(current->next != NULL){
printf("%s %i %i %i\n", new_process.processName, new_process.arrivalTime, new_process.timeNeeded, new_process.priority);
current = current->next;
}
return 0;
}
The program crashes at the print which is not the problem. The first problem is that it my program enters the if(head==NULL) loop everytime and inserts there. So head is probably never being changed but I am not sure how to fix that, I’m pretty sure its a double pointer but not positive. And I am also am sure there are other problems so if you could point me into the correct direction, and if I’m doing anything completely wrong let me know.
EDIT: OK so after adding the pointer to head I get an error at head->next = NULL saying “expression must have pointer-to-class type.” Tried adding a * before head but didn’t seem to help. Anyone know how to fix it?
Your add_process function here:
Takes whatever pointer you pass into it by-value. That means after your call in the while loop here:
head will still be NULL because it never got changed. To have it actually change head pointer and not some other pointer modify your add_process to take a double level pointer:
Another problem with your above code is that the
new_processargument is taken by value as well. So this is a temporary copy of whatever process you passed in. Onceadd_processreturns,new_processgoes out of scope. Now that means you have a dangling pointer in your linked list pointing to invalid memory.To fix this you should use malloc to dynamically allocate the memory and then make a copy of new_process. Then have your linked list point to the
malloc‘ed process. Objects created on the heap with malloc will persist until it’s freed.Here’s a quick example to give you an idea:
Don’t forget to free the malloc’ed memory when done. This is best done in your process cleanup function — the destructor equivalent in C++ only you have to call it manually.