Hello. Need help inserting at the end of a linked list

Hello everyone. I've started data structures this semester, I wasn't expecting to but had a sudden schedule change. I haven't touched c++ in a while so forgive me for making silly mistakes. I've finally gotten time to work on these 2 functions I've been told to finish, insert and intersection l, we are in groups but I cannot wait for group members to get on at 11 pm at night, I have sick family I take care of but I guess they don't care. Anyways so I've been sitting here couple hours trying to do this. I got insert at the front from the lecture a while back and now I've been drawing out the linked list and trying psuedo code for it
Assume we at the root node
Take in data x
Set next to NULL

If the list is not empty, check if the current node has its next set to null. If its not null, travel down the list until its NULL, otherwise set its data to x.
Now forgive me for this sloppy code, I'm in school without a laptop and typing from my phone, ill remember the structure best I can
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
insert(val_type x)
//TODO: implement this function
listNode* temp = root;
listNode* curr;
listNode* newNode;
temp->data = x;
temp->next = NULL;

if(curr->next != NULL) {
    curr->next = newNode;
    newNode = curr;
  }
else {
    curr->data = x;
     }
    


Yes Im aware its not efficient to insert from the back the professor knows that too but be wants to see if we can figure it out.
I'm not home to test and won't be for another 5 hours so I decided to post. Thanks!
Last edited on
So I'm home now, it compiles but I get segmentation fault. Any ideas would be appreciated.
Could this be moved to general c++ programming, I feel this might not be a beginner topic
Edit: So my group members are going OH WHY YOU USE NULL HE SAID USE ZERO HOW DARE YOU. >_>, I don't think thats the major issue and I'm pretty sure it gives the same effect.
Last edited on
Topic archived. No new replies allowed.