Linked List trouble

I'm having trouble with implementing a linked list

Here is the .hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

#include <iostream>
#include "linkedList.h"
// Purpose: effectively "empties" the list
// Postconditions: current size set to 0, head and tail ptrs set to NULL,
// and all dynamically allocated memory for nodes deallocated
template <typename T>
void LinkedList<T>::clear()
{
  while(m_head != NULL)
    removeAtHead();
  m_size = 0;
}
// Purpose: determines whether x is in the list
// Parameters: x is data value to be found
// Returns: true if x is in the list otherwise, false
template <typename T>
bool LinkedList<T>::find(const T& x) const
{
  Node<T> *thisNode;
  thisNode = m_head;
  while(thisNode != NULL)
  {
    if(thisNode -> m_data == x)
      return true;
  }
  return false;
}
// Purpose: puts the data value x at the beginning of the list
// Parameters: x is data value to inserted
// Postconditions: current size of list incremented by 1, head pointer
// now points to a node with data value x, the new node's next ptr points 
// to what was previously the first in the list, and its prev ptr points
// to NULL if x is the only value in the list, tail pointer also points 
// to the new node
template <typename T>
void LinkedList<T>::insertAtHead(const T& x)
{
  m_size++;
  Node<T> *newNode = new Node<T>;
  newNode -> m_data = x;
  newNode -> m_next = m_head;
  m_head = newNode;
}

// Purpose: puts the data value x at the end of the list
// Parameters: x is data value to inserted
// Postconditions: current size of list incremented by 1, tail pointer
// now points to a node with data value x, the new node's prev ptr points 
// to what was previously the last in the list, and its next ptr points
// to NULL if x is the only value in the list, head pointer also points 
// to the new node
template <typename T>
void LinkedList<T>::insertAtTail(const T& x)
{
  m_size++;
  Node<T> *newNode;
  newNode -> m_prev = m_tail;
  newNode -> m_data = x;
  m_tail = newNode;
}

// Purpose: removes the data value at the beginning of the list
// Postconditions: current size of list decremented by 1, head pointer
// now points to whatever the removed node's next ptr was pointing at, and
// that node's prev ptr is NULL if list is now empty, tail pointer 
// is set to NULL
// Exceptions: if the list is empty, then throw std::length_error
template <typename T>
void LinkedList<T>::removeAtHead()
{
  if(m_size == 0)
    throw std::length_error("List is empty");
  m_size--;
  m_head = m_head -> m_next;
  delete m_head -> m_prev;
  if(m_size == 0)
    m_tail = NULL;
}

// Purpose: removes the data value at the end of the list
// Postconditions: current size of list decremented by 1, tail pointer
// now points to whatever the removed node's prev ptr was pointing at, and
// that node's next ptr is NULL if list is now empty, head pointer 
// is set to NULL
// Exceptions: if the list is empty, then throw std::length_error
template <typename T>
void LinkedList<T>::removeAtTail()
{
  if (m_size == 0)
  {
    throw std::length_error("List is empty");
  }
  m_size--;
  m_tail = m_tail -> m_prev;
  delete m_tail -> m_next;
  if(m_size == 0)
    m_head = NULL;
}

// Purpose: starting from the head of the list, removes the first node
// with data value x
// Parameters: x is data value whose first occurrence is to be removed
// Returns: true if an occurrence of x successfully removed otherwise,
// false
// Postconditions: if x was in the list, current size of list decremented 
// by 1, and the (formerly) first occurrence of x has been removed
template <typename T>
bool LinkedList<T>::removeFirstOccurrence(const T& x)
{
  Node<T> *newNode = m_head;
  while(newNode != NULL)
  {
    if(newNode -> m_data == x)
    {
      if(newNode != m_head)
        newNode -> m_prev -> m_next = newNode -> m_next;
      if(newNode != m_tail)
      newNode -> m_next -> m_prev = newNode -> m_prev;
      m_size--;
      delete newNode;
      return true;
    }
    newNode = newNode -> m_next;
  }
  return false;
}

