标题:C++中CSimpleList的问题
只看楼主
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
结帖率:100%
已结贴  问题点数:20 回复次数:2 
C++中CSimpleList的问题
C++中CSimpleList的实现与测试实例
#include <windows.h>
#include <stddef.h>
#include<stdio.h>
class CSimpleList
{
public:
    CSimpleList(int nNextOffset = 0);
    void Construct(int nNextOffset);

    // 提供给用户的接口函数(Operations),用于添加、删除和遍历节点
    BOOL IsEmpty() const;
    void AddHead(void* p);
    void RemoveAll();
    void* GetHead() const;
    void* GetNext(void* p) const;
    BOOL Remove(void* p);

    // 为实现接口函数所需的成员(Implementation)
    void* m_pHead;        // 链表中第一个元素的地址
    size_t m_nNextOffset;    // 数据结构中pNext成员的偏移量
    void** GetNextPtr(void* p) const;
};

// 类的内联函数
inline CSimpleList::CSimpleList(int nNextOffset)
{
    m_pHead = NULL; m_nNextOffset = nNextOffset;
}
inline void CSimpleList::Construct(int nNextOffset)
{
    m_nNextOffset = nNextOffset;
}
inline BOOL CSimpleList::IsEmpty() const
{
    return m_pHead == NULL;
}
inline void CSimpleList::RemoveAll()
{
    m_pHead = NULL;
}
inline void* CSimpleList::GetHead() const
{
    return m_pHead;
}
inline void* CSimpleList::GetNext(void* preElement) const
{
    return *GetNextPtr(preElement);
}
inline void** CSimpleList::GetNextPtr(void* p) const
{
    return (void**)((BYTE*)p + m_nNextOffset);
}
void CSimpleList::AddHead(void* p)
{
    *GetNextPtr(p) = m_pHead;
    m_pHead = p;
}

BOOL CSimpleList::Remove(void* p)
{
    if (p == NULL)    // 检查参数
        return FALSE;

    BOOL bResult = FALSE; // 假设移除失败
    if (p == m_pHead)
    {
        // 要移除头元素
        m_pHead = *GetNextPtr(p);
        bResult = TRUE;
    }
    else
    {
        // 试图在表中查找要移除的元素
        void* pTest = m_pHead;
        while (pTest != NULL && *GetNextPtr(pTest) != p)
            pTest = *GetNextPtr(pTest);

        // 如果找到,就将元素移除
        if (pTest != NULL)
        {
            *GetNextPtr(pTest) = *GetNextPtr(p);
            bResult = TRUE;
        }
    }
    return bResult;
}
struct mythreaddata
{
    mythreaddata* pnext;
    int nsomedata;
};

void main()
{
    mythreaddata* pdata;
    CSimpleList list;
    list.Construct(offsetof(mythreaddata, pnext)); // 告诉csimplelist类pnext成员的偏移量

    // 向链表中添加成员
    for (int i = 0; i < 10; i++)
    {
        pdata = new mythreaddata;
        pdata->nsomedata = i;
        list.AddHead(pdata);
    }

    // …………   // 使用链表中的数据

    // 遍历整个链表,释放mythreaddata对象占用的空间
    pdata = (mythreaddata*)list.GetHead();
    while (pdata != NULL)
    {
        mythreaddata* pnextdata = pdata->pnext;
        printf(" the value of nsomedata is: %d \n", pdata->nsomedata);
        delete pdata;
        pdata = pnextdata;
    }
}
我在这个基础上加入c++的nullptr,和bool以及命名空间
#include<Windows.h>
namespace CWAF
{
    namespace CWTLS
    {
        class CSimpleList
        {
        private:
            void* m_pHead;
            size_t m_nNextOffset;
            void** GetNextPtr(void* p)const;
        public:
            explicit CSimpleList(int nNextOffset=0);
            virtual ~CSimpleList()=default;
            void Construct(int nNextOffset);
            bool IsEmpty()const;
            void AddHead(void* p);
            void RemoveAll();
            void* GetHead()const;
            void* GetNext(void* p)const;
            bool Remove(void* p);
        };
    }
}
#include "Cwaf.h"
namespace CWAF
{
    namespace CWTLS
    {
        CSimpleList::CSimpleList(int nNextOffset )
        {
            m_pHead = nullptr;
            m_nNextOffset = nNextOffset;
            return;
        }
        void CSimpleList::Construct(int nNextOffset)
        {
            m_nNextOffset = nNextOffset;
            return;
        }
        bool CSimpleList::IsEmpty()const
        {
            return m_pHead == nullptr;
        }
        void CSimpleList::RemoveAll()
        {
            m_pHead = nullptr;
        }
        void* CSimpleList::GetHead()const
        {
            return m_pHead;
        }
        void* CSimpleList::GetNext(void* preElement)const
        {
            return *GetNextPtr(preElement);
        }
        void** CSimpleList::GetNextPtr(void* p)const
        {
            return (void**)((BYTE*)p + m_nNextOffset);
        }
        void CSimpleList::AddHead(void* p)
        {
            *GetNextPtr(p) = m_pHead;
            m_pHead = p;
        }
        bool CSimpleList::Remove(void* p)
        {
            if (p == nullptr)
                return false;
            bool bReslut = false;
            if (p == m_pHead)
            {
                m_pHead = *GetNextPtr(p);
                bReslut = true;
            }
            else
            {
                void* pTest = m_pHead;
                while (pTest != nullptr && *GetNextPtr(pTest) != p)
                    pTest = *GetNextPtr(pTest);
               
                if (pTest != nullptr)
                {
                    *GetNextPtr(pTest) = *GetNextPtr(p);
                    bReslut = true;
                }
            }
            return bReslut;
        }
        
    }
}
#include<stdio.h>
#include <stddef.h>
#include"Cwaf.h"
struct MyThreadData
{
    MyThreadData* pNext;
    int nSomeData;
};
int main()
{
    MyThreadData* pData;
    CWAF::CWTLS::CSimpleList list;
    list.Construct(offsetof(MyThreadData, pNext));
    for (int i = 0; i < 10; ++i)
    {
        pData = new MyThreadData;
        pData->nSomeData = i;
        list.AddHead(pData);
    }
    pData = (MyThreadData*)list.GetHead();
    while (pData != nullptr);
    {
        MyThreadData* pNextDate = pData->pNext;
        printf("The value of nSomeData is %d\n", pData->nSomeData);
        delete pData;
        pData = pNextDate;
    }
    return 0;
}
运行没结果
搜索更多相关主题的帖子: return int BOOL void const 
2021-10-08 11:51
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
无关的信息说得太多,nullptr、bool、名字空间 等等都必然与问题无关

我使用文字比对软件,发现
原先 while (pdata != NULL)
后来 while (pData != nullptr);

你有没有发现多了个分号(;)
2021-10-08 14:16
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
得分:0 
真的服了我,我还看了几遍
2021-10-08 14:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-507076-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014151 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved