zoukankan      html  css  js  c++  java
  • 【链表】链表基本操作

      1 #include<iostream>
      2 
      3 
      4 using namespace std;
      5 
      6 typedef struct _NODE_
      7 {
      8     int a;
      9     _NODE_* pNext;
     10 
     11 }Node,*pNode;
     12 
     13 
     14 class CList
     15 {
     16 
     17 private:
     18     pNode m_pHead;
     19     pNode m_pTail;
     20     int iNodeCount;
     21 
     22 public:
     23     CList()
     24     {
     25         m_pHead = m_pTail = NULL;
     26         iNodeCount = 0;
     27     }
     28     ~CList()
     29     {
     30 
     31     }
     32     void InitList();
     33     pNode CreateNode(int a);
     34     bool LinkNode(pNode pNodeTemp);
     35     bool InsertNodeByIndex(pNode pNodeTemp,int iIndex);
     36     bool DeleteNode(int iIndex);
     37     void DestroyList();
     38 
     39     void TravelList()
     40     {
     41         if(m_pHead == NULL && iNodeCount == 0)
     42         {
     43             cout<<"List is Empty."<<endl;
     44             return ;
     45         }
     46         pNode pNodeTemp = m_pHead;
     47 
     48         for(int i = 0;i<iNodeCount;i++)
     49         {
     50             cout<<pNodeTemp->a<<ends;
     51             pNodeTemp = pNodeTemp->pNext;
     52         }
     53         cout<<endl;
     54     }
     55 
     56     
     57 };
     58 
     59 
     60 void CList::InitList()
     61 {
     62     if(m_pHead != NULL)
     63     {
     64         DestroyList();
     65     }
     66 
     67     m_pHead = m_pTail = NULL;
     68 
     69     iNodeCount = 0;
     70 }
     71 pNode CList::CreateNode(int a)
     72 {
     73     pNode pNodeTemp = new Node;
     74 
     75     if(pNodeTemp != NULL)
     76     {
     77         pNodeTemp->a = a;
     78         pNodeTemp->pNext = NULL;
     79         
     80         return pNodeTemp;
     81     }
     82     return false;
     83 }
     84     
     85 bool CList::LinkNode(pNode pNodeTemp)
     86 {
     87     if(m_pHead == NULL)
     88     {
     89         m_pHead = m_pTail = pNodeTemp;
     90     }
     91     else
     92     {
     93         m_pTail->pNext = pNodeTemp;
     94         m_pTail = pNodeTemp;
     95     }
     96 
     97     iNodeCount++;
     98 
     99     return true;
    100 }
    101 
    102 bool CList::InsertNodeByIndex(pNode pNodeTemp,int iIndex)
    103 {
    104     if(iIndex >=1 && iIndex <= iNodeCount)
    105     {
    106         pNode pNodeInsert = m_pHead;
    107 
    108         for(int i=1;i<iIndex-1;i++)
    109         {
    110             pNodeInsert = pNodeInsert->pNext;
    111         }
    112         
    113         pNodeTemp->pNext = pNodeInsert->pNext;
    114         pNodeInsert->pNext = pNodeTemp;
    115 
    116         iNodeCount++;
    117         return true;
    118     }
    119 
    120     return false;
    121 }
    122 bool CList::DeleteNode(int iIndex)
    123 {
    124      pNode pNodeDel = m_pHead;
    125     for(int i=1;i<iIndex-1;i++)
    126     {
    127         pNodeDel = pNodeDel->pNext;
    128     }
    129 
    130     pNodeDel->pNext = pNodeDel->pNext->pNext;
    131 
    132     iNodeCount--;
    133     if(iNodeCount == 0)
    134     {
    135         m_pHead = m_pTail = NULL;
    136     }
    137 
    138     return true;
    139 }
    140 
    141 void CList::DestroyList()
    142 {
    143     pNode pNodeDel = m_pHead;
    144 
    145     while(pNodeDel != NULL)
    146     {
    147         m_pHead = m_pHead->pNext;
    148 
    149         delete pNodeDel;
    150 
    151         pNodeDel = m_pHead;
    152 
    153         iNodeCount--;
    154     }
    155 
    156     m_pHead = m_pTail = NULL;
    157     
    158     iNodeCount = 0;
    159 }
    160 
    161 
    162 
    163 
    164 
    165 
    166 int main()
    167 {
    168 
    169     CList CListObj;
    170 
    171     pNode pNodeTemp = {0};
    172 
    173     int i = 0;
    174     int a = 0;
    175     int iIndex = 0;
    176     cout<<"Test."<<endl;
    177     for(i=0;i<5;i++)
    178     {
    179         cout<<"Please Input Data:"<<endl;
    180         cin>>a;
    181         pNode pNodeTemp = CListObj.CreateNode(a);
    182         CListObj.LinkNode(pNodeTemp);
    183     }
    184     CListObj.TravelList();
    185 
    186     cout<<"Input a Index to Delete."<<endl;
    187     cin>>iIndex;
    188 
    189     CListObj.DeleteNode(iIndex);
    190     
    191     CListObj.TravelList();
    192 
    193     cout<<"Input Data and Index to Inesert."<<endl;
    194     cout<<"Data:"<<endl;
    195     cin>>a;
    196     cout<<"Index:"<<endl;
    197     cin>>iIndex;
    198     pNodeTemp = CListObj.CreateNode(a);
    199 
    200     CListObj.InsertNodeByIndex(pNodeTemp,iIndex);
    201 
    202     
    203     CListObj.TravelList();
    204 
    205 
    206     cout<<endl;
    207 
    208     cout<<"DestroyList."<<endl;
    209 
    210     CListObj.DestroyList();
    211 
    212     cout<<endl;
    213     CListObj.TravelList();
    214     return 0;
    215 }

  • 相关阅读:
    切割图像(一)概要
    无锁队列--基于linuxkfifo实现
    c++ virturn function -- 虚函数
    c friend -- 友元
    c++ anonymous union,struct -- 匿名联合体和机构体
    c++ anonymous namespace -- 匿名空间
    c++ inheritance -- 继承
    c++ 类名和enum时重复时要在类名前加class::
    c vs c++ in strcut and class
    C++ operator overload -- 操作符重载
  • 原文地址:https://www.cnblogs.com/Lee-geeker/p/3294243.html
Copyright © 2011-2022 走看看