线性表主函数+扩容

线性表主函数

#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
typedef struct vector {
    int* data;
    int size;
    int len;
}Vec;
int insert(Vec *v,int idx,int val) {
    if (!v)  return 0;
    if (idx<0||idx>v->len )return 0;
    if (v->len == v->size)return 0;
    memcpy(v->data +idx+1,v->data +idx,sizeof(int)*(v->len -idx));
    v->data[idx] = val;
    v->len++;
    return 1;
}
int erase(Vec*v,int idx) {
    if (!v)return 0;
    if (idx < 0 || idx >= v->len)return 0;
    memcpy(v->data +idx,v->data+idx+1,sizeof(int)*(v->len-idx-1));
    v->len--;
}
Vec* initial(int n){
    Vec * v = (Vec*)malloc(sizeof(Vec)+1);
    v->data = (int*)malloc(sizeof(int) * n);
    v->size = n;
    v->len = 0;
   printf("init Vector sucessfully ,size is %d\n",v->size);
    return v;
}
void showVec(Vec* v) {
    if (!v)return;
    printf("Vec:[");
    int i;
    for (i = 0; i < v->len; i++) {
        i&& printf(",");
        printf("%d", v->data[i]);
    }
    printf(":]\n");
}
void freeVec(Vec* v) {
    if (v) {
        free(v->data);
        free(v);
        printf("free Vector sucessfully \n");
    }
    return;
}
int main(int argc, char* argv[]) {
    Vec* v = initial(10);
    srand(time(0));
    int cnt = 20;
    while (cnt--) {
        int val = rand() % 100;
        int op = rand() % 2;
        int idx = rand() % (v->len + 3) - 1;
        switch (op) {
        case 0:
        case 1:
        case 2:
            printf("insert %d at %d ,res =%d\n",val,idx, insert(v, idx, val)); 
               break;
        case 3:
            printf("erase at %d ,res =%d\n",  idx, erase(v, idx));
            break;
        }
        showVec(v);
    }
    freeVec(v);
    return 0;
}

线性表扩容

#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
typedef struct vector {
    int* data;
    int size;
    int len;
}Vec;
int expand(Vec* v);
int insert(Vec *v,int idx,int val) {
    if (!v)  return 0;
    if (idx<0||idx>v->len )return 0;
    if (v->len == v->size) {
        if (!expand(v)) {
            return 0;
        }
    }
    memcpy(v->data +idx+1,v->data +idx,sizeof(int)*(v->len -idx));
    v->data[idx] = val;
    v->len++;
    return 1;
}
int erase(Vec*v,int idx) {
    if (!v)return 0;
    if (idx < 0 || idx >= v->len)return 0;
    memcpy(v->data +idx,v->data+idx+1,sizeof(int)*(v->len-idx-1));
    v->len--;
}
Vec* initial(int n){
    Vec * v = (Vec*)malloc(sizeof(Vec)+1);
    v->data = (int*)malloc(sizeof(int) * n);
    v->size = n;
    v->len = 0;
   printf("init Vector sucessfully ,size is %d\n",v->size);
    return v;
}
void showVec(Vec* v) {
    if (!v)return;
    printf("Vec:[");
    int i;
    for (i = 0; i < v->len; i++) {
        i&& printf(",");
        printf("%d", v->data[i]);
    }
    printf("]\n");
}
void freeVec(Vec* v) {
    if (v) {
        free(v->data);
        free(v);
        printf("free Vector sucessfully \n");
    }
    return;
}
int expand(Vec*v) {
    if (!v)return 0;
    int expsize = v->size;
    int* tmp = &(v->size);
    while (expsize) {
        tmp = (int*)realloc(v->data,sizeof(int)*(v->size +expsize));
        if (tmp)break;
        expsize >>= 2;
    }
    if (!tmp) {
        printf("expend failed~\n");
        return 0;
    }
    v->data = tmp;
    v->size += expsize;
    printf("expand sucessfully~\n");
    return 1;
}
int main(int argc, char* argv[]) {
    Vec* v = initial(1);
    srand(time(0));
    int cnt = 20;
    while (cnt--) {
        int val = rand() % 100;
        int op = rand() % 2;
        int idx = rand() % (v->len + 3) - 1;
        switch (op) {
        case 0:
        case 1:
        case 2:
            printf("insert %d at %d ,res =%d\n",val,idx, insert(v, idx, val)); 
               break;
        case 3:
            printf("erase at %d ,res =%d\n",  idx, erase(v, idx));
            break;
        }
        showVec(v);
    }
    freeVec(v);
    return 0;
}
上一篇:Lecture 08 Shading 2 (Shading, Pipeline and Texture Mapping)


下一篇:c++学习 lambda