双向循环链表
来源:互联网 发布:标准普通话发音软件 编辑:程序博客网 时间:2024/05/02 02:37
- #include<iostream>
- #include<malloc.h>
- #include<iostream>
- #include<malloc.h>
- #include<stdlib.h>
- using namespace std;
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- typedef int Status;
- typedef int ElemType;
- typedef struct DuLNode
- {
- ElemType data;
- struct DuLNode *prior;
- struct DuLNode *next;
- }DuLNode,*DuLinkList;
- Status InitDuList(DuLinkList &DL,int n)
- {
- if(n<1) return ERROR;
- DuLinkList p,q;
- DL=(DuLinkList)malloc(sizeof(DuLNode));
- for(int i=0;i<n;i++)
- {
- p=(DuLinkList)malloc(sizeof(DuLNode));
- cin>>p->data;
- if(!i) {
- DL->next=p;
- p->prior=DL;
- q=p;}
- else {
- q->next=p;
- p->prior=q;
- q=p;}
- }
- p->next=DL;
- DL->prior=p;
- return OK;
- }
- DuLinkList GetElemP_DuL(DuLinkList DL,int i)
- {
- if(i<0) exit(1);
- DuLinkList p=DL;
- int j=0;
- for(;p->next!=DL&&j<i;j++)
- p=p->next;
- if(p->next==DL&&j<i) return NULL;
- return p;
- }
- Status InsertDuList(DuLinkList &DL,int i,ElemType e)
- {
- DuLinkList p=GetElemP_DuL(DL,i),q;
- if(!p) return ERROR;
- if(!(q=(DuLinkList)malloc(sizeof(DuLNode)))) exit(OVERFLOW);
- q->data=e;
- q->prior=p->prior;
- q->next=p;
- p->prior->next=q;
- p->prior=q;
- return OK;
- }
-
- Status DeleteDuList(DuLinkList &DL,int i,ElemType &e)
- {
- DuLinkList p=GetElemP_DuL(DL,i);
- if(!p) return ERROR;
- e=p->data;
- p->prior->next=p->next;
- p->next->prior=p->prior;
- free(p);
- return OK;
- }
- void SortDuList(DuLinkList &DL,int n)
- {
- int i=n-1,j;
- DuLinkList p;
- for(bool change=true;i>0&&change;i--)
- {
- change=false;
- for(j=0,p=DL->next;j<i;j++,p=p->next)
- if(p->data>p->next->data) {
- ElemType t=p->data;
- p->data=p->next->data;
- p->next->data=t;
- change=true;}
- }
- }
- void MergeDuList(DuLinkList &La,DuLinkList &Lb,DuLinkList &Lc)
- {
-
- DuLinkList pa=La->next,pb=pb->next,pc=Lc=La;
- while(pa!=La&&pb!=Lb)
- {
- if(pa->data<=pb->data) {pc->next=pa;pa->prior=pc;pc=pa;pa=pa->next;}
- else {pc->next=pb;pb->prior=pc;pc=pb;pb=pb->next;}
- }
- while(pa!=La) {pc->next=pa;pa->prior=pc;pc=pa;pa=pa->next;}
- while(pb!=Lb) {pc->next=pb;pb->prior=pc;pc=pb;pb=pb->next;}
- pc->next=Lc;
- Lc->prior=pc;
- }
-
-
- void Output(DuLinkList DL)
- {
- DuLinkList p=DL->next;
- while(p!=DL)
- {
- cout<<p->data<<" ";
- p=p->next;}
- cout<<endl; }
-
- void minus(DuLinkList &La,DuLinkList &Lb)
- {
- DuLinkList q;
- for(DuLinkList p=Lb->next;p!=Lb;p=p->next)
- {
- for(q=La->next;q!=La&&q->data!=p->data;q=q->next);
- if(q!=La&&q->data==p->data) {
- q->prior->next=q->next;
- q->next->prior=q->prior;
- free(q);}
- }
- }
- int main()
- {
- void minus(DuLinkList &La,DuLinkList &Lb);
- DuLinkList La,Lb,Lc;
- int num,num_a,num_b,pos_in,pos_del;
- ElemType insert_elem,delete_elem;
- cin>>num_a;
- InitDuList(La,num_a);
- Output(La);
- SortDuList(La,num_a);
- Output(La);
- cin>>num_b;
- InitDuList(Lb,num_b);
- Output(Lb);
- SortDuList(Lb,num_b);
- Output(Lb);
- minus(La,Lb);
- Output(La);
- MergeDuList(La,Lb,Lc);
- Output(Lc);
- cout<<"please input the position and the element you want to insert:";
- cin>>pos_in>>insert_elem;
- if(InsertDuList(Lc,pos_in,insert_elem)) cout<<"insert succeed"<<endl;
- Output(Lc);
- cout<<"please input the position you want to delete:";
- cin>>pos_del;
- DeleteDuList(Lc,pos_del,delete_elem);
- Output(Lc);
- return 0;}
- 4
- 5 7 3 1
- 5 7 3 1
- 1 3 5 7
- 5
- 3 7 9 2 8
- 3 7 9 2 8
- 2 3 7 8 9
- 1 5
- 1 2 3 5 7 8 9
- please input the position and the element you want to insert:1
- 8
- insert succeed
- 8 1 2 3 5 7 8 9
- please input the position you want to delete:4
- 8 1 2 5 7 8 9
- 请按任意键继续. . .