// Purpose: removes all nodes with data value x
// Parameters: x is data value to be removed
// Returns: # of occurrences of x that were removed (0 if none)
// Postconditions: all occurrence of x have been removed and current size
// of list updated accordingly
template <typename T>
unsigned int LinkedList<T>::removeAllOccurrences(const T& x)
{
  int count;
  while(removeFirstOccurrence(x))
    count++;
  return count;
}

// Purpose: performs a deep copy of the data from rhs into this linked list
// Parameters: rhs is linked list to be copied
// Returns: *this
// Postconditions: this list contains same data values (in the same order)
// as are in rhs any memory previously used by this list has been
// deallocated
template <typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& rhs)
{
  if(*this == &rhs)
    return *this;
  clear();
  Node<T> *rhsNode;
  *rhsNode = rhs.getHeadPtr();
  while(rhsNode != NULL)
  {
    insertAtTail(rhsNode -> m_data);
    rhsNode = rhsNode -> m_next;
  }
  return *this;
}
// Purpose: determines whether this list is identical to rhs list in
// terms of data values and their order in the list
// Parameters: rhs is list to be compared to this list
// Returns: true if lists are identical otherwise, false
template <typename T>
bool LinkedList<T>::operator==(const LinkedList<T>& rhs) const
{
  if(m_size != rhs.size())
    return false;
  Node<T> *rhsNode = rhs.getHeadPtr();
  Node<T> *newNode = m_head;
  while(newNode != NULL)
  {
    if(newNode -> m_data != rhsNode -> m_data)
      return false;
  }
  return true;
}


And here is the header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef LINKED_LIST_H
#define LINKED_LIST_H

#include "node.h"
#include <stdexcept>
#include "linkedList.hpp"
template <class T>
class LinkedList
{
  private:
    unsigned int m_size;         // # of nodes in list
    Node<T> *m_head, *m_tail;    // ptrs to first and last nodes in list
    
  public:
    LinkedList(): m_size(0), m_head(NULL), m_tail(NULL) {}
    LinkedList(const LinkedList<T>& cpy):m_head(NULL), m_tail(NULL) { *this = cpy; }
  
    ~LinkedList() { clear(); }
    void clear();
    
    Node<T>* getHeadPtr() { return m_head; }
    Node<T>* getTailPtr() { return m_tail; }
    unsigned int size() const{ return m_size;}
    bool isEmpty() const { return m_size==0; }
  
    bool find(const T& x) const;
  
    void insertAtHead(const T& x);
    void insertAtTail(const T& x);
    void removeAtHead();
    void removeAtTail();

    bool removeFirstOccurrence(const T& x);
    unsigned int removeAllOccurrences(const T& x);
    
    LinkedList<T>& operator=(const LinkedList<T>& rhs);
    bool operator==(const LinkedList<T>& rhs) const;
};

#include "linkedList.hpp"

#endif 


The errors I'm getting are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
linkedList.hpp: In member function ‘LinkedList<T>&
LinkedList<T>::operator=(const LinkedList<T>&) [with T = int]’:
test_list.cpp:276:   instantiated from here
linkedList.hpp:159: error: no match foroperator==’ in
‘*(LinkedList<int>*)this == (const LinkedList<int>*)rhs’
linkedList.hpp:176: note: candidates are: bool
LinkedList<T>::operator==(const LinkedList<T>&) const [with T = int]
linkedList.hpp:163: error: passing ‘const LinkedList<int>’ as ‘this’
argument of ‘Node<T>* LinkedList<T>::getHeadPtr() [with T = int]’
discards qualifiers
linkedList.hpp: In member function ‘bool
LinkedList<T>::operator==(const LinkedList<T>&) const [with T = int]’:
test_list.cpp:322:   instantiated from here
linkedList.hpp:180: error: passing ‘const LinkedList<int>’ as ‘this’
argument of ‘Node<T>* LinkedList<T>::getHeadPtr() [with T = int]’
discards qualifiers



Any help would be greatly appreciated
In your header file (I think linkedList.h) you should remove the "#include "linkedList.hpp" on line 6 and line 40.

Then in your test_list.cpp you should only include "linkedList.hpp"

What you had seemed to be causing a circular reference:

you included linkedList.hpp in linkedList.h and included linkedList.h in linkedList.hpp.
Topic archived. No new replies allowed.