C++ Lecture 8

[Previous Lecture] [Lecture Index]

L.h

#ifndef LIST_H
# define LIST_H

class List {
    struct ListLink {
        ListLink(int v) : val(v), next(0) { }
        int val;
        ListLink *next;
    };

public:
    typedef struct ListLink *Iterator;

    List();
    List(const List &ic);
    ~List();
    Iterator getIterator() const;
    bool getNext(Iterator &iter, int &val)
        const;
    void add(int val);
    bool remove(const int &val);
    void removeAll();

private:
    ListLink *head;
    ListLink *tail;
};

#endif /* LIST_H */

L.cc


// ...

bool
List::getNext(Iterator &iter, int &val) const {
    if (!iter)
        return false;
    val = iter->val;
    iter = iter->next;
    return true;
}


void
List::add(int val)
{
    ListLink *n = new ListLink(val);
    if (tail)
        tail->next = n;
    else
        head = tail = n;
    tail = n;
}

Lt.h

#ifndef LIST_H
# define LIST_H

template <class T>
class List {
    struct ListLink {
        ListLink(T v) : val(v), next(0) { }
        T val;
        ListLink *next;
    };

public:
    typedef struct ListLink *Iterator;

    List();
    List(const List &ic);
    ~List();
    Iterator getIterator() const;
    bool getNext(Iterator &iter, T &val)
        const;
    void add(T val);
    bool remove(const T &val);
    void removeAll();

private:
    ListLink *head;
    ListLink *tail;
};

#endif /* LIST_H */

Lt.h (continued)


// ...

template <class T>
bool
List<T>::getNext(Iterator &iter, T &val) const
{
    if (!iter)
        return false;
    val = iter->val;
    iter = iter->next;
    return true;
}


template <class T>
void
List<T>::add(T val)
{
    ListLink *n = new ListLink(val);
    if (tail)
        tail->next = n;
    else
        head = tail = n;
    tail = n;
}

Streams and Formatting: methods

the following effect all subsequent output

Streams and Formatting: manipulators

`manipulators' allow formating in a << sequence
(a bit more convenient)
[Previous Lecture] [Lecture Index]