简体   繁体   中英

assignment operator for linked list

#include <iostream>

using namespace std;

class LinkedList
{
private: 

    struct ListNode
    {
    int value;
    struct ListNode *next;
    };

ListNode *head;

public:
    LinkedList()
    {
        head = NULL;
    }
    ~LinkedList();

    const LinkedList& operator=(const LinkedList &rhs);

    void appendNode(int);
    void insertNode(int);
    void deleteNode(int);

    ListNode *removeLast();
};

void LinkedList::appendNode(int num)
{
    ListNode *newNode;
    ListNode *nodePtr;

    newNode = new ListNode;
    newNode->value = num;
    newNode->next = NULL;

    if (!head)
    {
        head = newNode;
    }
    else
    {
        nodePtr = head;

        while (nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }

        nodePtr->next = newNode;
    }
}

void LinkedList::insertNode(int num)
{
    ListNode *newNode;
    ListNode *nodePtr;
    ListNode *preNode;

    newNode = new ListNode;
    newNode->value = num;

    if (!head)
    {
        head = newNode;
        newNode->next = NULL;
    }
    else
    {
        nodePtr = head;
        preNode = NULL;

        while (nodePtr->next)
        {
            preNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        if (!preNode) // if new node is to be the 1st in the list
        {
            head = newNode;
            newNode->next = nodePtr;
        }

        else
        {
            preNode->next = newNode;
            newNode->next = nodePtr;
        }

    }
}

void LinkedList::deleteNode(int num)
{
    ListNode *nodePtr;
    ListNode *preNode;

    if (!head)
        return;

    if (head->value == num)
    {
        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }

    else
    {
        nodePtr = head;

        while (nodePtr != NULL && nodePtr->value != num)
        {
            preNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        if (nodePtr)
        {
            preNode->next = nodePtr->next;
            delete nodePtr;
        }
    }
}

const LinkedList& LinkedList::operator=(const LinkedList &rhs)
{
    if (this != &rhs)
    {
        ListNode *curr, *nextNode;

        curr = head;
        nextNode = NULL;

        while (curr)
        {
            nextNode = curr->next;
            delete curr;
            curr = nextNode;
        }

        ListNode *cptr = new ListNode;
        cptr = rhs.head;
        cptr->next = NULL;

        temp = rhs.head->next;
        while (temp)
            {
                cptr->next = new ListNode;
                cptr->next = temp;
                cptr = cptr->next;
                cptr->next = NULL;

                temp = rhs.head->next;
            }
        }

    return *this;
}

LinkedList::ListNode* LinkedList::removeLast()
{
    ListNode *nodePtr, *preNode;

    if (!head)
    {
        return;
    }

    nodePtr = head;
    preNode = NULL;

    while (nodePtr->next)
    {
        preNode = nodePtr;
        nodePtr = nodePtr->next;
    }

    preNode->next = NULL;

    return nodePtr;
}

LinkedList::~LinkedList()
{
    ListNode *nodePtr;
    ListNode *nextNode;

    nodePtr = head;

    while (nodePtr)
    {
        nextNode = nodePtr->next;
        delete nodePtr;
        nodePtr = nextNode;
    }
}

Thank you for those who helped me out!

I fixed my assignment operator and my removeLast function.

For removeLast, 2nd last node will point null, then it returns very last node

I am not sure if my assignment operator is fine or not.

as well as removeLast function..

If you guys find the error, then let me know!

thank you again!

Also, can you guys teach me how to declare "ListNode *removeLast();"?

LinkedList::ListNode* LinkedList::removeLast()

You just had the return type and class crossed over, but ListNode is also declared inside LinkedList, so you have to specify it as "LinkedList::ListNode" whenever you use it.

As far as I can tell, your destructor looks find. You're just setting one pointer equal to another pointer, that doesn't trigger the copy constructor.

As for creating a deep copy, pointers just hold memory addresses, they aren't the thing themselves.

ListNode *node = head;

Just creates a pointer referring to the head node. To make a copy, you have to do something like this:

ListNode node = *head; // sometimes brackets such as (*head) avoids errors

or this

ListNode *node = new ListNode;
*node = *head;

.

* 

before a pointer's name is "de-referencing". It is a reference to the object pointed at instead of the pointer variable. You can then use the normal copy semantics. For very simple classes (ie all simple objects), the default copy behavior is sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM