最简单链表实现

下面由MFC代码中抽取来,以最简便的方式实现了的链表功能:

#include <windows.h>
#include <stddef.h>
#include <stdio.h>

#define AFX_INLINE inline /*__forceinline*/
#define ASSERT(f)          ((void)0)
#define AFX_NOVTABLE
#define DEBUG_ONLY(f)      (f)

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;

    void** GetNextPtr(void* p) const;   // somewhat trusting...
};

AFX_INLINE CSimpleList::CSimpleList(int nNextOffset)
{
    m_pHead = NULL;
    m_nNextOffset = nNextOffset;
}

AFX_INLINE void CSimpleList::Construct(int nNextOffset)
{
    ASSERT(m_pHead == NULL);
    m_nNextOffset = nNextOffset;
}

AFX_INLINE BOOL CSimpleList::IsEmpty() const
{
    return m_pHead == NULL;
}

void CSimpleList::AddHead(void* p)
{
    ASSERT(p != NULL);
    ASSERT(*GetNextPtr(p) == NULL);

    *GetNextPtr(p) = m_pHead;
    m_pHead = p;
}

AFX_INLINE void** CSimpleList::GetNextPtr(void* p) const
{
    ASSERT(p != NULL);
    return (void**)((BYTE*)p+m_nNextOffset);
}

AFX_INLINE void CSimpleList::RemoveAll()
{
    m_pHead = NULL;
}

AFX_INLINE void* CSimpleList::GetHead() const
{
    return m_pHead;
}

AFX_INLINE void* CSimpleList::GetNext(void* prevElement) const
{
    return *GetNextPtr(prevElement);
}

BOOL CSimpleList::Remove(void* p)
{
    ASSERT(p != NULL);

    if (m_pHead == NULL)
        return FALSE;

    BOOL bResult = FALSE;
    if (m_pHead == p)
    {
        m_pHead = *GetNextPtr(p);
        DEBUG_ONLY(*GetNextPtr(p) = NULL);
        bResult = TRUE;
    }
    else
    {
        void* pTest = m_pHead;
        while (pTest != NULL && *GetNextPtr(pTest) != p)
            pTest = *GetNextPtr(pTest);
        if (pTest != NULL)
        {
            *GetNextPtr(pTest) = *GetNextPtr(p);
            DEBUG_ONLY(*GetNextPtr(p) = NULL);
            bResult = TRUE;
        }
    }
    return bResult;
}

template<class TYPE>
class CTypedSimpleList : public CSimpleList
{
public:
    CTypedSimpleList(int nNextOffset = 0)
        : CSimpleList(nNextOffset) { }
    void AddHead(TYPE p)
    { CSimpleList::AddHead(p); }
    TYPE GetHead()
    { return (TYPE)CSimpleList::GetHead(); }
    TYPE GetNext(TYPE p)
    { return (TYPE)CSimpleList::GetNext(p); }
    BOOL Remove(TYPE p)
    { return CSimpleList::Remove((TYPE)p); }
    operator TYPE()
    { return (TYPE)CSimpleList::GetHead(); }
};

class AFX_NOVTABLE CNoTrackObject
{
public:
    void* PASCAL operator new(size_t nSize)
    {
        void* p = ::LocalAlloc(LPTR, nSize);
//         if (p == NULL)
//             AfxThrowMemoryException();
        return p;
    }

    void PASCAL operator delete(void* p)
    {
        if (p != NULL)
            ::LocalFree(p);
    }

    void PASCAL operator delete(void* pObject, LPCSTR, int)
    {
        if (pObject != NULL)
            ::LocalFree(pObject);
    }

    virtual ~CNoTrackObject() { }
};

struct CThreadData : public CNoTrackObject
{
    CThreadData* pNext; // required to be member of CSimpleList
    int nCount;         // current size of pData
    LPVOID* pData;      // actual thread local data (indexed by nSlot)
};

int main(int argc, char* argv[])
{
    CTypedSimpleList<CThreadData*> m_list;  // list of CThreadData structures
    m_list.Construct(offsetof(CThreadData, pNext));

    for (int i = 0; i < 10; i++)
    {
        CThreadData *p = new CThreadData;
        p->nCount = i;
        p->pData = NULL;
        m_list.AddHead(p);
    }

    CThreadData *p = m_list.GetHead();
    while (p != NULL)
    {
        printf("%d ", p->nCount);
        p = p->pNext;
//        p = m_list.GetNext(p);
    }

    return 0;
}
时间: 2025-01-16 07:11:51

最简单链表实现的相关文章

[Lua学习]简单链表

1 --简单链表 2 list = nil 3 4 for i = 1,5,1 do 5 a = io.read() 6 list = { ['next'] = list, ['value'] = a} 7 end 8 9 l = list 10 while l do 11 print(l.value) 12 l = l.next 13 end ps:不得不说lua令人惊叹.逆序形成链表结构. 遍历的时候也会逆序输出.

纯C语言(C89)实现简单链表

起因 工作很少接触纯C项目,业余写着玩玩,不断雕琢 目标 纯C实现简单链表,提供方便易用泛型接口,避免依赖 实现 完全封装,隐藏结构体细节,不支持栈创建 拷贝存储,轻微性能代价换来易用性 list.h #ifndef LIST_H #define LIST_H #include <stddef.h> typedef struct ListItem_ ListItem; typedef struct List_ List; List* list_new(); void list_free(Lis

简单链表-C语言复习

实现一个基本的链表,包括在链表后添加数据节点和输出链表i所有数据的功能 1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef struct node 5 { 6 int data; 7 struct node *link; 8 }NODE; 9 10 //定义头指针 11 NODE *head; 12 13 void add(int data) 14 { 15 16 NODE *p; 17 p=(NODE *)malloc(siz

一个简单链表的C++实现(二)

/* LList.cpp * Author: Qiang Xiao * Time: 2015-07-12 */ #include<iostream> using namespace std; class Node{ public: int data; Node* ptr; Node(int elem= 0, Node* node= NULL){this->data= elem; this->ptr= node;} }; class LList{ private: Node* hea

利用C#的指针编写都一个简单链表

using System; namespace UnsafeTest { unsafe struct link { public int x; public link* next; } class Program { static unsafe void Main(string[] args) { int val; link* head = stackalloc link[sizeof(link)]; link*q =head; for(int i=0;i<=10;i++) { val = i+

一个简单链表的C++实现

/* LList.cpp * Author: Qiang Xiao * Time: 2015-07-12 */ #include<iostream> using namespace std; class Node{ public: int data; Node* ptr; Node(int elem= 0, Node* node= NULL){this->data= elem; this->ptr= NULL;} }; class LList{ private: Node* hea

c实现简单链表

#include <stdio.h>  #include <stdlib.h> struct node {    int data;    struct node *next;}; int datas[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; void list_head_init(struct node *head){    head->data = 0;    head->next = NULL;} int  list_empty(s

5.STL简单链表(_cghList)的实现

我用VS2013写的程序(github ),list版本的代码位于cghSTL/version/cghSTL-0.3.2.rar 相较于vector的连续线性空间,list就显得复杂许多,它的好处是每次插入或删除一个元素,就配置或释放一个元素空间.因此,list对于空间的运用有绝对的精准,一点不浪费.Vector的插入操作可能造成内存的重新配置,但是List不会. List不再能够像vector一样以普通指针作为迭代器,因为其节点不保证在存储空间在连续存在.由于list是一个双向链表,迭代器必须

C语言实现单链表

单链表的应用非常广,它可以实现栈,队列等: Problem 我对学习任何东西都希望能找到尽可能简单的例子,而不是看起来好高大上的: 对链表这样简答的数据结构,有些书也是写得太过“完美”啦: 初学者很难抓住重点,反正我初学的时候就是看不懂: 因此我打算从简单入手去演示: 我们需要实现下面图这样的结构,叫做链表哈: Solution 首先咱们要想到这几个ABC圆圈可以代表一个数据的节点,什么是节点呢?: 节点就好比是几个同学聚集到一个宿舍一样,他们各自有各自的身高体重: 因此,我们需要一个结构体,声