双向循环链表

来源:互联网 发布:标准普通话发音软件 编辑:程序博客网 时间:2024/05/02 02:37
  1.  #include<iostream>
  2. #include<malloc.h>
  3. #include<iostream>
  4. #include<malloc.h>
  5. #include<stdlib.h>
  6. using namespace std;
  7. #define OK 1
  8. #define ERROR 0
  9. #define OVERFLOW -2
  10. typedef int Status;
  11. typedef int ElemType; 
  12. typedef struct DuLNode
  13. {
  14.    ElemType data;
  15.    struct DuLNode *prior;
  16.    struct DuLNode *next;
  17. }DuLNode,*DuLinkList;
  18. Status InitDuList(DuLinkList &DL,int n)         //初始化 
  19. {
  20.      if(n<1) return ERROR;
  21.      DuLinkList p,q;
  22.       DL=(DuLinkList)malloc(sizeof(DuLNode));
  23.       for(int i=0;i<n;i++)
  24.        {
  25.          p=(DuLinkList)malloc(sizeof(DuLNode));
  26.          cin>>p->data;
  27.          if(!i) {
  28.                 DL->next=p;
  29.                 p->prior=DL;
  30.                 q=p;}
  31.          else   {
  32.                 q->next=p;
  33.                 p->prior=q;
  34.                 q=p;}
  35.          }
  36.       p->next=DL;
  37.       DL->prior=p;
  38.       return OK;
  39.        }
  40. DuLinkList GetElemP_DuL(DuLinkList DL,int i)        //返回第i个元素节点的指针 
  41. {
  42.    if(i<0) exit(1);
  43.    DuLinkList p=DL;
  44.    int j=0;
  45.    for(;p->next!=DL&&j<i;j++)
  46.            p=p->next;
  47.    if(p->next==DL&&j<i) return NULL;
  48.    return p;
  49.    }
  50. Status InsertDuList(DuLinkList &DL,int i,ElemType e)   //插入 
  51. {
  52.        DuLinkList p=GetElemP_DuL(DL,i),q;
  53.        if(!p) return ERROR;
  54.        if(!(q=(DuLinkList)malloc(sizeof(DuLNode)))) exit(OVERFLOW);
  55.         q->data=e;
  56.         q->prior=p->prior;
  57.         q->next=p;
  58.         p->prior->next=q;
  59.         p->prior=q;
  60.         return OK;
  61.         }
  62.        
  63. Status DeleteDuList(DuLinkList &DL,int i,ElemType &e) //删除 
  64. {
  65.        DuLinkList p=GetElemP_DuL(DL,i);
  66.        if(!p) return ERROR;
  67.        e=p->data;
  68.        p->prior->next=p->next;
  69.        p->next->prior=p->prior;
  70.        free(p);
  71.        return OK;
  72.        }
  73. void SortDuList(DuLinkList &DL,int n)              //排序 
  74. {
  75.      int i=n-1,j;
  76.      DuLinkList p;
  77.     for(bool change=true;i>0&&change;i--)
  78.     {
  79.        change=false;
  80.        for(j=0,p=DL->next;j<i;j++,p=p->next)
  81.             if(p->data>p->next->data) {
  82.                                       ElemType t=p->data;
  83.                                       p->data=p->next->data;
  84.                                       p->next->data=t;
  85.                                       change=true;}
  86.             }
  87. }       
  88. void MergeDuList(DuLinkList &La,DuLinkList &Lb,DuLinkList &Lc)      
  89. {     //已知La和Lb的元素按值非递减排列。
  90.   // 归并La和Lb得到新的双循环链表Lc,Lc的元素也按值非递减排列。
  91.      DuLinkList pa=La->next,pb=pb->next,pc=Lc=La;
  92.      while(pa!=La&&pb!=Lb)
  93.      {
  94.          if(pa->data<=pb->data) {pc->next=pa;pa->prior=pc;pc=pa;pa=pa->next;}
  95.          else {pc->next=pb;pb->prior=pc;pc=pb;pb=pb->next;}                  
  96.          }
  97.      while(pa!=La) {pc->next=pa;pa->prior=pc;pc=pa;pa=pa->next;}
  98.      while(pb!=Lb) {pc->next=pb;pb->prior=pc;pc=pb;pb=pb->next;}
  99.      pc->next=Lc;
  100.      Lc->prior=pc;
  101. }
  102.      
  103.      
  104. void Output(DuLinkList DL)       //输出 
  105. {
  106.      DuLinkList p=DL->next;
  107.     while(p!=DL)
  108.     {
  109.      cout<<p->data<<" ";
  110.      p=p->next;}
  111.     cout<<endl; }
  112.      
  113. void minus(DuLinkList &La,DuLinkList &Lb)  //La-Lb  
  114. {
  115.     DuLinkList q;
  116.     for(DuLinkList p=Lb->next;p!=Lb;p=p->next)
  117.     {
  118.         for(q=La->next;q!=La&&q->data!=p->data;q=q->next);
  119.         if(q!=La&&q->data==p->data) {
  120.                            q->prior->next=q->next;
  121.                            q->next->prior=q->prior; 
  122.                            free(q);} 
  123.      }
  124. }
  125. int main()
  126. {
  127. void minus(DuLinkList &La,DuLinkList &Lb);
  128. DuLinkList La,Lb,Lc;
  129. int num,num_a,num_b,pos_in,pos_del;
  130. ElemType insert_elem,delete_elem;
  131. cin>>num_a;
  132. InitDuList(La,num_a);
  133. Output(La);
  134. SortDuList(La,num_a);
  135. Output(La);
  136. cin>>num_b;
  137. InitDuList(Lb,num_b);
  138. Output(Lb);
  139. SortDuList(Lb,num_b);
  140. Output(Lb);
  141. minus(La,Lb);
  142. Output(La);
  143. MergeDuList(La,Lb,Lc);
  144. Output(Lc);
  145. cout<<"please input the position and the element you want to insert:";
  146. cin>>pos_in>>insert_elem;
  147. if(InsertDuList(Lc,pos_in,insert_elem)) cout<<"insert succeed"<<endl;
  148. Output(Lc);
  149. cout<<"please input the position  you want to delete:";
  150. cin>>pos_del;
  151. DeleteDuList(Lc,pos_del,delete_elem);
  152. Output(Lc);
  153. return 0;}
  154. 4
  155. 5 7 3 1
  156. 5 7 3 1
  157. 1 3 5 7
  158. 5
  159. 3 7 9 2 8
  160. 3 7 9 2 8
  161. 2 3 7 8 9
  162. 1 5
  163. 1 2 3 5 7 8 9
  164. please input the position and the element you want to insert:1
  165. 8
  166. insert succeed
  167. 8 1 2 3 5 7 8 9
  168. please input the position  you want to delete:4
  169. 8 1 2 5 7 8 9
  170. 请按任意键继续. . .
原创粉丝点击