DoubleEndList iterator

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?

thank's
Harel
-----------------------------------------------------------------
-----------------------------------------------------------------

list.h

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
//------------------------------------------------
//  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)
			return true;
	// not found
	return false;
}

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;
} 


DoubleEndList.h
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
//--------------------------------------------
// class DoubleEndedList
// a variation on Lists - can add elements 
// to the end as well as to front
//--------------------------------------------
#include "list.h"
#include <iostream>

using namespace 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
	friend class 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);
	}


my main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "DoubleEndList.h"
#include "list.h"
#include "iostream"

using namespace std;

void main()
{




system("pause");
}


Last edited on
Topic archived. No new replies allowed.