从双向链表

时间:2017-04-19 21:21:09

标签: c++ linked-list doubly-linked-list

int StringList::remove(string value)
{
if ((head == NULL) || (value > tail->data) || (value < head->data))
{
    return false;
}

if ((head == tail) && (head->data == value))
{
    delete head;
    head = tail = NULL;
    return true;
}

if ((head->data) == value)
{
    head = head->next;
    delete head->previous;
    head->previous = NULL;
    return true;
}

if ((tail->data) == value)
{
    tail = tail->previous;
    delete tail->next;
    tail->next = NULL;
    return true;
}

Node *nodeToDelete = head;

while ((nodeToDelete->data) < value)
{
    nodeToDelete = nodeToDelete->next;
}

if ((nodeToDelete->data) == value)
{
    nodeToDelete->previous->next = nodeToDelete->next;
    nodeToDelete->next->previous = nodeToDelete->previous;
    delete nodeToDelete;
    return true;
}
else
{
    return false;
}
}

我在这里有一个双向链表,我试图删除所有出现的给定字符串。目前该功能只会删除一个事件,这很好但不太有效。我相信我需要以某种方式实现一个计数器变量,它将计算已删除给定字符串的次数,但我不太清楚如何去做。

完整代码:https://pastebin.com/3bRfJzJT

2 个答案:

答案 0 :(得分:0)

from sqlalchemy import create_engine
import pandas as pd 
engine = create_engine('oracle://myusername:mypassword@SID')
con = engine.connect()
outpt = con.execute("SELECT * FROM YOUR_TABLE")
df = pd.DataFrame(outpt.fetchall())
df.columns = outpt.keys()
print(df.head())
con.close() 

答案 1 :(得分:0)

int StringList::remove(string value)
{
    int numDeleted = 0;

    Node *node = head;
    while (node)
    {
        Node *nextNode = node->next;

        if (node->data == value)
        {
            if (head == node)
                head = head->next;

            if (tail == node)
                tail = tail->previous;

            if (node->previous)
                node->previous->next = node->next;

            if (node->next)
                node->next->previous = node->previous;

            delete node;
            ++numDeleted;
        }

        node = nextNode;
    }

    return numDeleted;
}

话虽如此,您确实应该使用std::list而不是手动列表实现。让它为您处理节点,包括删除值,例如:

#include <list>

class StringList
{
private:
    std::list<std::string> data;

public:
    ...
    void remove(string value);
};

void StringList::remove(string value)
{
    data.remove(value);
}