juli (caladri) wrote in dailysrc,
juli
caladri
dailysrc

I've talked about this before, now for a bad C++ example.

Clever?


#include <iostream>

struct Node {
        Node()
        : next_(NULL)
        {
        }
        Node *next_;
};

class TailQ {
public:
        TailQ(void)
        : head_(NULL)
        {
                init();
        }

        ~TailQ()
        {
        }

        bool
        empty(void)
        {
                return (head_ == NULL);
        }

        void
        insert(Node *node)
        {
                *tailp_ = node;
                tailp_ = &node->next_;
        }

        Node *
        pop(void)
        {
                if (empty()) {
                        return (NULL);
                }

                Node *node;

                node = head_;
                head_ = node->next_;
                init();
                return (node);
        }

protected:
        void
        init(void)
        {
                if (empty()) {
                        tailp_ = &head_;
                }
        }

private:
        Node *head_;
        Node **tailp_;
};

int
main(void)
{
        TailQ l;
        Node n, o, *p;
        l.insert(&n);
        p = l.pop();
        std::cout << "p = " << p << "; &n = " << &n << "; &o = " << &o << ";\n";
        l.insert(p);
        l.insert(&o);
        std::cout << "p = " << p << "; &n = " << &n << "; &o = " << &o << ";\n";
        p = l.pop();
        std::cout << "p = " << p << "; &n = " << &n << "; &o = " << &o << ";\n";
        p = l.pop();
        std::cout << "p = " << p << "; &n = " << &n << "; &o = " << &o << ";\n";
        p = l.pop();
        std::cout << "p = " << p << "; &n = " << &n << "; &o = " << &o << ";\n";
        return (0);
}

  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

  • 0 comments