Copying a List

This is what I came up with, as one of the member functions of a list class:

class chList {
        chNode *head;
        int count;
        void deleteList(void);
        void initList(void);
        void copyList(const chList &list);

        chList(const char &data);
        chList(const chList &list);
        chList &operator=(const chList &list);
        int addToList(const char &data);
        int addToTail(const char &data);
        struct chNode *deleteFromList(const char &data);
        void printList(void);
        bool isEmpty(void);

void chList::copyList(const chList &list)
        int i = 1;
        struct chNode *tmp1, *tmp2, *tmp3;

        if (!list.count)

        tmp2 = list.head;
        while (i <= list.count) {
                tmp1 = new chNode(tmp2->data);
                if (!head) {
                        head = tmp1;
                        tmp3 = head;
                else {
                        tmp3->next = tmp1;
                        tmp3 = tmp1;

                tmp2 = tmp2->next;

It so happens that if we choose to maintain a count variable, things get a little bit easier.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s