算法2.1_C代码实现_《数据结构》_严蔚敏

// 2.4_顺序表中插入.cpp : 定义控制台应用程序的入口点。
//《数据结构》——严蔚敏;算法2.1实现
//功能:无重复合并两个线性表
//用到的方法:
//typedef struct List
//Status InitList_Sq(SqList &L)
//Status InsertList_Sq(SqList &L,int i,Elemtype e)
//Status GetElem(SqList &L,int i,Elemtype *e)
//Status compare(Elemtype e1,Elemtype e2)
//Status LocateElem(SqList &L,Elemtype e)
//int ListLength(SqList L)
//void Union(SqList &La,SqList Lb)
//void visit(Elemtype e)
//void print(Elemtype *c)
//Status ListTraverse(SqList L,void(*visit)(Elemtype*))
//实现代码如下:
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#define LIST_INIT_SIZE 100
#define LISTCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int Status;
typedef int Elemtype;
typedef struct List{
    Elemtype *elem;//空间基地址
    int length;//数据当前长度---相对于数据来说
    int Listsize;//空间容量---相对于内存来说
}SqList;
Status InitList_Sq(SqList &L){
    L.elem = (Elemtype *) malloc(LIST_INIT_SIZE*sizeof(Elemtype));
    if(!L.elem) exit(OVERFLOW);//系统级别错误是exit
    L.Listsize = LIST_INIT_SIZE;
    L.length = 0;
    return OK;
}
Status InsertList_Sq(SqList &L,int i,Elemtype e){
    if(i<1||i>L.length+1) return ERROR;//人为插入位置不当
    if(L.length>=L.Listsize){   //本身存储空间不够
        Elemtype *newbase = (Elemtype *)realloc(L.elem,(L.Listsize+LISTCREMENT)*sizeof(Elemtype));
        if(!newbase) exit(OVERFLOW);
        L.elem = newbase;
        L.Listsize+=LISTCREMENT;
    }
    int *q = &L.elem[i-1];//取出要插入的位置的值
    for(int *p=&L.elem[L.length-1];p >= q;p--) *(p+1)=*p;//向后移动
    *q = e;
    ++L.length;
    return OK;
}
Status GetElem(SqList &L,int i,Elemtype *e){
    if(i<1||i>L.length+1) return ERROR;//先判断参数i
    *e = *(L.elem+i-1);//由于要把所取到的数赋值返回给e,所以用指针
    return OK;
}
Status compare(Elemtype e1,Elemtype e2){
    if(e1 == e2) {
        return OK;
    }else{
        return ERROR;
    }
}
Status LocateElem(SqList &L,Elemtype e){
    int i=1;//所在数据的位置是对于人是从1开始,而L.elem[i-1]对于机器是从0开始
    Elemtype *p;
    p = L.elem;
    while(i<=L.length&&!compare(*p++,e))
        ++i;
    if(i<=L.length) return i;
    else return 0;
}
int ListLength(SqList L){
    return L.length;
}
void Union(SqList &La,SqList Lb){//将Lb和La合并
    int La_len = ListLength(La);  int Lb_len = ListLength(Lb);//调用方法返回长度;
    int e;
    for(int i =1;i<=Lb_len;i++){
        GetElem(Lb,i,&e);//把Lb中元素返回;
        if(!LocateElem(La,e)) InsertList_Sq(La,++La_len,e);//先判断La中是否有e元素,没有的话,插入e到La
    }
}
void visit(Elemtype e){
    printf("%d",e);//打印传进来的元素
}
void print(Elemtype *c){
   printf("%d",*c);
 }
Status ListTraverse(SqList L,void(*visit)(Elemtype*)){
    //元素依次调用函数visit();
    Elemtype *p;
    p = L.elem;
    int i;
    for(i=1;i<=L.length;i++){
        visit(p++);
    }
    printf("\n");
    return OK;
}
int _tmain(int argc, _TCHAR* argv[]){
    SqList La,Lb;
    int a[4]={3,5,8,11},b[7]={2,6,8,9,11,15,20};
    InitList_Sq(La);
    for(int j =1;j<=4;j++){
        InsertList_Sq(La,j,a[j-1]);
    }
    InitList_Sq(Lb);
    for(int j=1;j<=7;j++){
        InsertList_Sq(Lb,j,b[j-1]);
    }
    Union(La,Lb);
    printf("La= ");
    ListTraverse(La,print);
    getchar();
    return 0;
}


你可能感兴趣的:(算法)