简体   繁体   English

如何更改此插入函数以使用字符串在链表中工作?

[英]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. 这是我要在StackOverflow上发布的第一个问题,请原谅我似乎有点断断续续。

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. 我实际上在本周早些时候在StackOverflow上发现了一个插入函数,但是将其设置为使用主文件内部的结构,而不是使用此项目所使用的单独的类文件。 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 LinkedList.h成员数据

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; 现在唯一的错误是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. 话虽这么说,您实际上应该使用标准的std::list容器,而不是手动实现双链表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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