0

I'm currently working on a project about Linked Lists for my Data Structures class and I'm having a bit of trouble implementing an add method to the Linked List class. Currently, my class definition for the project are as such:

template <class Process>
class LinkedList {

   Process _info;
   LinkedList* _next;

   public:
      // Methods
};

class Process {
   protected:
      int memory_request;
   public:
      // Methods
};

The Project is to create a Linked List with the List having Process class objects. The add method I am having trouble with is as followed:

template <class Process>
void LinkedList<Process>::add_process(const Process& process) {
   if (_info == NULL) {
      _info = process;
   }
   else {
      LinkedList<Process>* newList = new LinkedList<Process>(_info, _next);

      if (newList == NULL); // throw error TODO
      _info = process;
      _next = newList;
   }
}

When I insert some test data in the main method and run the program, it seems to create the last two items with the last in the list connected to a null Linked List object. I believe I'm doing this correctly, but I need some help figuring out the issue of where its going wrong.

Thanks

Mark Wilcoxen
  • 69
  • 1
  • 10
  • Not really sure what your code is meant to be doing, but you're assigning `_info = process;` every time `add_process` is called, which is presumably not what you want to do. – Jonathan Potter Mar 22 '17 at 22:36
  • Excuse me for not being clear, `process` is an incoming object that I would like to add to the Linked List. Would including the test data help? – Mark Wilcoxen Mar 22 '17 at 22:38
  • 1
    I think you may have mashed your linked list and your linked list link classes into one class. This will bring pain. I suggest making two classes: `LinkedList` and `Link`. `LinkedList` contains and manages a list of `Links`. It has all of the functions that insert, remove , and otherwise modify `Link`s. `Link` contains the data, a `Process` in your case, a pointer to the next `Link` and nothing else. Generally the stupider the `Link` class is, the better off you are. – user4581301 Mar 22 '17 at 22:46
  • I was looking at some examples of Linked Lists, is what your getting at more like create a Node structure alongside the Linked List class? – Mark Wilcoxen Mar 22 '17 at 22:48
  • Precis-olutely! – user4581301 Mar 22 '17 at 22:49
  • I will definitely keep that in mind, but I'm looking over the project outline and though it doesn't explicitly say I ~cant~ do that, I'd like to see if there was a way to fix the code I currently have. Please don't take this as me brushing off your suggestion! – Mark Wilcoxen Mar 22 '17 at 22:53
  • 1
    No worries, Mark. In your current code, `if (_info == NULL)` tests whether or not `_info` is NULL, but `Process` shows no signs of whether `_info` can be NULL. Be careful with that. Next, if it is not NULL, you set up a new `LinkedList`, link to it, and then overwrite the current `LinkedList`'s `_info` instead of setting the `_info` in the new link. This just lost you a `Process`. – user4581301 Mar 22 '17 at 23:00
  • 1
    Off topic, be careful in C++ with preceding underscores. They can mean more than you think. What you have here looks safe, but it's good to know the rules before they bite you: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – user4581301 Mar 22 '17 at 23:00
  • I appreciate all of your help, but I kind of just had a faceplam moment...in my copy constructor I was setting `_next` to itself instead of the incoming LinkedList object...I about put my forehead though my desk – Mark Wilcoxen Mar 22 '17 at 23:34

0 Answers0