#ifndef LINK_H #define LINK_H #include <memory> #include <iostream> struct ListNode { int val; std::shared_ptr<ListNode> next; }; struct LinkList{ std::shared_ptr<ListNode> begin; std::shared_ptr<ListNode> end; }; std::shared_ptr<ListNode> CreateNode(int i); bool CreateLinkList(LinkList& ll,std::shared_ptr<ListNode>& insertNode); bool AppendNode(LinkList& ll,std::shared_ptr<ListNode>& insertNode); void PrintLinkList(const LinkList& ll); std::shared_ptr<ListNode> FindInLinkList(LinkList& ll,int findValue); bool DeleteNode(LinkList& ll,int value); #endif // LINK_H
#include "link.h" std::shared_ptr<ListNode> CreateNode(int i){ std::shared_ptr<ListNode> p(new ListNode()); p->val = i; p->next = nullptr; return p; } bool CreateLinkList(LinkList& ll,std::shared_ptr<ListNode>& insertNode){ bool ret = false; ll.begin = ll.end = insertNode; ret =true; return ret; } bool AppendNode(LinkList& ll,std::shared_ptr<ListNode>& appendNode){ bool ret = false; if( nullptr == appendNode){ return ret; } if(nullptr == ll.end || nullptr == ll.begin){ ll.begin = ll.end = appendNode; ret = true; return ret; } ll.end->next = appendNode; ll.end = appendNode; ret = true; return ret; } void PrintLinkList(const LinkList& ll){ std::shared_ptr<ListNode> p = ll.begin; std::cout << "print linklist: " << std::endl; while( p != nullptr ){ std::cout << p->val << " "; p = p->next; } std::cout << std::endl; std::cout << std::endl; } std::shared_ptr<ListNode> FindInLinkList(LinkList& ll,int findValue){ std::shared_ptr<ListNode> p = ll.begin; do{ if(p->val == findValue){ break; } p= p->next; }while(p != nullptr); return p; } bool DeleteNode(LinkList& ll,int value){ bool ret = false; if(nullptr == ll.begin || nullptr == ll.end){ return ret; } if(ll.begin->val == value){ if(ll.begin == ll.end){ ll.begin = ll.end = nullptr; }else{ ll.begin = ll.begin->next; } ret = true; return ret; } // 遍历链表 查找符合的node std::shared_ptr<ListNode> p = ll.begin; while(p->next != nullptr){ if(p->next->val == value){ break; } p = p->next; } if(p->next != nullptr){ if(p->next == ll.end){ ll.end = p; } p->next = p->next->next; } ret = true; return ret; }
// 123.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <assert.h> #include "link.h" #include <windows.h> void TestLinkList() { LinkList ll; if (!AppendNode(ll, CreateNode(1))) { std::cerr << __FUNCTION__ << "(). line:" << __LINE__ << " Error !" << std::endl; return; } std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); if (!AppendNode(ll, CreateNode(2))) { std::cerr << __FUNCTION__ << "(). line:" << __LINE__ << " Error !" << std::endl; return; } std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); if (!AppendNode(ll, CreateNode(3))) { std::cerr << __FUNCTION__ << "(). line:" << __LINE__ << " Error !" << std::endl; return; } std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); std::shared_ptr<ListNode> p = FindInLinkList(ll, 7); assert(p == nullptr); p = FindInLinkList(ll, 2); assert(p != nullptr && p->val == 2); DeleteNode(ll, 3); std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); DeleteNode(ll, 2); std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); DeleteNode(ll, 1); // std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; // std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); if (!AppendNode(ll, CreateNode(1))) { std::cerr << __FUNCTION__ << "(). line:" << __LINE__ << " Error !" << std::endl; return; } std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); if (!AppendNode(ll, CreateNode(2))) { std::cerr << __FUNCTION__ << "(). line:" << __LINE__ << " Error !" << std::endl; return; } std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); if (!AppendNode(ll, CreateNode(3))) { std::cerr << __FUNCTION__ << "(). line:" << __LINE__ << " Error !" << std::endl; return; } std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); DeleteNode(ll, 1); std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); DeleteNode(ll, 2); std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); DeleteNode(ll, 3); //std::cout << "begin:" << ll.begin << " begin.val:" << ll.begin->val << std::endl; //std::cout << "end:" << ll.end << " end.val:" << ll.end->val << std::endl; PrintLinkList(ll); } void TestLinkListMemory() { LinkList ll; LinkList ll0; while(1) { for (int i = 1; i<1000; ++i) { AppendNode(ll, CreateNode(i)); } for (int i = 1; i<1000; ++i) { DeleteNode(ll, i); } std::cout << "begin:" << ll.begin << std::endl; std::cout << "end:" << ll.end << std::endl; PrintLinkList(ll); } } unsigned int dictIntHashFunction(unsigned int key) { key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; } int main(int argc, char *argv[]) { TestLinkList(); TestLinkListMemory(); return 0; }
时间: 2024-10-26 12:21:13