I have a template singly linked list function that has a node class:

template <class T>

class Node {

      T Data;
      Node <T> *Next;
      
      public:
             
      Node();
      Node (const T&);
      T getData() const;
      Node <T>* getNext() const;
      void setData(T);
      void setNext(Node<T>*);
};

and SLL:

class SinglyLinkedList {

      Node <T>* Head;
   
   /* Recursive "helper" functions for Search, Destroy & Delete. */
   
      Node<T> Search(Node <T>*, const T&) const;
      Node <T>* deleteNodeR (Node<T>*, const T&);
      void destroy();
      
      public:
 
             SinglyLinkedList();
             ~SinglyLinkedList();
             void addToHead(const T&);
             bool isEmpty() const;
             void deleteNode(const T);
             Node <T> isInList(T) const;
             Node <T>* getHead() const;
             void setHead(Node<T>*);
};

template <class T>
SinglyLinkedList<T>::~SinglyLinkedList() {
destroy();
Head=NULL;
}

template <class T>
SinglyLinkedList<T>::SinglyLinkedList() {
Head = NULL;
}

template <class T>
bool SinglyLinkedList<T>::isEmpty() const {
     return (Head == NULL);
}

template <class T>
void SinglyLinkedList<T>::addToHead(const T &t) {
     
     // Create a new node and initialize it with t;

           Node<T> *P = new Node<T>(t);     
     
           P->setNext(Head);
           Head = P;

}

template <class T>
Node <T> SinglyLinkedList<T>::isInList(T t) const {
     return Search(Head, t);
}

template <class T>
Node <T> SinglyLinkedList<T>::Search(Node <T>* pointer, const T& t) const {

     // If we've reached the end of the list and we did not find anything, return an empty node.

     if (pointer==NULL) {
                        Node<T> *P=new Node<T>(); 
                        return *P;
                        }
     
     // If we found a match, return it.

     if (pointer->getData()==t)
     return *pointer;

     // Otherwise, keep searching (recursively).

     return Search (pointer->getNext(), t);
}
     
template <class T>
Node <T>* SinglyLinkedList<T>::getHead() const {
     return Head;
}

template <class T>
void SinglyLinkedList<T>::deleteNode (const T t) {

     // If the list is not empty, call the recursive function.

     if (!isEmpty()) {
                     Head=deleteNodeR (Head, t);
                     }
}

template <class T>
Node <T>* SinglyLinkedList<T>::deleteNodeR (Node<T>* pointer, const T& t) {

     // If we've reached the end of the list without any results, return a null pointer.     

     if (pointer == NULL)
     return NULL;
     
     // If we found our match, save the next pointer and delete the node.

     if (pointer->getData()==t) {
                           Node<T> *tempNextP;
                           tempNextP=pointer->getNext();
                           delete pointer;
                           return tempNextP;
                           }

     // Check the rest of the list.

     pointer->setNext(deleteNodeR(pointer->getNext(), t));

     // Return the pointer back to where it was called from.

return pointer;
} 

template <class T>
void SinglyLinkedList<T>::setHead(Node<T>* head) {
     Head=head;
}

I have a student class that aside from student info (name, ssn, etc) stores courses taken by the student in a singlylinkedlist member:

SinglyLinkedList<CourseInfo> courseInfoList;

The program works fine if I do one student and I store courses for the student. However, I am required to have a different class (Registration system) that has a list of Students.

SinglyLinkedList <Student> studentlist;

So in other words, a singly linked list which stores student objects that have their own Singlylinkedlists that use the same generic SLL class.

When I do an addtohead of a Student object, I get a segmentation error. I've tried anything possible and I cannot figure this out. :(

I would appreciate any help on this. I have spent hours and hours trying to figure it out....

Can you give us a sample main() to illustrate the problem clearly?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.