Hi, in the header file: DoubleEndList.h line 44;46;48 :
When I move the mouse on "p" then apeared: <unknown> List<T>::p
And when I pass the mouse on "next" then apeared:<unknown> <unnamed> ::next
What is the motivation problem?
And what should I change the software code to better identify the p and the next?
//------------------------------------------------
// class List
// arbitrary size Lists
// permits insertion and removal
// only from the front of the List
//------------------------------------------------
#define NULL 0
template<class T>
class List
{
protected:
//--------------------------------------------
// inner class link
// a single element for the linked List
//--------------------------------------------
class Link
{
public:
// constructor
Link(){}
Link( T linkValue, Link * nextPtr);
Link (const Link &);
// data areas
T value;
Link* next;
}; //end of class Link
public:
// constructors
List();
List(const List<T>&);
~List();
// operations
void add( T value);
T firstElement() const;
int includes(const T &value) const;
bool isEmpty() const;
void removeFirst();
void clear();
protected:
// data field
Link* first;
};
//------------------------------------------------
// class Link implementation
//------------------------------------------------
template<class T>
List<T>::Link::Link( T val, Link* nxt)
{
value=val;
next=nxt;
}
template<class T>
List<T>::Link::Link(const Link& source) :
value(source.value),next(source.next) {}
//--------------------------------------------
// class List implementation
//--------------------------------------------
template<class T>
List<T>::List(): first(NULL)
{
// no further initialization
}
template<class T>
List<T>::List(const List<T> &l)
{
Link<T> *src, *trg;
if(l.first==NULL)
first=NULL;
else
{
first= new Link<T>((l.first)->value, NULL);
src=l.first;
trg=first;
while(src->next!=NULL)
{
trg->next= new Link<T>((src->next)->value, NULL);
src=src->next;
trg=trg->next;
}
}
}
template<class T>
List<T>::~List()
{
clear();
}
template<class T>
void List<T>::clear()
{
// empty all elements from the List
Link* next;
for (Link* p=first; p != NULL; p=next)
{
// delete the element pointed to by p
next=p->next;
p->next=NULL;
delete p;
}
// mark that the List contains no elements
first=NULL;
}
template<class T>
bool List<T>::isEmpty() const
{
// test to see if the List is empty
// List is empty if the pointer to the first
// Link is null
return first == NULL;
}
template<class T>
void List<T>::add( T val)
{
//Add a new value to the front of a Linked List
first=new Link(val, first);
if(first==NULL)
throw"failed in memory allocation";
}
template<class T>
T List<T>::firstElement() const
{
// return first value in List
if (isEmpty())
throw"the List is empty, no first Element";
return first->value;
}
template<class T>
int List<T>::includes(const T &val) const
{
// loop to test each element
for (Link* p=first; p!=NULL ; p=p->next)
if (val == p->value)
returntrue;
// not found
returnfalse;
}
template<class T>
void List<T>::removeFirst()
{
// make sure there is a first element
if(isEmpty())
throw"the List is empty, no Elements to move";
// save pointer to the removed node
Link* p=first;
// reassign the first node
first= p->next;
p->next = NULL;
// recover memory used by the first element
delete p;
}
//--------------------------------------------
// class DoubleEndedList
// a variation on Lists - can add elements
// to the end as well as to front
//--------------------------------------------
#include "list.h"
#include <iostream>
usingnamespace std;
template<class T>
class DoubleEndedList : public List<T>
{
public:
// constructors
DoubleEndedList ();
// override the following methods from class List
void add(T value);
void clear();
void removeFirst();
// add a new element to the end of the List
void addToEnd(T value);
//<---------iterator begin---------->
// Declaration required
class iterator;
// Make it a friend
friendclass iterator;
// The define of the iterator
class iterator
{
DoubleEndedList& v;
int index;
public:
//constructor 1 (begin)
iterator(DoubleEndedList& vt): v(vt),index(0){}
iterator(DoubleEndedList&v1,bool a): v(v1) , index (0)
{
v=v1;
List<T>::Link *p = v1.first; //<---------------------------------
while(p->next)
{
p=p->next;
index++;
}
}
};
//<---------iterator end---------->
iterator begin()
{
return iterator(*this);
}
// Create the "end sentinel":
iterator end()
{
return iterator(*this, true);
}
protected:
// data area -- Link to end
Link* last;
};
//------------------------------------------------
// class DoubleEndedList implementation
//------------------------------------------------
template<class T>
DoubleEndedList<T>::DoubleEndedList() :List(), last(NULL)
{}
template<class T>
void DoubleEndedList<T>::add( T val)
{
// add an element to the front of a double
// ended List only need to handle addition to
// empty List
if (isEmpty()) {
List<T>::add(val);
last=first;
}
else
List<T>::add(val);
}
template<class T>
void DoubleEndedList<T>::clear()
{
// delete all values from collection
List<T> ::clear();
// then set the pointer to the last element to z
last = NULL;
}
template<class T>
void DoubleEndedList<T> ::removeFirst(){
// remove the first element
List<T> ::removeFirst();
// if we remove last element
if (isEmpty())
last = NULL;
}
template<class T>
void DoubleEndedList<T>::addToEnd( T val)
{
// add a new element to end of a double ended List
if (last != NULL)
{
last->next = new Link (val,NULL);
last = last->next;
}
// otherwise, just add to front
else
add(val);
}