數據結構——雙向鏈表實現,基本操作的C++版

對於循環雙向鏈表

判斷一個鏈表是否爲空的條件爲:head->next==head (頭指針)

判斷*p爲最後一個節點的條件爲:p->next=head

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. /*雙鏈表結構*/  
  5. typedef struct node  
  6. {  
  7.     int data;  
  8.     struct node *prior;  
  9.     struct node *next;  
  10.   
  11. }DNode;   
  12. /*創建一個帶頭節點的雙鏈表*/  
  13. void CreateDLink(DNode *&head)  
  14. {  
  15.     int x;  
  16.     DNode *p,*s;  
  17.     s=(DNode *)malloc(sizeof(DNode));  
  18.     if(s==NULL)  
  19.     {  
  20.        cout<<"Fail to malloc the head node!"<<endl;    
  21.     }  
  22.        s->data=0;  
  23.        s->prior=NULL;  
  24.        s->next=NULL; //建立一個頭結點爲head的雙鏈表  
  25.     head=s;   //若是循環雙向鏈表則爲:s->next=s->prior=s;  
  26.     cout<<"please input the data of the node"<<endl;  
  27.     cin>>x;  
  28.     while(x!=0)  
  29.     {  
  30.       p=(DNode *)malloc(sizeof(DNode));  
  31.      if(p==NULL)  
  32.      {  
  33.         cout<<"Fail to malloc a new  node!"<<endl;  
  34.      }  
  35.        
  36.      p->data=x;  
  37.      s->next=p;  
  38.      p->prior=s;  
  39.      p->next=NULL;  
  40.      s=p;  
  41.   
  42.      cin>>x;  
  43.     }  
  44.   
  45. }  
  46.   
  47. void PrintLink(DNode *head)  
  48. {  
  49.     if(head->next==NULL)  
  50.     {  
  51.         cout<<"The list is NULL"<<endl;  
  52.         return ;  
  53.     }  
  54.     DNode *p;  
  55.     p=head->next;  
  56.     while(p!=NULL)  
  57.     {  
  58.         cout<<p->data<<" ";  
  59.         p=p->next;  
  60.           
  61.     }  
  62.    cout<<endl;  
  63. }  
  64. int Length(DNode *head) //求雙鏈表長度  
  65. {  
  66.     int len=0;  
  67.     DNode * p=head->next;  
  68.     while(p!=NULL)  
  69.     {  
  70.         len++;  
  71.         p=p->next;  
  72.     }  
  73.     return len;  
  74.   
  75. }  
  76. DNode * Get(DNode *head,int i) //獲取鏈表第i個節點地址  
  77. {  
  78.     int j=0;  
  79.     DNode *p=head->next;  
  80.     while(j<i&&p!=NULL)  
  81.     {  
  82.         p=p->next;  
  83.         j++;  
  84.     }  
  85.     if(p!=NULL)  
  86.         return p;  
  87.     else   
  88.         return NULL;  
  89. }  
  90. int InsertNode(DNode *head,int x,int i)//在第i個位置插入一個節點  
  91. {  
  92.     DNode *s,*p;  
  93.     s=(DNode *)malloc(sizeof(DNode));  
  94.     s->data=x;  
  95.     if(i==0)  
  96.     {  
  97.         s->next=head->next;  
  98.         if(head->next!=NULL)  
  99.             head->next->prior=s;  
  100.         s->prior=head;  
  101.         head->next=s;  
  102.           
  103.     }  
  104.     else  
  105.     {  
  106.         p=Get(head,i-1); //查找待插入節點前一個位置  
  107.         if(p==NULL)  
  108.             return 0;  
  109.         else  
  110.         {  
  111.             s->next=p->next;  
  112.             if(p->next!=NULL)  
  113.                 p->next->prior=s;  
  114.             s->prior=p;  
  115.             p->next=s;  
  116.         }  
  117.     }  
  118.   
  119.     return 1;  
  120. }  
  121.   
  122. void DeleteNode(DNode *head,int index) //刪除第index個節點  
  123. {  
  124.     if(head->next==NULL)  
  125.     {  
  126.         cout<<"The list is NULL"<<endl;  
  127.         return;  
  128.     }  
  129.     DNode *p,*s/*保存待刪除的節點*/;  
  130.     if(index==0)  
  131.     {  
  132.         s=head->next;  
  133.         head->next=s->next;  
  134.         if(s->next!=NULL)  
  135.             s->next->prior=head;  
  136.          free(s);  
  137.     }  
  138.     else  
  139.     {  
  140.         p=Get(head,index-1);  
  141.         if(p==NULL)  
  142.             cout<<"The Node "<<index<<"is not exist"<<endl;  
  143.         else  
  144.         {  
  145.             s=p->next;  
  146.             p->next=s->next;  
  147.             if(s->next!=NULL)  
  148.                 s->next->prior=p;  
  149.             free(s);  
  150.         }  
  151.     }  
  152.   
  153. }  
  154. int main(int argc,char *argv[])  
  155. {  
  156.   DNode *head;  
  157.   CreateDLink(head);   
  158.   cout<<"鏈表長度爲:"<<Length(head)<<endl;  
  159.    PrintLink(head);  
  160.    cout<<"第3個節點的值爲:"<<Get(head,3)->data<<endl;  
  161.    
  162.    cout<<"輸入插入節點的位置及值:"<<endl;  
  163.     int value, index;  
  164.     cin>>index>>value;  
  165.    InsertNode(head,value,index);  
  166.    cout<<"插入後的鏈表爲:"<<endl;  
  167.        PrintLink(head);  
  168.   
  169.        cout<<"請輸入要刪除的節點位置:"<<endl;  
  170.        cin>>index;  
  171.        DeleteNode(head,index);  
  172.    PrintLink(head);  
  173.     return 0;  
  174. }  
發佈了33 篇原創文章 · 獲贊 10 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章