简体   繁体   中英

How can I change this insert function to work in a linked list using strings?

This is the first question that I'm posting on StackOverflow, so forgive me if it seems kinda choppy.

For my computer science class, we're working with a double linked list for the current assignment. One of the functions required is an insert function.

I actually found an insert function on StackOverflow earlier this week, but it was set up to use structures inside of the main file instead of separate class files like this project is using. I think the function can work, but I'm not sure what alterations I need to make so that it can work with class files instead.

LinkedList.h member data

private:
    Node *head, *tail;
    mutable Node *it;
    int count;

Insert function

bool LinkedList::insert(const string & str) const
{
    LinkedList * tempVar;
    if (hasMore) {
        resetIterator();
    }
    else {
        Node * temp = new Node;
        //temp = str;
        temp->data = str;
        temp->next = it;
        temp->prev = nullptr;
        it->prev = temp;
        it = temp;
    }

    if (it != nullptr) {
        Node * current = it;
        Node * previous = nullptr;
        Node * tempNode = nullptr;

        while (current->next != nullptr) {
            tempNode = current->next;

            if (current->data > tempNode->data) {
                swap(current->data, tempNode->data);
            }
            else {
                previous = current;
                current = current->next;
            }
        }
        tempVar->count += 1;
    }
    return false;
}

I haven't been able to test it yet due to not knowing what alterations are needed, but the function should insert strings that are passed into the argument into the linked list, as well as sort them in a dictionary style. Right now the only error is that temp = str; not working, and I'm not entirely sure what I need to do to get it to work.

Try something more like this:

bool LinkedList::insert(const string & str)
{
    Node * current = head;

    while ((current) && (current->data < str))
        current = current->next;

    Node *newNode = new Node;
    newNode->data = str;
    newNode->next = nullptr;
    newNode->prev = nullptr;

    if (current)
    {
        if (current->previous)
        {
            current->previous->next = newNode;
            newNode->previous = current->previous;
        }

        current->previous = newNode;
        newNode->next = current;

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

        if (tail)
        {
            tail->next = newNode;
            newNode->previous = tail;
        }
        tail = newNode;
    }

    count += 1;
    return true;
}

That being said, you really should be using the standard std::list container instead of implementing a double-linked list manually.

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