last day (14 days later) » 

12:23 AM
0
Q: Copying linked list to another list in C

cosmoonotHave a question about copying a linked list to another list in a container. At the moment, my code is copying data from the global list and storing them in a temporary list and that data is stored in "student" node of the container. However, the result returned from the function halts the program...

 
Could you post the rest of your code?
 
Sure. It's long, let me create a codepen post. or could I send you a message with the code?
 
I think codepen would be best in case others refer to this question later?
Is your issue the return value of the function, or the console output from the printf statements in your code?
 
Issue is with the return value. Full code: codeshare.io/5zl6BN
 
My mistake then... full code should not be necessary. Sorry for figuring that part out after you uploaded it.
 
12:23 AM
The issue still remains. Would you mind running the code. You will need to add student names first with the format "Name:1st". Then print using the option 'n'.
Hey, moved this to the chat. Would be easier to converse.
 
indeed
 
Thanks for looking at the code.
 
i ran it, and i'm poking through the code.
no problem
 
It has been good 10 years since I have worked with C.
 
i don't think the issue is in the method you posted, at least after you remove the current->next = NULL
 
12:26 AM
so, the problem is in the last function of the code.
something is going wrong with the copying, but certainly feel free to check it out.
To run the code, you will need to add student names first, using 'a', then add student with "Tom:1st", "Jen:2nd".
After that you can use the option 'n', it will throw the error
 
Yeah, I did that
and when I remove the line, I get all of the students I entered
 
can you post a screenshot?
 
sure
Actually, here's the output from the n to the next prompt:

n
marydoe
tomdoe
janedoe
johndoe

List of students sorted by name:

Name: marydoe
Standard: 2nd
Absence on file: No absence documented.

Name: tomdoe
Standard: 1st
Absence on file: No absence documented.

Name: janedoe
Standard: 2nd
Absence on file: No absence documented.

Name: johndoe
Standard: 1st
Absence on file: No absence documented.
 
What in the world?
 
But, keep in mind that once you run this method, the rest of your list disappears
or rather, you can't run it a second time
 
12:31 AM
Let me upload my screenshot
 
because you overwrite the value of list
sure
can you also show me your code after you removed the line?
just the one method
it's possible something went wrong when you recompiled the code too
 
Sure
There you go. Code on my end hangs up everytime.
 
there's an asterisk on the file name, did you save it after changing it?
that's happened to me before... more times than I'd care to admit
try saving, recompiling, and running again
i think that'll fix your issue
 
Yup, saved and recompiled.
That is error I get everytime.
Why would I see a different result than yourself?
 
Well, I did remove your _CrtDumpMemoryLeaks function call
Since I don't have that library
But I feel like there's a different cause
are you testing with some absences reported?
try running with no absences reported
 
12:40 AM
Nope, at the moment there are no absences
 
your code does appear to be halting while attempting to print info about the absences...
since it isn't printing "no absences documented"
try commenting out the part that prints the absences in display?
 
I will check on that function, though it shouldn't halt after copy function, right?
 
Well the copy function is running just fine, otherwise your code wouldn't print at all - you call display after copy
I'm also running the code on linux, not windows
 
True, let me comment out absences section
give me a second
 
I think I know the issue - in add_student, you don't explicitly set absents to NULL
 
12:44 AM
k, see you're right, I commented out the absences section, now it prints the names
but it still hangs up
it prints all three student names, I enter and then halts again
 
hangs up even without printing absents?
 
yup
Let me upload a screenshot
 
well if you're hitting enter after printing, it may be because your code is expecting there to be input of a character,
or does it halt without you pressing any buttons
 
halts without pressing enter
or any button
 
hmm... let me check the code again
 
12:46 AM
Would mind checking my add function?
add_student
Maybe I'm not creating the linked list properly? Which might have an issue?
 
ok, it's crashing at remove_all
and yeah, like i mentioned, your add_student is missing something
you need to explicitly set absents to null for all new students
 
aaahhhh
 
because, as I mentioned above, some implementations will not set absents to 0 when you use malloc
 
Sorry, let me change that really quick.
DUDE! You're awesom1
awesome!
 
no problem sir, i'm glad i could help
 
12:51 AM
Give me a second to test this.
 
