0

Hello everyone, I am having some trouble trying to get my circularly linked list to work. It does compile but it gives a Segmentation fault. This is my first time learning something like this and I don't really know whats wrong. If anyone could possibly point out some problems it would help a lot.

Header File:

#include <string>
    #include <iostream>
    #ifndef ARRAYLIST_H
    #define ARRAYLIST_H
        
    using namespace std;
        
    class ArrayListException
    {
      public:
        ArrayListException(const string& aMessage)
        {
          message=aMessage;
        }
        string what() const
        {
          return message;
        }
      private:
        string message;
    };
        
    template <typename T>
    class ArrayList
    {
      public:
        cout<<"liststart";
        ArrayList<T>();
        cout<<"listfinish";
        virtual ~ArrayList<T>();
        ArrayList<T>(const ArrayList<T>& rhs);
        void add(T element);
        void add(int index, T element) throw(ArrayListException);
        T& operator[](int index)const throw(ArrayListException);
        T remove(int index) throw(ArrayListException);
        long size() const;
      private:
        template <typename U> class Node;
        Node<T>* rear;
        Node<T>* head;
        long length;
    };
    

    template <class U>
    template <typename T>
    class ArrayList<U>::Node
    {
      public:
        Node(T s);
    
      private:
        T data;
        Node<T>* next;
        friend class ArrayList<U>;
    };

CPP File:

#include <iostream>
    #include <cassert>
    #include <string>
    #include "arraylist.h"

    using namespace std;
    
    template <typename T>
    ArrayList<T>::ArrayList()
    {
      head=NULL;
      rear=NULL;
      length=0;
    }
    template<typename T>
    ArrayList<T>::ArrayList(const ArrayList<T>& rhs)
    {
      int i;
      for(i=0; i<rhs.size(); i++)
      {
	T element = rhs[i];
        add(element);
      }
    }
      
      
    template <typename T>
    ArrayList<T>::~ArrayList()
    {
      Node<T>* tmp;
      while(head)
      {
        tmp=head;
        head=head->next;
        delete tmp;
      }
    }


    template<typename T>
    void ArrayList<T>::add(T element)
    {
      Node<T>* tmp;
      Node<T>* newNode=new Node<T>(element);
      if(length==0)
      {
        head = newNode;
      }
      else
      {
        rear->next=newNode;
        rear=rear->next;
      }
      length++;
    }

   
    template<typename T>
    void ArrayList<T>::add(int index, T element) throw(ArrayListException)
    {
      if(index<0||index>length)
        throw ArrayListException("Stack Exception: top() called on empty stack.");
        Node<T>* newnode=new Node<T>(element);
        this[index-1]->next=newnode;
        newnode->next=this[index]; 
    }

    
    template<typename T>
    T& ArrayList<T>::operator[](int index)const throw(ArrayListException)
    {
      if(index<0||index>length-1 )
        throw ArrayListException("Stack Exception: top() called on empty stack.");
        int i;
        Node<T>*tmp;
        tmp=head;
        for(i=0; i<index; i++)
          tmp=tmp->next;
        return tmp->data;
    }

    
    template<typename T>
    T ArrayList<T>::remove(int index) throw(ArrayListException)
    {
      Node<T>* nodePtr;
      T item;
      if(index<0||index>length-1)
        throw ArrayListException("Stack Exception: top() called on empty stack.");
        return head->data;
    }

 
    template<typename T>
    long ArrayList<T>::size() const
    {
      return length;
    } 


    template <class U>
    template <typename T>
    ArrayList<U>::Node<T>::Node(T s)
    {
      data=s;
      next=NULL;
    }

Thank You

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by MooAndStuff
0

I definitley have the same program as u...due tonight... duncan at LSU?
i had some seg fault and fixed it, it was an error with my add function accessing something it wasnt supposed to... but i cant find anything within ur code...sorry im not much help =/

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.