最近开始学习数据结构,每天都打代码打到深夜~~突然想发表一下自己的成果,呵呵贴吧!! 觉得好的支持一下 ,谢谢! 类定义声明SList.h #ifndef S_LIST_H #define S_LIST_H struct _SListNode; typedef struct _SListNode SListNode; typedef int (*DataCmpFunc)(void* ctx,void* data); typedef int (*DataVisitFunc)(void* ctx,void* data); class CSList { private: SListNode* m_pfirst; //创建节点 SListNode* CreateNode(void* _data); //销毁节点 void DestroyNode(SListNode* _node); public: //创建单链表 CSList(); //销毁单链表 ~CSList(); //插入节点 int Insert(size_t _index,void* _data); //追加节点 int Append(void* _data); //在第一个节点的前面插入一个节点 int Prepend(void* _data); //删除节点 int Delete(size_t _index); //查找 int Find(DataCmpFunc _lpCmpFunc,void* _ctx); //遍历 int Foreach(DataVisitFunc _lpVisitFunc,void* _ctx); //获取长度 size_t Length(); //获取某个节点 int GetByIndex(size_t _index,void** _data); //设置某个节点 int SetByIndex(size_t _index,void* _data); //反转所有节点 int reverse(void); }; #endif 类具体实现SList.cpp #include "slist.h" #include <stdio.h> struct _SListNode { void* data; _SListNode* next; }; //创建节点 SListNode* CSList::CreateNode(void* _data) { SListNode* node = new SListNode[1]; if (node != NULL) { node->next = NULL; node->data = _data; } return node; } //销毁节点 void CSList::DestroyNode(SListNode* _node) { delete _node; } //创建单链表 CSList::CSList() { m_pfirst = NULL; } //销毁单链表 CSList::~CSList() { SListNode* node = NULL; SListNode* cursor = NULL; cursor = m_pfirst; while(cursor != NULL) { node = cursor; cursor = cursor->next; DestroyNode(node); } m_pfirst = NULL; } //插入节点 int CSList::Insert(size_t _index,void* _data) { // 插入是否越界 if (_index > Length()) { return -1; } // 创建一个新的节点 SListNode* node; node = CreateNode(_data); if (NULL == node) { return -1; } // 第一次插入节点 if (NULL == m_pfirst) { m_pfirst = node; return 0; } // 在索引0处插入节点 if (0 == _index) { node->next = m_pfirst; m_pfirst = node; return 0; } // 查找到index SListNode* prev = NULL; SListNode* cursor = NULL; cursor = m_pfirst; while(cursor != NULL && _index >0) { prev = cursor; cursor = cursor->next; _index--; } prev->next = node; node->next = cursor; return 0; } //追加节点 int CSList::Append(void* _data) { return Insert(Length(),_data); } //在第一个节点的前面插入一个节点 int CSList::Prepend(void* _data) { return Insert(0,_data); } //删除节点 int CSList::Delete(size_t _index) { // 删除是否越界 if (_index >= Length()) { return -1; } SListNode* prev = NULL; SListNode* cursor = NULL; // 删除第0个节点 if (0 == _index) { cursor = m_pfirst; m_pfirst = m_pfirst->next; DestroyNode(cursor); return 0; } // 普通删除 cursor = m_pfirst; prev = cursor; while (cursor != NULL && _index > 0) { prev = cursor; cursor = cursor->next; _index--; } prev->next = cursor->next; DestroyNode(cursor); return 0; } //查找 int CSList::Find(DataCmpFunc _lpCmpFunc,void* _ctx) { int index = 0; SListNode* cursor = NULL; cursor = m_pfirst; while(cursor != NULL) { if (0 == _lpCmpFunc(_ctx,cursor->data)) { return index; } cursor = cursor->next; index++; } return -1; } //遍历 int CSList::Foreach(DataVisitFunc _lpVisitFunc,void* _ctx) { int result = -1; SListNode* cursor = NULL; cursor = m_pfirst; while(cursor != NULL) { result = _lpVisitFunc(_ctx,cursor->data); cursor = cursor->next; } return result; } //获取长度 size_t CSList::Length() { int nLength = 0; SListNode* cursor = NULL; cursor = m_pfirst; while(cursor != NULL) { nLength++; cursor = cursor->next; } return nLength; } //获取某个节点 int CSList::GetByIndex(size_t _index,void** _data) { SListNode* cursor = NULL; if (_index > Length()) { return -1; } cursor = m_pfirst; while(cursor != NULL && _index > 0) { cursor = cursor->next; _index--; } *_data = cursor->data; return 0; } //设置某个节点 int CSList::SetByIndex(size_t _index,void* _data) { SListNode* cursor = NULL; if (_index > Length()) { return -1; } cursor = m_pfirst; while(cursor != NULL && _index > 0) { cursor = cursor->next; _index--; } cursor->data = _data; return 0; } //反转所有节点 int CSList::reverse(void) { if (m_pfirst == NULL || Length() < 1) { return -1; } SListNode* prev = m_pfirst; //前一个节点 SListNode* cursor = m_pfirst->next;//当前节点 SListNode* next = NULL;//下一个节点 while(cursor != NULL) { next = cursor->next; cursor->next = prev; prev = cursor; cursor = next; } m_pfirst->next = NULL; m_pfirst = prev; return 0; } 一个试用小例子Main.cpp #include "slist.h" #include <iostream> using namespace std; static int print_int(void* ctx, void* data) { cout<<*(int*)(data); return 0; } static int sum_int(void* ctx, void* data) { int* sum = (int*)(ctx); *sum += *(int*)(data); return 0; } static int Cmp_int(void* ctx,void* data) { if (*(int*)(ctx) == *(int*)(data)) return 0; return -1; } int main() { int a = 0; int b = 1; int c = 2; int d = 3; int sum = 0; CSList* thiz = new CSList; thiz->Append(&a); thiz->Append(&b); thiz->Insert(2,&c); thiz->Append(&d); thiz->Foreach(print_int,NULL); cout<<endl; thiz->Foreach(sum_int,&sum); cout<<"sum = "<<sum<<endl; thiz->reverse(); thiz->Foreach(print_int,NULL); cout<<endl; thiz->reverse(); thiz->Foreach(print_int,NULL); cout<<endl; int n; cout<<"请输入需要查找的数:"; cin>>n; n = thiz->Find(Cmp_int,&n); cout<<"找到位置:"<<n<<endl; delete thiz; return 0; }