栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构——手写线性表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构——手写线性表

手写线性表,代码是函数形式的,每个函数都是实现一部分功能
实现的功能有顺序表的初始化,扩容,插入,删除,按值查找,按位查找

#include
#define MAX_SIZE 10
using namespace std;
typedef struct Vector
{
    int *data;
    int maxsize;
    int length;
}Vector;

void Vector_Init(Vector &L) //初始化
{
    L.maxsize=MAX_SIZE;
    L.data=(int*)malloc(sizeof(int)*L.maxsize);
    L.length=0;
}

void Vector_Extend(Vector &L,int len) //扩容
{
    int* p=L.data;
    L.data=(int*)malloc(sizeof(int)*(L.maxsize+len));
    for(int i=0;i
        L.data[i]=p[i];
    }

    L.maxsize=L.maxsize+len;
    free(p);
}

void Vector_Input(Vector &L) //初始化步骤:给data中的每一位赋 1
{
    for(int i=0;i
        L.data[i]=1;
        L.length++;
    }
}

bool Vector_Insert(Vector &L,int pos,int val) //插入,pos表示位序(位序从1开始)
{
    if(pos<1||pos>L.length+1) return false;
    if(L.length==L.maxsize){
        Vector_Extend(L,2);
        cout<<"扩容成功"<=pos;i--){
        L.data[i]=L.data[i-1];
    }
    L.data[pos-1]=val;
    L.length++;
    return true;
}
bool Vector_Erase(Vector &L,int pos,int &val) //插入,pos表示位序(位序从1开始)
{
    if(pos<1||pos>L.length) return false;
    if(L.length==0) return false;

    val=L.data[pos-1];
    for(int i=pos-1;i
        L.data[i]=L.data[i+1];
    }
    L.length--;
    return true;
}

int Getval(Vector &L,int pos) //按位查找,pos表示位序(位序从1开始)
{
    if(pos<1||pos>L.length) return -1;
    if(L.length==0) return -1;
    return L.data[pos-1];
}

int LockVal(Vector &L,int val) //按值查找,返回的是位序(位序从1开始)
{
    for(int i=0;i
        if(L.data[i]==val)
            return i+1;
    }
    return -1;
}
void Vector_Output(Vector &L) //输出data中所有的元素
{
    printf("data[%d]=",L.length);
    for(int i=0;i
    Vector L;
    Vector_Init(L);
    Vector_Input(L);
    Vector_Output(L);

    if(Vector_Insert(L,1,5))
        Vector_Output(L);
    if(Vector_Insert(L,1,6))
        Vector_Output(L);
    if(Vector_Insert(L,1,7))
        Vector_Output(L);
    if(Vector_Insert(L,14,8))
        Vector_Output(L);
    if(Getval(L,12)>0)
        cout<<"第12位元素是"<0)
        cout<<"有元素8,位于"< 

下面是另外一种写法
功能包含插入,删除,清空,打印,元素个数,表长,realloc函数扩容线性表

#include
using namespace std;
typedef struct MyVector
{
    int *data;
    int vecsize;
    int number;
}Vector;

Vector* Init(int N)
{
    Vector* vec=(Vector*)malloc(sizeof(Vector));
    vec->data=(int*)malloc(sizeof(int)*N);
    vec->vecsize=N;
    vec->number=0;
}

bool Extend(Vector *vec) {
    int new_size = vec->vecsize * 2;
    int *p = (int *)realloc(vec->data, sizeof(int) * new_size);
    if (p == NULL) return 0;
    vec->vecsize = new_size;
    vec->data = p;
    return true;
}

bool VecInsert(Vector* vec,int pos,int val)
{
    if(vec==NULL) return false;
    if(pos<0||pos>vec->number) return false;
    if(vec->number==vec->vecsize)
        if (!Extend(vec)) return 0;
        else printf("successn");

    for(int i=vec->number;i>pos;i--){
        vec->data[i]=vec->data[i-1];
    }
    vec->data[pos]=val;
    vec->number++;
    return true;
}

bool VecErase(Vector* vec,int pos)
{
    if(vec==NULL) return false;
    if(pos<0||pos>=vec->number) return false;
    if(vec->number==0) return false;


    for(int i=pos;inumber;i++){
        vec->data[i]=vec->data[i+1];
    }
    vec->number--;
    return true;
}

bool VecClear(Vector* vec)
{
    if(vec==NULL) return false;
    free(vec->data);
    free(vec);
    return true;
}

void VecOutput(Vector *vec)
{
    cout<<"data = ";
    for(int i=0;inumber;i++){
        cout<data[i]<<" ";
    }
    cout<

    Vector* vec =Init(2);
    cout<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026515.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号