Daily Archive for August 18th, 2009

Simple C++ List Class

Just as I published some days ago the Simple C++ String Class as a C++ learning exercise, now I am freeing a Simple C++ List Class.

The standard library has a list class. But, while learning, it’s a good idea to know how to develop your own list class.

That’s why I made the List class. It’s not intended for professional projects (for them, you should use the standard library’s list), but as help to learn C++.

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
template <class TYPE>
class List
{
    /* ... */
 
public:
    //Construction and destruction
    List() { /* ... */ }
    ~List() { /* ... */ }
     
    List(const List& rlList) { /* ... */ }
     
    //Assignment operator
    List& operator=(const List& rlList);
 
    //Information
    int Length() { /* ... */ }
    bool Empty() { /* ... */ }
 
    //Element managing
    int Add(TYPE& rtData);
    TYPE* Elem(int nPos);
    bool Delete(int nPos);
    void DeleteAll();
     
    //Search
    int Find(TYPE& rItem, int nStartAt = 0);
 
    //Operadores
    TYPE& operator[](int nPos) { /* ... */ }    //Elem
    int operator<<(TYPE& rdData) { /* ... */ }    //Add
 
protected:
    void FreeList();
    void Init() { /* ... */ }
};
 
//Output
template <class TYPE>
std::ostream& operator<<(std::ostream& oStream, List<TYPE>& rlList);

Keep reading…

Incoming search terms for the article: