基础合并:
- void MergeList(List La,List Lb,List &Lc)
- {
- InitList(Lc);
- i=j=1;k=0;
- La_len=ListLength(La);
- Lb_len=ListLength(Lb);
- while((i<=La_len)&&(j<=Lb_len))
- {
- GetElem(La,i,ai);
- GetElem(Lb,i,bi);
- if(ai<=bi)
- {
- ListInsert(Lc,++k,ai);
- i++;
- }
- else
- {
- ListInsert(Lc,++k,bi);
- j++;
- }
- }
- while(i<=La_len)
- {
- GetElem(La,i++,ai);
- ListInsert(Lc,++k,ai);
- }
- while(j<=Lb_len)
- {
- GetElem(Lb,j++,bj);
- ListInsert(Lc,++k,bj);
- }
- }
顺序合并:
- #define List_INIT_SIZE=100
- #define LISTINCREMENT 10
- typedef struct
- {
- ElemType *elem;
- int length;
- int listsize;
- }SqList;
- void MergeList_Sq(SqList La,SqList Lb,SqList &Lc)
- {
- pa=La.elem;
- pb=Lb.elem;
- Lc.listsize=Lc.length=La.length+Lb.length;
- pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
- if(!Lc.elem)exit(OVERFLOW);
- pa_last=La.elem+La.length-1;
- pb_last=Lb.elem+Lb.length-1;
- while(pa<=pa_last&&pb<=pb_last)
- {
- if(*pa<=*pb)*pc++=*pa++;
- else *pc++=*pb++;
- }
- while(pa<=pa_last)*pc++=*pa++;
- while(pb<=pb_last)*pc++=*pb++;
- }
链表合并:
- typedef struct LNode
- {
- ElemType data;
- struct LNode *next;
- }LNode,*LinkList;
- void Merge_L(LinkList &La,LinkList &Lb,LinkList &Lc)
- {
- pa=La->next;pb=Lb->next;
- Lc=pc=La;
- while(pa&&pb)
- {
- if(pa->data<=pb->data)
- {
- pc->next=pa;
- pc=pa;
- pa=pa->next;
- }
- else
- {
- pc->next=pb;
- pc=pb;
- pb=pb->next;
- }
- }
- pc->next=pa?pa:pb;
- free(Lb);
- }
顺序插入:
- #define List_INIT_SIZE=100
- #define LISTINCREMENT 10
- typedef struct
- {
- ElemType *elem;
- int length;
- int listsize;
- }SqList;
- void ListInsert_Sq(SqList &L,int i,ElemType e)
- {
- if(i<1||i>L.length+1)return ERROR;
- if(L.length>L.listsize)
- {
- newbase=(ElemType*)realloc(l.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
- if(!newbase)exit(OVERFLOW);
- L.elem=newbase;
- L.listsize+=LISTINCREMENT;
- }
- q=&(L.elem[i-1]);
- for(p=&(l.elem[L.length-1]);p>q;--p)
- *(p+1)=*p;
- *q=e;
- ++L.length;
- }
链表插入:
- typedef struct LNode
- {
- ElemType data;
- struct LNode *next;
- }LNode,*LinkList;
- void ListInsert_L(LinkList &L,int i,int e)
- {
- p=L;j=0;
- while(p&&j<i-1)
- {
- p=p->next;
- j++;
- }
- if(!p||j>i-1)
- return ERROR;
- s=(LinkList)malloc(sizeof(LNode));
- s->data=e;
- s->next=p->next;
- p->next=s;
- }
顺序删除:
- #define List_INIT_SIZE=100
- #define LISTINCREMENT 10
- typedef struct
- {
- ElemType *elem;
- int length;
- int listsize;
- }SqList;
- int ListDelete_Sq(Sqlist &L,int i,ElemType &e)
- {
- if((i<1)||(i>L.length))return ERROR;
- p=&(L.elem[i-1]);
- e=*p;
- q=L.elem+L.length-1;
- for(++p;p<q;++p)
- *(p-1)=*p;
- --L.length;
- return OK;
- }
链表删除:
- typedef struct LNode
- {
- ElemType data;
- struct LNode *next;
- }LNode,*LinkList;
- void ListDelete_L(LinkList &L,int i;ElemType &e)
- {
- p=L;j=0;
- while(p->next&&j<i-1)
- {
- p=p->next;
- j++;
- }
- if(!(p->next)||j>i-1)return ERROR;
- q=p->next;
- p->next=q->next;
- e=q->data;
- free(q);
- return OK;
- }
求A∪B的总体代码:
- void union(List &La,List Lb)
- {
- La_len=Listlength(La);
- Lb_len=Listlength(Lb);
- for ( i=1;i<=Lb.len;i++);
- {
- GetElem(Lb,i,e); //获得线性表Lb中i的元素e,并返回;
- if(!Location(La,e,equal)) //如果不相等,则插入;
- ListInsert(La,++La_len,e);
- }
- }
对线性表的初始化:
- #define List_INIT_SIZE=100
- #define LISTINCREMENT 10
- typedef struct
- {
- ElemType *elem;
- int length;
- int listsize;
- }SqList;
- void InitList_Sq(SqList &La)
- {
- La.elem=(ElemType*)malloc(LIST_INTI_SIZE*sizeof(ElemType));
- if(!La.elem)exit(OVERFLOW);
- La.length=0;
- L.listsize=LIST_INTI_SIZE; //初始化存储容量
- }
查找位序:
- int LocateElemd_Sq(SqList L.ElemType e,int (*compare)(ElemType,ElemType))
- {
- i=1;
- p=L.elem;
- while(i<L.length&&!(*compare)(*p++,e))++i;// 如果e不满足函数compare,则继续向后移动
- if(i<L.length)return i;
- else return 0;
- }
链表中获得第i个位序的值:
- void GetElem_L(LinkList L,int i,int &e)
- {
- p=L->next;j=1;
- while(p&&j<i)
- {
- p=p->next;
- ++j;
- }
- if(!p||j>i)return ERROR;
- e=p->data;
- }
逆向建立单链表:
- void create_list(ListLink &L,int n)
- {
- //逆序位输入n个值,建立带头结点的单链表L
- L=(LinkLink)malloc(sizeof(LNode));
- L->next=NULL;
- for(i=n;i>0;--i)
- {
- p=(LinkList)malloc(sizeof(LNode));
- scanf(&p->data);
- p->next=L->next;
- L->next=p:
- }
- }