Developing a non-STL list
Aug 9, 2017 at 5:52am UTC
Hi,
I'm going to using the following code create my own List:
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
#include <std_lib_facilities_4.h>
using namespace std;
//***************************************
template <class Elem>
class Mylist {
class Link {
public :
Link* prev;
Link* succ;
Elem val;
};
iterator first;
iterator last;
public :
class iterator;
iterator begin(); // iterator to the first element
iterator end(); // iterator to one beyond the last element
iterator insert(iterator p, const Elem& v); // insert v into the list after p
iterator erase(iterator p); // remove p from the list
void puch_back(const Elem& v); // insert v at end
void push_front(const Elem& v); // insert v at front
void pop_front(); // remove the first element
void pop_back(); // remove the last element
Elem& front(); // the first element
Elem& back(); // the last element
};
//*****************************
int main()
{
system("pause" );
return 0;
}
//*****************************************
template <class Elem>
class Mylist<Elem>::iterator {
Link<Elem>* curr; // Here in this line (the source of errors)
public :
iterator(Link* p) : curr(p) { }
iterator& operator ++() { curr = curr->succ; return *this ; } // forward
iterator& operator --() { curr = curr->prev; return *this ; } // backward
Elem& operator *() { return curr->val; } // get value (dereference)
bool operator ==(const iterator& b) const { return curr == b.curr; }
bool operator !=(const iterator& b) const { return curr != b.curr; }
};
//****************************************************************************
template <class Elem>
typename Mylist<Elem>::iterator Mylist<Elem>::begin()
{
return first;
}
//************************************
template <class Elem>
typename Mylist<Elem>::iterator Mylist<Elem>::end()
{
return last + 1;
}
//*************************************************************
template <class Elem>
typename Mylist<Elem>::iterator Mylist<Elem>::insert(iterator p, const Elem & v)
{
return iterator();
}
//************************************************
template <class Elem>
typename Mylist<Elem>::iterator Mylist<Elem>::erase(iterator p)
{
return iterator();
}
//*****************************
template <class Elem>
void Mylist<Elem>::puch_back(const Elem & v)
{
}
//**************************************************
template <class Elem>
void Mylist<Elem>::push_front(const Elem & v)
{
}
//******************************
template <class Elem>
void Mylist<Elem>::pop_front()
{
}
//*********************************
template <class Elem>
void Mylist<Elem>::pop_back()
{
}
//************************************
template <class Elem>
Elem & Mylist<Elem>::front()
{
// TODO: insert return statement here
}
//********************************
template <class Elem>
Elem & Mylist<Elem>::back()
{
// TODO: insert return statement here
}
I get these errors:
Severity Code Description Project File Line Suppression State
Error C2059 syntax error: '<'
Error C2238 unexpected token(s) preceding ';'
for the line shown above (51).
What is the problem please?
Last edited on Aug 9, 2017 at 6:09am UTC
Aug 9, 2017 at 7:43am UTC
Another compiler says:
48:2: error: 'Mylist<Elem>::Link' is not a template
Aug 9, 2017 at 8:31am UTC
Where is the definition of iterator?
Aug 9, 2017 at 8:38am UTC
But we don't have such a construct!
Aug 9, 2017 at 9:02am UTC
If you use it, you'll need to create one.
Aug 9, 2017 at 10:23am UTC
Is this an exercise from this infamous book "Programming: Principles and Practice Using C++"
If yes it would usefult to post the complete exercise.
Aug 9, 2017 at 3:04pm UTC
You must declare class iterator inside Mylist. On my compiler, I also had to move the declarations of first and last to after the declaration of iterator.
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
#include <std_lib_facilities_4.h>
using namespace std;
//***************************************
template <class Elem>
class Mylist {
class Link {
public :
Link* prev;
Link* succ;
Elem val;
};
public :
class iterator {
Link* curr; // Here in this line (the source of errors)
public :
iterator(Link* p) : curr(p) { }
iterator& operator ++() { curr = curr->succ; return *this ; } // forward
iterator& operator --() { curr = curr->prev; return *this ; } // backward
Elem& operator *() { return curr->val; } // get value (dereference)
bool operator ==(const iterator& b) const { return curr == b.curr; }
bool operator !=(const iterator& b) const { return curr != b.curr; }
};
iterator begin(); // iterator to the first element
iterator end(); // iterator to one beyond the last element
iterator insert(iterator p, const Elem& v); // insert v into the list after p
iterator erase(iterator p); // remove p from the list
void puch_back(const Elem& v); // insert v at end
void push_front(const Elem& v); // insert v at front
void pop_front(); // remove the first element
void pop_back(); // remove the last element
Elem& front(); // the first element
Elem& back(); // the last element
protected :
iterator first;
iterator last;
};
Aug 9, 2017 at 4:31pm UTC
Why did you declare first
and last
within protected
scope and not private
?
Last edited on Aug 9, 2017 at 4:31pm UTC
Topic archived. No new replies allowed.