c++ linked list deduplication, reverse question.

Asked 2 years ago, Updated 2 years ago, 110 views

Source to clear if a simple link list has duplicate data. IsEmpty() is a boolean function that asks if the list is zero Length() is a function that checks the length of the list. The Delete (int Position) function is a function that removes a list of specified positions (position integers). All of these functions work fine The deduplication function deDuplicate() is problematic... I don't know why it doesn't work, but I keep getting errors and asking questions.

void listClass::deDuplicate()
{
    if (IsEmpty() || Length()==1)
        cout << "not enough list" << endl;
    else
    {
        Nptr temp = Head;
        Nptr temp2 = Head;
        temp2 = temp2->Next;
        for (int i = 0; i < Count; i++)
        {
            temp = temp->Next;
            for (int j = 0; j < Count-1; j++)
            {
                temp2 = temp2->Next;
                if (temp->Data == temp2->Data) {
                    Delete(j+2);
                    Count -= 1;
                }
            }
        }
    }
}

It's a class.

typedef struct nodeRecord
{
    int Data;
    struct nodeRecord* Next;
}node;

typedef node* Nptr;

class listClass
{
public:
    listClass();
    listClass(const listClass& L);
    ~listClass();
    void Insert(int Position, int Item);
    void Delete(int Position);
    void Retrieve(int Position, int *ItemPtr);
    void Init();
    bool IsEmpty();
    int Length();
    void printlist();
    void deDuplicate();
    //void reverse();
private:
    int Count;
    Nptr Head;
}; 

The reverse function is a function that links sorted lists in reverse order... I don't know how to start and end the connection If you can help me with this, please.

c++ linked-list deduplicate reverse

2022-09-21 22:08

1 Answers

I think it's a segmentation fault.

Incorrect number of loops in for statements (Note 1, Note 3)

Temp2 is not initializing after the second loop statement. (Note 2)

else
{
    Nptr temp = Head;
    Nptr temp2 = Head;
    for (inti = 0; i < Count-1; i++)//Note1
    {
        temp2 = temp;//note 2
        for (int j = 0; j < Count-i-1; j++)//Note3
        {
            temp2 = temp2->Next;
            if (temp->Data == temp2->Data) 
            {
                Delete(j+2);
                Count -= 1;
            }
        }
        temp = temp->Next;
    }
}

If you don't understand anything, please leave a comment.

For reverse, you can put the address of the previous node in the next of the pointer pointing to the current node and change the value of the pointer to the next node. Here, you cannot access the previous node from the current node, and if you put the address of the previous node in the next of the current node, you cannot access the next node. So you need multiple pointers.

Lastly, I personally think it was easier to code on the Linked List by determining the end of the Linked List whether the next is null or not instead of counting as shown below.

while(temp->next != Null)
    temp=temp->next;


2022-09-21 22:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.