简体   繁体   English

链表实现C ++

[英]Linked list implementation c++

I am trying to store data by ID using convening structs and pointers, but I am only able to store the first value. 我正在尝试使用常规结构和指针按ID存储数据,但是我只能存储第一个值。 This is my first time trying to use this kind of method for store data dynamically, so I am not sure I am using node->p_next and node->p_prev in the right way. 这是我第一次尝试使用这种方法动态存储数据,因此我不确定我node->p_prev以正确的方式使用了node->p_nextnode->p_prev

Can someone give a look and tell me if I am doing something wrong in my implementation? 有人可以看一下并告诉我我的实现是否做错了吗?

List.h 清单.h

class list
{
public:

    // Construction /destruction
    list();
    virtual ~list();

    void DeleteAll();
    void DeleteID(uint16_t ID);
    uint8_t add(uint16_t ID, uint8_t data);
    void printf(uint16_t ID);

    struct node
    {
        uint16_t ID;
        uint8_t  data;
        node * p_prev;
        node * p_next;
    };  
protected:
    node * LookingforRecord(uint16_t id, bool add = false);
    node * m_p_node;
};

List.cpp List.cpp

#include "list.h"

uint16_t ID = 0;
// Construction
list::list()
{
    m_p_node = 0;
}

// Destruction
list::~list()
{
    DeleteAll();
}

void list::printf(uint16_t ID)

    node * p_node = LookingforRecord(ID, false);

    if (p_node != 0)
    {
        printf("p_node->ID %d \r\n", p_node->ID);
        printf("p_node->data %d \r\n", p_node->data);
    }
}
uint8_t list::add(uint16_t ID, uint8_t data)
{
    uint8_t batch_size = 0;
    // Find the ID, creating one if necessary
    node * p_node = LookingforRecord(ID, true);
    if (p_node != 0)
    {
        //store ID and data
        p_node->ID= ID;
        p_node->data = data;

        batch_size = ID;
    }
    return batch_size;
}

list::Node * list::LookingforRecord(uint16_t ID, bool add)// Finds a ID
{
    node * p_node= m_p_node;

    while (p_node != NULL)
    {
        // Find by ID
        if(p_node->ID == ID)        
            return p_node;  

        p_node = p_node->p_next; // Move on to the next one
    }       

    if(add)
    {
        if ((p_node = new node) != 0)
        {
            memset(p_node, 0, sizeof(node));
            p_node->ID = ID;
            p_node->data = m_p_node;
            **p_node->p_next = m_p_node;**

            if (m_p_node != 0)  
                m_p_node->p_prev = p_node;          
            else
                m_p_node = p_node;
        }
    }
    return p_node;
}

void list::DeleteAll() // Deletes everything
{
    while (m_p_node != 0)
    {
        DeleteID(m_p_node->ID);
    }
}

void list::DeleteID(uint16_t ID) // Deletes ID
{ 
    // Find by ID
    node * p_node = LookingforRecord(ID, false);
    if (p_node != 0)
    {
        // Unlink it from the list
        if(p_node->p_prev != 0)
            p_node->p_prev->p_next = p_node->p_next;
        if (p_node->p_next != 0)
            p_node->p_next->p_prev = p_node->p_prev;
        if (m_p_node == p_node)
            m_p_node = p_node->p_next;

        delete p_node;
    }
}

My main concern is at LookingforRecord . 我主要关心的是LookingforRecord It is not entering into if(p_node->ID == ID) , and it is not updating p_node from m_p_node , which is supposed to be updated each time the code enters lookingforRecord . 它不进入if(p_node->ID == ID)并且它不更新p_nodem_p_node ,这是应该各码进入时间被更新lookingforRecord

Here are some things that are definitely wrong. 这里有些事情肯定是错误的。

1) You have an add method uint8_t add(uint16_t ID, uint8_t data) in your header, but you don't implement it. 1)标头中有一个添加方法uint8_t add(uint16_t ID,uint8_t数据),但未实现。 You use add a new element to your module in LookingforRecord method. 您可以在LookingforRecord方法中使用向模块添加新元素。 I suggest you to separate those two and call one from another(if you really need it, which I believe you don't) 我建议您将两者分开,然后彼此调用(如果您确实需要,我认为您不需要)

2) You use your uint8_t batch_size variable to store ID, which is type uint16_t. 2)您使用uint8_t batch_size变量来存储ID,即uint16_t类型。 I believe you know why it is bad :D 我相信你知道为什么它不好:D

3) Your DeleteID is not entirely correct. 3)您的DeleteID并不完全正确。 If you want to delete the first element, you should null the next element's prev pointer, otherwise it'll point to garbage. 如果要删除第一个元素,则应将下一个元素的prev指针为空,否则它将指向垃圾。

4) Use both first and last pointers in your protected(why not private) section. 4)在您的protected(为什么不私有)部分中同时使用第一个和最后一个指针。 And call them accordingly. 并据此打电话给他们。 It is very confusing. 这很令人困惑。 It would be much easier to add a new node if you have the last pointer. 如果有最后一个指针,则添加新节点会容易得多。

5) You adding it wrong. 5)您添加错误。 When you add a new node you do this 添加新节点时,请执行此操作

if (m_p_node != 0)  
   m_p_node->p_prev = p_node; 

But you need your LAST element's p_next point to a new element you just added. 但是您需要LAST元素的p_next指向刚刚添加的新元素。

6) If it is a forward list, you only need p_next, but if it is a doubly linked list(where you need both p_prev and p_next) you should be able to add elements from both sides. 6)如果它是一个前向列表,则只需要p_next,但是如果它是一个双链表(其中需要p_prev和p_next),则应该能够从两侧添加元素。

I hope you fix it ^_^ 我希望你能解决它^ _ ^

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

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