向量的基本运算

vector.h

#ifndef vector_h__
#define vector_h__
# include <stdio.h>
# include <malloc.h>

enum boolean {FALSE, TRUE};
typedef enum boolean BOOL;
typedef int ElementType;

typedef struct vector_def{
    ElementType*  elements;
    int ArraySize;
    int VectorLength;
}Vector;
/*共九个函数*/
void GetArray(Vector* V);
void InitVector(Vector *V,int sz);
ElementType* GetNode(Vector *V, int i);
void FreeVector(Vector * V);
int Find(Vector * V, ElementType x);
BOOL insert (Vector * V, ElementType x, int i);
BOOL Remove(Vector * V,int i);
Vector* Union(Vector * Va, Vector* Vb);
Vector* Intersection(Vector* Va, Vector *Vb);
#endif // vector_h__

vector.c

# include "vector.h"

void GetArray(Vector* V){
    /* 数组获取内存空间*/
    V->elements = (ElementType *) malloc(sizeof(ElementType)* V->ArraySize);
    if (V->elements == NULL)
        printf("Mempry Allocation Error!
");
}

void InitVector(Vector *V,int sz){
    /*初始化函数, 建立一个最大长度为sz的数组*/
    if (sz > 0){
        V->ArraySize = sz;
        V->VectorLength = 0;
        GetArray(V);
    }
    else{
        printf("Invalid Array Size.
");
    }
}

ElementType* GetNode(Vector *V, int i){
    /*取向量中第i个结点的值。 
    若第 i 个结点存在,则返回该结点的值。
    否则返回NULL
    */
    return (i<0 || i>=V->VectorLength)? NULL: V->elements+i;
}

void FreeVector(Vector * V){
    /*
    释放向量的内存空间
    */
    free(V->elements);
}

int Find(Vector * V, ElementType x){
    /*
        查找值为x的结点。若找到,则返回站点序号,否则返加-1。
    */
    int i;
    for (i=0; i<V->VectorLength; i++){
        if (V->elements[i] == x) 
            return i;
    }
    return -1;
}

BOOL insert (Vector * V, ElementType x, int i){
    /*在向量第 i 个位置插入值为x 的新结点。若插入成功,则返回TRUE;否则返回FALSE*/
    int j;

    if (V->VectorLength == V->ArraySize){
        printf("overflow
");
        return FALSE;
    }
    else if (i<0 || i>V->VectorLength){
        printf("Position error
");
        return FALSE;
    }
    else{
        // 此处length -1 , 因为有一个时,length == 1
        for (j=V->VectorLength-1; j>=i; j--)
            V->elements[j+1] = V->elements[j];
        V->elements[i] = x;
        V->VectorLength++;
    }
    return TRUE;
}

BOOL Remove(Vector * V,int i){
    /*删除第 i 个结点。成功返回TRUE;否则返回FALSE;*/
    int j;
    if (V->VectorLength == 0){
        printf("Vector is empty.
");
        return FALSE;
    }
    else if (i<0 || i>V->VectorLength-1){
        printf("Position Error.");
        return FALSE;
    }
    else{
        for (j=i; j<V->VectorLength-1; j++)
            V->elements[j] = V->elements[j+1];
        V->VectorLength--;
    }
    return TRUE;
}

Vector* Union(Vector * Va, Vector* Vb){
    /*把向量Va Vb 合并到 Vc 中, 重复元素只留一个*/
    int m, n, i, k, j;
    ElementType x;
    Vector *Vc = (Vector * )malloc(sizeof(Vector));

    n = Va->VectorLength;
    m = Vb->VectorLength;

    InitVector(Vc, m+n);

    j = 0;
    for (i=0; i<n; i++){
        x = *GetNode(Va, i);
        insert(Vc, x, j);
        j++;

    }

    for (i=0; i<m; i++){
        x = *GetNode(Vb, i);
        k = Find(Vc, x);
        if (k == -1){
            insert(Vc, x, j);
            j++;
        }
    }
    return Vc;
}

Vector* Intersection(Vector* Va, Vector *Vb){
    /*求 Va 和 Vb 中相同的元素,并存入Vc*/
    int m, n, i, k ,j;
    ElementType x;
    Vector * Vc = (Vector*)malloc(sizeof(Vector));

    n = Va->VectorLength;
    m = Vb->VectorLength;
    InitVector(Vc, (m>n)?n:m);

    i=0; j = 0;
    while (i<m){
        x = *GetNode(Vb, i);
        k = Find(Va, x);
        if (k != -1){
            insert(Vc, x, j);
            j++;
        }
        i++;
    }
    return Vc;
}

main.c

#include "vector.h"
#define M 6
/*
    void GetArray(Vector* V);
    void InitVector(Vector *V,int sz);
    ElementType* GetNode(Vector *V, int i);
    void FreeVector(Vector * V);
    int Find(Vector * V, ElementType x);
    BOOL insert (Vector * V, ElementType x, int i);
    BOOL Remove(Vector * V,int i);
    Vector* Union(Vector * Va, Vector* Vb);
    Vector* Intersection(Vector* Va, Vector *Vb)
*/
void printout(Vector * V){
    int i;
    for (i=0; i<V->VectorLength; i++)
        printf("%d ", V->elements[i]);
    printf("
");
}
int main(){

    Vector* Va = (Vector*)malloc(sizeof(Vector));
    Vector* Vb = (Vector*)malloc(sizeof(Vector));
    Vector* Vc = (Vector*)malloc(sizeof(Vector));

    puts("初始化,,,,");
    InitVector(Va, M);
    puts("依次插入元素");
    insert(Va, 10, 0);
    insert(Va, 20, 1);
    insert(Va, 30, 2);
    insert(Va, 40, 3);
    insert(Va, 50, 4);
    insert(Va, 80, 2);
    printf("Va:");
    printout(Va);

    Remove(Va, 2);
    printf("Va:");
    printout(Va);

    puts("初始化,,,,");
    InitVector(Vb, M);
    puts("依次插入元素");
    insert(Vb, 10, 0);
    insert(Vb, 12, 1);
    insert(Vb, 13, 2);
    insert(Vb, 40, 3);
    insert(Vb, 50, 4);
    insert(Vb, 18, 2);
    printf("Vb:");
    printout(Vb);

    printf("
交并集操作

");
    Vc = Union(Va, Vb);
    printf("Union:
");
    printout(Vc);

    Vc = Intersection(Va, Vb);
    printf("Intersection:
");
    printout(Vc);

    FreeVector(Va);
    FreeVector(Vb);
    FreeVector(Vc);
    return 0;
}

运行结果:
VS2010

原文地址:https://www.cnblogs.com/laohaozi/p/12538410.html