0
Follow
4
View

error in remove method for singly linked list

dzdyfj123 注册会员
6 days ago

on line 109 i needed to decrement size.

bool remove(int x)
    {
        if(contains(x)){
            Node<Object> *temp = head;
            Node *prev = NULL;

            if(temp != NULL && temp ->data == x){
                head = temp->next;
                delete temp;
                return 0;
            }else{
                while(temp != NULL && temp->data != x){
                    prev = temp;
                    temp = temp->next;
                }
                if(temp ==NULL){
                    return 0;

                }
                prev->next = temp->next;
                delete temp;
                theSize--;
            }
            return true;
            //theSize--;
        }
        return false;
    }
bljd13 注册会员
6 days ago

Answer after second update:

had to fix the remove()

#include 
using namespace std;

template Object>
struct Node
{
    Object data;
    Node* next;
    Node(const Object &d = Object(), Node *n = (Object)NULL) : data(d), next(n) {}
};

template 
class singleList
{
public:
    singleList() { init(); }
    ~singleList() { eraseList(head); }
    singleList(const singleList &rhs)
    {
        eraseList(head);
        init();
        *this = rhs;
        print();
        contains(head);
    }

    void init()
    {
        theSize = 0;
        head = new Node;
        head->next = (Object)NULL;
    }

    void eraseList(Node *h)
    {
        Node *ptr = h;
        Node *nextPtr;
        while (ptr != (Object)NULL)
        {
            nextPtr = ptr->next;
            delete ptr;
            ptr = nextPtr;
        }
    }

    int size()
    {
        return theSize;
    }

    void print()
    {
        int i;
        Node *current = head;
        for(i=0; i < theSize; ++i){
            cout << current->data << " ";
            current = current->next;
        }
        
    }   

    bool contains(int x)
    {
        Node *current = head;
        for (int i = 0; i < theSize; ++i){
            if (current->data == x){
                return true;
            }
            current = current -> next;
        }
        return false;

    }

    bool add(Object x){
        if(!contains(x)){
            Node *new_node = new Node(x);
            new_node->data = x;
            new_node->next = head;
            head = new_node;
            //head->next = new_node;
            theSize++;
            return true;
        }
        return false;
    }

    bool remove(int x){
        Node *pCur  = head;
        Node *pPrev = pCur;

        while (pCur && pCur->data != x) {
            pPrev = pCur;
            pCur  = pCur->next;
        }

        if (pCur==nullptr)  // not found
            return false;

        if (pCur == head) {   // first element matches
            head = pCur->next;
        } else {
            pPrev->next = pCur->next;
        }

        // pCur now is excluded from the list

        delete pCur; 
        theSize--;
        return true;
    }

private:
    Node *head;
    int theSize;
};

 int main()
 {
     singleList *lst = new singleList();
     lst->add(10);
     lst->add(12);
     lst->add(15);
     lst->add(6);
     lst->add(3);
     lst->add(8);
     lst->add(3);
     lst->add(18);
     lst->add(5);
     lst->add(15);

     cout << "The original linked list: ";
     lst->print();
     cout << endl;
     lst->remove(6);
     lst->remove(15);
     cout << "The updated linked list: ";
     lst->print();
     cout << endl;
     cout << "The number of node in the list: " << lst->size() << endl;

     return 0;
 }

About the Author

Question Info

Publish Time
6 days ago
Update Time
6 days ago