Splitting a linked list into two lists

Hi everyone. So I wrote a function that adds a set of XYZ coordinates to a linked list, and that all worked fine. But now I'm trying to figure out how to take that list and split it into two lists at a given number. I CANNOT FOR THE LIFE OF ME FIGURE OUT HOW TO DO THIS. Here is my code for the list of coordinates: (List of numbers comes out like "66 12 234 3 20 88 4 5 6."

I literally have no idea where to even start, so if someone could explain and push me in the right direction that would be great. I assume I have to have a splitList function that has two pointers that point to NULL? And then after that I have no idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  class XYZ
{
private:
    double x;
    double y;
    double z;
    XYZ* next;
    
public:
    
    void report()
    {
        cout <<x <<" " <<y <<" " <<z << " " ;
    }
    
    // all coordinates have to be at least +1; Constructor to initialize node
    XYZ( double x1, double y1, double z1 )
    {
        x = pushup(x1);
        y = pushup(y1);
        z = pushup(z1);
        next = NULL;
    }
    
    // returns number at least +1
    double pushup( double x )
    {
        if( x<1) { x = 1; }
        return x;
    }
    
    // access
    void setNext( XYZ *p  ) { next = p; } // mutator: sets the value of Next
    XYZ* getNext() { return next; } // accessor
};


// prototype
void addXYZ( double, double, double, XYZ*&);
void report( XYZ*);

int main () {
    
    XYZ *coordList;  // head of the list
    coordList = NULL; // pointer is initialized as empty
    
    addXYZ( 4 , 5 , 6, coordList );
    addXYZ( 3 , 20 , 88, coordList );
    addXYZ( 66 , 12 , 234, coordList );
    
    report( coordList );
    
    return 0;
}


// function to add an XYZ to the list
void addXYZ( double x, double y, double z, XYZ* &head )
{
    XYZ* pCoord = new XYZ( x, y, z );
    pCoord->setNext( head ); //pCoord->next = coordList;
    head = pCoord;
    
}

// takes a list of XYZ objects and reports their values
void report( XYZ* head )
{
    XYZ* current = head;
    while ( current != NULL )
    {
        current->report(); // report the number at that node
        current = current->getNext(); // move on to next node to report until it reaches the end (NULL)
    }
}
What would this do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void foo( XYZ* lhs, XYZ* & rhs ) {
  if ( lhs ) {
    auto bar = lhs->getNext();
    lhs->setNext( nullptr );

    if ( bar ) {
      if ( ! rhs ) rhs = bar;
      else {
        auto tail = rhs;
        while ( tail->getNext() ) tail = tail->getNext();
        tail->setNetx( bar );
      }
    }
  }
}
keskiverto,

Could you explain your code? I really don't understand what any of that means
"any of that"? Seriously?

Surely you do understand line 1, the getNext(), setNext(), if, else, while?

By syntax the lhs, rhs, bar, and tail are names (of variables), are they?

C++11 introduced nullptr to replace the less specific NULL.
C++11 gave auto a new meaning. Both here effectively evaluate to XYZ*.
Topic archived. No new replies allowed.