#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
ElemType *elem;
int length;
int listsize;
} SqList;
Status InitList(SqList *L)
{
L->elem=(ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
if(!L->elem) exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
Status DestroyList(SqList *L)
{
if(!L->elem) return ERROR;
L->elem=NULL;
return OK;
}
Status ListEmpty(SqList L)
{
if(!L.length) return TRUE;
else return FALSE;
}
Status ListLength(SqList L)
{
return L.length;
}
Status ClearList(SqList *L)
{
if(!ListEmpty(*L))
L->length=0;
return OK;
}
Status GetElem(SqList L, int i, ElemType *e)
{
if(i<1 || i>L.length) return ERROR;
*e=L.elem[i-1];
return OK;
}
Status equal(ElemType a, ElemType b)
{
if(a==b) return TRUE;
else return FALSE;
}
Status LocateElem(SqList L, ElemType e, Status (*compare)(ElemType, ElemType))
{
int i=1;
while(i<=L.length && !compare(L.elem[i-1], e)) i++;
if(i<=L.length) return i;
else return ERROR;
}
Status PriorElem(SqList L, ElemType cur_e, ElemType *pre_e)
{
int i=LocateElem(L, cur_e, equal);
if(i>1)
{
GetElem(L, i-1, pre_e);
return OK;
}
return ERROR;
}
Status NextElem(SqList L, ElemType cur_e, ElemType *next_e)
{
int i=LocateElem(L, cur_e, equal);
if(i && i<L.length)
{
GetElem(L, i+1, next_e);
return OK;
}
return ERROR;
}
Status ListInsert(SqList *L, int i, ElemType e)
{
if(i<1 || i>(L->length+1)) return ERROR;
if(L->length==L->listsize)
{
ElemType *newbase;
newbase=(ElemType *)realloc(L->elem, (L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
ElemType *p, *q;
q=&L->elem[i-1];
for(p=&L->elem[L->length-1]; p>=q; p--) *(p+1)=*p;
*q=e;
(L->length)++;
return OK;
}
Status ListDelete(SqList *L, int i, ElemType *e)
{
if(i<1 || i>(L->length+1)) return ERROR;
ElemType *p, *q;
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;
}
void Union(SqList *La, SqList Lb)
{
ElemType e;
int i;
int La_len=ListLength(*La);
int Lb_len=ListLength(Lb);
for(i=1; i<=Lb_len; i++)
{
GetElem(Lb, i, &e);
if(!LocateElem(*La, e, equal))
ListInsert(La, ++La_len, e);
}
}
void MergeList(SqList La, SqList Lb, SqList *Lc)
{
InitList(Lc);
int i=1, j=1;
int k=0;
ElemType ai, bj;
int La_len=ListLength(La);
int Lb_len=ListLength(Lb);
while((i<=La_len)&&(j<=Lb_len))
{
GetElem(La, i, &ai);
GetElem(Lb, j, &bj);
if(ai<=bj)
{
ListInsert(Lc, ++k, ai);
i++;
}
else
{
ListInsert(Lc, ++k, bj);
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);
}
}
/**********O(La.length+Lb.length)**********/
void MergeList_Sq(SqList La, SqList Lb, SqList *Lc)
{
ElemType *pa=La.elem;
ElemType *pb=Lb.elem;
Lc->listsize=Lc->length=La.length+Lb.length;
ElemType *pc=Lc->elem=(ElemType *)malloc(sizeof(ElemType)*Lc->listsize);
if(!Lc->elem) exit(OVERFLOW);
int *pa_last=La.elem+La.length-1;
int *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++;
}
void PrintList(SqList L)
{
int i;
for(i=1; i<=L.length; i++)
printf("%d ", L.elem[i-1]);
printf("\n");
}
void CreateList(SqList *L)
{
InitList(L);
int Num, i;
ElemType e;
scanf("%d", &Num);
for(i=1; i<=Num; i++)
{
scanf("%d", &e);
ListInsert(L, i, e);
}
}
int main()
{
SqList La, Lb, Lc;
CreateList(&La);
CreateList(&Lb);
MergeList_Sq(La, Lb, &Lc);
PrintList(Lc);
return 0;
}
线性表(动态数组)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 对于线性结构的顺序表而言,特点: ···1.添加和删除元素,时间复杂度是O(n),因为要移动元素.。 ···(1)...
- 本文行文思路结构 一. 线性表 线性结构是数据结构中三种基本结构之一. 而线性结构的特点是:在数据元素的非空有限集...