944,110 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 483
  • C++ RSS
Nov 10th, 2009
0

Error With Circularly Linked List

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <iostream>
  3. #ifndef ARRAYLIST_H
  4. #define ARRAYLIST_H
  5.  
  6. using namespace std;
  7.  
  8. class ArrayListException
  9. {
  10. public:
  11. ArrayListException(const string& aMessage)
  12. {
  13. message=aMessage;
  14. }
  15. string what() const
  16. {
  17. return message;
  18. }
  19. private:
  20. string message;
  21. };
  22.  
  23. template <typename T>
  24. class ArrayList
  25. {
  26. public:
  27. cout<<"liststart";
  28. ArrayList<T>();
  29. cout<<"listfinish";
  30. virtual ~ArrayList<T>();
  31. ArrayList<T>(const ArrayList<T>& rhs);
  32. void add(T element);
  33. void add(int index, T element) throw(ArrayListException);
  34. T& operator[](int index)const throw(ArrayListException);
  35. T remove(int index) throw(ArrayListException);
  36. long size() const;
  37. private:
  38. template <typename U> class Node;
  39. Node<T>* rear;
  40. Node<T>* head;
  41. long length;
  42. };
  43.  
  44.  
  45. template <class U>
  46. template <typename T>
  47. class ArrayList<U>::Node
  48. {
  49. public:
  50. Node(T s);
  51.  
  52. private:
  53. T data;
  54. Node<T>* next;
  55. friend class ArrayList<U>;
  56. };

CPP File:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cassert>
  3. #include <string>
  4. #include "arraylist.h"
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9. ArrayList<T>::ArrayList()
  10. {
  11. head=NULL;
  12. rear=NULL;
  13. length=0;
  14. }
  15. template<typename T>
  16. ArrayList<T>::ArrayList(const ArrayList<T>& rhs)
  17. {
  18. int i;
  19. for(i=0; i<rhs.size(); i++)
  20. {
  21. T element = rhs[i];
  22. add(element);
  23. }
  24. }
  25.  
  26.  
  27. template <typename T>
  28. ArrayList<T>::~ArrayList()
  29. {
  30. Node<T>* tmp;
  31. while(head)
  32. {
  33. tmp=head;
  34. head=head->next;
  35. delete tmp;
  36. }
  37. }
  38.  
  39.  
  40. template<typename T>
  41. void ArrayList<T>::add(T element)
  42. {
  43. Node<T>* tmp;
  44. Node<T>* newNode=new Node<T>(element);
  45. if(length==0)
  46. {
  47. head = newNode;
  48. }
  49. else
  50. {
  51. rear->next=newNode;
  52. rear=rear->next;
  53. }
  54. length++;
  55. }
  56.  
  57.  
  58. template<typename T>
  59. void ArrayList<T>::add(int index, T element) throw(ArrayListException)
  60. {
  61. if(index<0||index>length)
  62. throw ArrayListException("Stack Exception: top() called on empty stack.");
  63. Node<T>* newnode=new Node<T>(element);
  64. this[index-1]->next=newnode;
  65. newnode->next=this[index];
  66. }
  67.  
  68.  
  69. template<typename T>
  70. T& ArrayList<T>::operator[](int index)const throw(ArrayListException)
  71. {
  72. if(index<0||index>length-1 )
  73. throw ArrayListException("Stack Exception: top() called on empty stack.");
  74. int i;
  75. Node<T>*tmp;
  76. tmp=head;
  77. for(i=0; i<index; i++)
  78. tmp=tmp->next;
  79. return tmp->data;
  80. }
  81.  
  82.  
  83. template<typename T>
  84. T ArrayList<T>::remove(int index) throw(ArrayListException)
  85. {
  86. Node<T>* nodePtr;
  87. T item;
  88. if(index<0||index>length-1)
  89. throw ArrayListException("Stack Exception: top() called on empty stack.");
  90. return head->data;
  91. }
  92.  
  93.  
  94. template<typename T>
  95. long ArrayList<T>::size() const
  96. {
  97. return length;
  98. }
  99.  
  100.  
  101. template <class U>
  102. template <typename T>
  103. ArrayList<U>::Node<T>::Node(T s)
  104. {
  105. data=s;
  106. next=NULL;
  107. }

Thank You
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jehutiy is offline Offline
2 posts
since Nov 2009
Nov 10th, 2009
0
Re: Error With Circularly Linked List
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 =/
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MooAndStuff is offline Offline
9 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Copy constructor- "Bus Error"
Next Thread in C++ Forum Timeline: C++ IsDebuggerPresent Help?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC