| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3456 人关注过本帖
标题:C++中CSimpleList的问题
取消只看楼主 加入收藏
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
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
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
收藏
得分:0 
真的服了我,我还看了几遍
2021-10-08 14:30
快速回复:C++中CSimpleList的问题
数据加载中...
 
   



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

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