also, something else you may want to be aware of: if you choose n twice in a row, the students will be printed the first time, but not the second
this is because you are overwriting the variable list. Instead, you should have a temporary variable of type struct container *
set this temporary variable to list before the while loop, and then replace list with the temporary variable everywhere else in the list_by_name method.
 
Quick question: for the add_student function
I should set the abscent to NULL? tempStudentList->absents = NULL;
 
yes
 
hmmm...code still halts
void add_student(char* name, char* standard)
{
struct student *tempStudentList = (struct student *) malloc(sizeof(struct student));
struct container *tempMainContainer = (struct container *) malloc(sizeof(struct container));

// store student data into list
strcpy(tempStudentList->name, name);
strcpy(tempStudentList->standard, standard);
tempStudentList->absents = NULL;

// store the student info into main list
tempMainContainer->student = tempStudentList;
tempMainContainer->next = list;

// save to global list
 
show me a screenshot?
 
12:55 AM
I think, I will need to set the next value of absents too?
 
where is it halting?
no, since you're setting it to NULL
so it won't have a next value that you can set
 
hmm... it appears to still be crashingin remove_all
add a printf statement to the bottom of that function (just print like... "deleting") and show me the output?
or actually at the top of the if-statement
 
k
I don't see the code going into remove_all immediately
printf has been added above IF statement in remove_all function
 
oh, ok, i think i found it
so, in list_by_name
the last node will have next set to some non-NULL value since it's never explicitly set to null
so:
after tempStudentList->absents = tmp->student->absents;, add tempStudentList->next = NULL;
 
1:02 AM
aaah k
let me add that
 
oh wait no
my mistake
after tempMainContainer->student = tempStudentList;, it'll be tempMainContainer->next = NULL;
 
K, let me recompile
HA!
That worked!
 
nice
 
would you mind, hang on a moment?
 
sure
 
1:05 AM
Haha...It took 6 hours to figure that out. Sigh...this is what happens when you don't code in C for 10 years
 
yeah, it is a bit of a minefield
 
Lol
 
I posted the final, corrected version of the method on the original question
note that I replaced list with a temporary variable. If you haven't done that, you'll probably want to
 
Let me check
which line did you modify?
 
I replaced list with tmp in every location, removed current->next = NULL, added tempMainContainer->next = NULL, and added the declaration *tmp = list at the top of the method (on the same line as your other declarations)
 
1:09 AM
Aaahh perfect!
Thank you!!!
dude!!! wish I could buy you drink
 
no problem, again - I'm glad I could help
 
Glad I wasn't too far off
 
and haha, just accepting my answer will make me happy :) usually it's a small detail that causes the whole thing to crash
the code was really well written for someone who hasn't programmed in C for 10 years
 
Certainly accepted your answer, btw...I have one more function to finish.
is there a way to message you if I get stuck?
Well, yes, I still code.
Just not in C.
 
i think so - can you send direct messages through SO?
 
1:11 AM
I'm more of web programmer, last time I coded in C was in college. Feels like years ago.
No clue?
 
ahh, got it. makes sense
 
Would you mind if I left this chat on?
 
sure. or, reach me at facebook - facebook.com/amgoyal
that might be easier for me
 
Awesome! Thanks. I will leave this on, but if it gets disconnected, I will message on facebook
k, sounds good.
Facebook it is
I know the remove_one function is working, but wouldn't mind your thoughts.
Could I refine the code to be better?
 
ahh I no longer have the code, and I would've referred to the codepen, but it appears gone
 
1:27 AM
Oh
hang on
Problem now appears when I add absences for students
so, I'm thinking something is wrong in add_student function?
after I add absences, then try option 'n' again, program once again halts
 
make sure that every time you add an absence, you set the next for the last absence to null
 
You're right
let me modify it
 
and remove_one doesn't free the absences for any students that are removed. it also doesn't free the student struct for the student unless it is the head
 
void add_absent(char* name, char* date)
{
struct container *tempList = list;
struct absent *tempCheck = (struct absent *) malloc(sizeof(struct absent));

// loop through the list and find the student name
while (tempList != NULL) {
if (strcmp(tempList->student->name, name) == 0) {
// store the checkup data
strcpy(tempCheck->date, date);
// assign to student
tempList->student->absents = tempCheck;
tempList->next = NULL;
}
tempList = tempList->next;
}
}
I added tempList->next = NULL;
still halts, but prints the first absences
then it hangs up
I think, it needs to be "tempList->student->absents->next = NULL;" instead
 
yeah, you need the next for the last absence to be null
which is what your second recommendation would do
 
1:38 AM
Yup, k, no more halt issues, but now it only prints the last absence that was added.
It seems like the first absence is being overwritten.
Let me know, think there a few more small bugs, which I need some guidance on. I would appreciate the help. brb.
 
after creating tempCheck in addAbsent, you want to set tempCheck->next = NULL
not in the other place
then it will work
or rather when you find the correct student, tempCheck->next = tempList->student->absents;
 
let me check, in which function?
 
in add_absent
void add_absent(char* name, char* date)
{
struct container *tempList = list;
struct absent *tempCheck = (struct absent *) malloc(sizeof(struct absent));

// loop through the list and find the student name
while (tempList != NULL) {
if (strcmp(tempList->student->name, name) == 0) {
// store the checkup data
strcpy(tempCheck->date, date);
// assign to student
tempCheck->next = tempList->student->absents;
tempList->student->absents = tempCheck;
}
tempList = tempList->next;
}
}
 
1:55 AM
HAHA!
Who are you?
 
lol what do you mean?
 
omg...you're adding in the missing pieces
this is awesome!
that was it.
 
yeah, once you find a few of the bugs, the rest follow similar patterns
 
I have a few more, I think
 
i think based on these you'll be able to find the others. it seems the general missing piece is failing to set the last node's next pointer to null, or setting the first node's next to null
 
1:56 AM
Could you show me the changes for remove_one?
 
that's going to be a bit more involved, and i think you'll want to refer to your remove_all function for how to properly free memory
remove_all looks correct to me
 
k, sounds good. I will check the remove_all
I think, there is one bug with option 'b'
let me check that out really quick
 
looks to me like it is probably the line "temp->next = NULL"
 
Yup, there is a bug, where I can add many students under a standard like 1st
but only one shows up, when choose the option B
 
refer to my earlier comment - temp->next = NULL is the likely cause
 
2:00 AM
k, let me check
I removed, but still only prints one student from a specific standard
 
tempMainContainer->next = NULL - should be tempMainContainer->next=temp2
 
Infinite loop
struct container* list_of_standard(char* standard)
{
struct container *temp = list;
struct container *tempMainContainer = (struct container *) malloc(sizeof(struct container));
struct container *temp2 = (struct container *) malloc(sizeof(struct container));
struct student *tempStudentList = (struct student *) malloc(sizeof(struct student));

// loop through and the student based on standard
while (temp != NULL) {
if (strcmp(temp->student->standard, standard) == 0) {

// store the data into student container
 
temp2 should initially be NULL
rather than malloc'd
 
updated, still infinite loop
struct container* list_of_standard(char* standard)
{
struct container *temp = list;
struct container *temp2 = NULL;
struct container *tempMainContainer = (struct container *) malloc(sizeof(struct container));
struct student *tempStudentList = (struct student *) malloc(sizeof(struct student));

// loop through and the student based on standard
while (temp != NULL) {
if (strcmp(temp->student->standard, standard) == 0) {

// store the data into student container
strcpy(tempStudentList->name, temp->student->name);
Yup, still on infinite loop. brb
 
struct container* list_of_standard(char* standard)
{
struct container *temp = list;
struct container *tempMainContainer ;
struct container *temp2 = NULL;
struct student *tempStudentList ;

// loop through and the student based on standard
while (temp != NULL) {
if (strcmp(temp->student->standard, standard) == 0) {

tempMainContainer = (struct container *) malloc(sizeof(struct container));
tempStudentList = (struct student *) malloc(sizeof(struct student));

// store the data into student container
your initialization of tempMainContainer and tempStudentList should be occurring inside the loop, not outside
good luck going further, i'm sure you can get the rest of this
i'm going to have to sign off for the night
 
2:11 AM
That worked!
Thank you very much!
I highly appreciate all the help.
 
no problem
best of luck going forward
 

last day (14 days later) »