-2

So I'm trying to make a basic singly linked list, without tail and going to use another class "CarObject" to add it to my CarList, which is basically my linked list. The only issue is that after writing out the code and testing it, nothing seems to print/work, i dont even get an error at all. I'm kind of lost and not sure what i did wrong.

UPDATE!: lets say we ignore my code, but how would you implement an add function to a linked list(no tail)

class CarList
{
    class NodeType{

       friend class CarList;
       private:
         CarObject* data;
         NodeType* next;
     };



    public:
        void addCar(CarObject*);


    private:     
        NodeType *head;



};

void CarList::addCar(CarObject *car){


  NodeType* newNode;
  NodeType* currNode;


  newNode = new NodeType;
  newNode->data = car;
  newNode->next = NULL;

  currNode = head;


  while (currNode != NULL) {
    if (car->getYearModel().lessThan(currNode->data->getYearModel()))
      break;

    currNode = currNode->next;
  }

  newNode->next = currNode;
}

void CarList::print(){

    NodeType* currNode = head;
   while (currNode != NULL) {
    currNode->data->printTheCarInfo();
    currNode = currNode->next;
  }



}
  • Please provide a [mcve]. What is `CarObject` and where is your main program entry point? – Ron Oct 28 '18 at 21:20
  • 2
    It might be a good moment to learn [how to debug your program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Go step-by-step through your code with a debugger. As a starting point, check what is the value of `head` before and after insertion of the first element - is there any change? Is it correct? – Yksisarvinen Oct 28 '18 at 21:21
  • Do you also have a `main`? – Matteo Ragni Oct 28 '18 at 21:21
  • `add` does not match `addCar`. `CarList` does not define a `print` method. It's good that you are trying to minimize, but it's of limited benefit if you break the code in the process. – user4581301 Oct 28 '18 at 21:23
  • `main` does not need to be big. Just big enough to add a few cars and demonstrate the failure. – user4581301 Oct 28 '18 at 21:23
  • @alroithmhelp We can't compile your program. Please [edit] your post and provide a [mcve] so that we can compile it. – Ron Oct 28 '18 at 21:25
  • Lets just say we ingnore all the issues, how would you implement an add function for a linked list as simple as that – alroithmhelp Oct 28 '18 at 21:27
  • First thing I'd do is make sure when I'm inserting a node that the previous node's `next` pointer is updated to point at the inserted node. – user4581301 Oct 28 '18 at 21:28
  • @Yksisarvinen if you haven't produced an MCVE or something like it to help find the problem, you haven't done enough debugging. – user4581301 Oct 28 '18 at 21:30

1 Answers1

1

You forgot to update the next pointer of the previous node:

  NodeType* prevNode = NULL;
  while (currNode != NULL) {
    if (car->getYearModel().lessThan(currNode->data->getYearModel()))
      break;

    prevNode = currNode; //remember the previous node
    currNode = currNode->next;
  }

  newNode->next = currNode;
  if (prevNode)
      prevNode->next = newNode;
  else
      head = newNode; //if there's no previous node - newNode is the new head!
r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
  • There is a much better solution to this that carries a pointer to the `next` pointer so that you do not have to book-keep the head and `previous` pointer – user4581301 Oct 28 '18 at 21:32
  • @user4581301, I just think this solution is simpler and hence is better suited to explain the basics. – r3mus n0x Oct 28 '18 at 21:35
  • @alroithmhelp, there is no pointer to the previous node **in the node**. You need to update the `next` pointer of the node after which you are inserting the new one. – r3mus n0x Oct 28 '18 at 21:36
  • @r3musn0x but your solution is implementing a doubly linked list right? Its not doing singly linked list. I see my mistake im not keeping track of thte previous node, but now if i do im not doing a singly linked list no more. I'm doing a doubly linked list I think ? – alroithmhelp Oct 28 '18 at 21:37
  • @alroithmhelp, no this is still a singly-linked list, see my explanation above. – r3mus n0x Oct 28 '18 at 21:38
  • @alroithmhelp this is a singly linked list, but to put something in a singly linked list you need to know where to put it. That's the what the `previous` pointer is doing: Keeping track of where the new node goes. – user4581301 Oct 28 '18 at 21:38
  • Ooooooooooooh, I'm actually sooo stupid! I'm such a bad programmer :( I see what im doing wrong now, been coding for 6 hours straight no break, i think im taking a break now – alroithmhelp Oct 28 '18 at 21:40
  • @alroithmhelp One of the best ways to figure out linked lists is to draw pictures.Draw all the nodes and draw all of the links between them. When you want to perform an operation, draw the before and draw the after. Also helpful is drawing all the steps you need to get from before to after. Those steps become the code. – user4581301 Oct 28 '18 at 21:42
  • @alroithmhelp Here's an example of the pointer to pointer trick being used to remove a node: https://stackoverflow.com/a/22122095/4581301 See the community addition tacked on to the bottom. – user4581301 Oct 29 '18 at 04:00