zoukankan      html  css  js  c++  java
  • 链表

    struct ListNode{
        int m_nValue;
        ListNode* m_pNext;
    }
    
    ListNode* CreateListNode(int value);
    void ConnectListNodes(ListNode* pCurrent, ListNode* pNext);
    void PrintListNode(ListNode* pNode);
    void PrintList(ListNode* pHead);
    void DestroyList(ListNode* pHead);
    void AddToTail(ListNode** pHead, int value);
    void RemoveNode(ListNode** pHead, int value);
    //创建链表 ListNode* CreateListNode(int value){ ListNode* pNode = new ListNode(); pNode->m_nValue = value; pNode->m_pNext = nullptr; return pNode; } //添加元素 void AddToTail(ListNode** pHead,int value){ ListNode* pNew = new ListNode(); pNew->value = value; pNew->next = nullptr; if(*pHead == nullptr) *pHead = pNew; else{ ListNode* pNode = new ListNode(); pNode = *pHead; while(*pHead != nullptr) pHead = pHead->m_pNext; pNode->m_pNext = pNew; } } //删除元素 void RemoveNode(ListNode** pHead,int value){ if(pHead == nullptr || *pHead == nullptr) return; ListNode* pToBeDeleted = nullptr; if((*pHead)->m_nValue == value){ pToBeDeleted = *pHead; *pHead = (*pHead)->m_pNext; } else{ ListNode* pNode = *pHead; while(pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue != value) pNode = pNode->m_pNext; if(pNode->m_pNext != nullptr && pNode->m_pNext->m_nValue == value){ pToBeDelted = pNode->m_pNext; pNode->m_pNext = pNode->m_pNext->m_pNext; } } if(pToBeDeleted != nullptr){ delete pToBeDeleted; pToBeDeleted = nullptr; } } //销毁链表 void DestroyList(ListNode* pHead){ ListNode* pNode = pHead; while(pNode != nullptr){ pHead = pHead->m_pNext; delete[] pNode; pNode = pHead; } } //打印链表 void PrintList(ListNode* pHead){ printf("PringList starts. "); ListNOde* pNode = pHead; whiel(pNode != nullptr){ printf("%d ",pNode->m_nValue); pNode = pNode->m_pNext; } printf("PrintLisr end. "); } //打印节点 void PrintListNode(ListNode* pNode){ if(pNode == nullptr){ printf("The node is nullptr. "); } else{ printf("The key node is %d. ",pNode->m_nValue); } } //连接两个节点 void ConnectListNodes(ListNode* pCurrent,ListNode* pNext){ if(pCurrent == nullptr){ printf("Error to connect two nodes. "); exit(1); } pCurrent->m_pNext = pNext; }
  • 相关阅读:
    Bootstrap3系列:按钮式下拉菜单
    Bootstrap3系列:按钮组
    Bootstrap3系列:下拉菜单
    CSS系列:CSS常用样式
    Entity Framework中使用IEnumerable<T>、IQueryable<T>及IList<T>的区别
    ASP.NET中Session的sessionState 4种mode模式
    ASP.NET MVC系列:Area
    Sql Server系列:SQL语句查询数据库中表、视图、存储过程等组成
    ASP.NET MVC系列:Model
    jQuery LigerUI系列:ligerComboBox
  • 原文地址:https://www.cnblogs.com/ovs98/p/9870675.html
Copyright © 2011-2022 走看看