数据结构之 线性表 数组实现

1 前言

线性表是由0个或者多个具有相同类型的元素组成的一个有序序列,通常用逗号进行分隔,比如 a1,a2,a3,a4.......an

表示一个线性表,嗯,看上去像高中学过的等比等差数列。

2.线性表的两种表现形式。

    1顺序表示(数组)

    2链式表示

3.线性表的典型操作

   1 InitList(& L)   //线性表的初始化

   2 Insert(x,p,L) // 将x 插入到表L的p处

   3 Locate(x,L)  // 返回x在L的位置

   4 Delete(p,L) //删除位置p处的元素

   5 DeleteVal(L,x) //删除值为x的元素

#include <stdio.h>
#include <malloc.h>
 
 
typedef int Elementtype;
 
 
struct LISTA{
    Elementtype *elements; //存放元素的数组
    int length; //已经使用了多少元素
    int size;  //数组的容量
};
 
 
typedef struct LISTA LIST;
 
 
//初始化LIST 为NULL
void InitList(LIST *L,int s){
    if(s <= 0){
        printf("s 非法 <= 0");
        exit(1);
    }
    else{
        L->length = 0;
        L->size = s;
        L->elements = (Elementtype *)malloc(s*sizeof(Elementtype));
        if (L->elements)
            printf("malloc success\n");
        else
            printf("malloc fails\n");
    }
}
//在LIST表中插入数据
void InsertLastList(LIST *L,Elementtype x){
    if(L->size == L->length){
        printf("the list is full\n");
        L->elements = (Elementtype *)realloc(L->elements,2*L->size*sizeof(Elementtype));
        L->size = L->size*2;
 
 
    }
 
 
    L->elements[L->length] = x;
    L->length ++ ;
 
 
}
//返回List的长度
int ListLength(LIST *L){
    return L->length;
}
 
 
//返回List的长度
int ListSize(LIST *L){
    return L->size;
}
 
 
//打印List
void PrintList(LIST *L){
    if(L->length == 0)
        printf("LIST IS NULL");
    for (int i = 0;i <L->length;i++)
        printf("Print list = %d\n",L->elements[i]);
}
//清除线性表
void clearList(LIST *L){
    if (L->elements != NULL){
        free(L->elements);
        L->elements =0;
        L->size = 0;
        L->length = 0;
    }
}
// 返回第几个位置的元素
Elementtype getElem(LIST *L,int pos){
    if(pos<1||pos>L->size){
        printf("the pos > size or pos < 1");
        return;
    }
    else
        return L->elements[pos-1];
 
 
}
//指定位置的插入数据
 
 
void InsertPosList(LIST *L,int pos,int x)
{
    if(pos <1 || pos>L->size){
        printf("the size is %d and the pos is %d",L->size,pos);
        return;
    }
 
 
    if(L->size == L->length){
        printf("the list is full");
        return ;
    }
 
 
    for(int i = L->length ;i>=pos;i--)
        L->elements[i] =L->elements[i - 1];
    L->elements[pos - 1] = x;
    L->length++;
 
 
}
//删除指定位置的参数
void DeleteEleFromList(LIST *L,int pos){
    if (pos < 1||pos>L->length){
        printf("the element is not exsit\n");
        return;
    }
    for(int i = pos -1;i<= L->length;i++)
        L->elements[i] = L->elements[i+1];
    L->length--;
}
 
 
void DeleteVal(LIST *L,int x){
   int l = L->size;
   for(int i = 0;i<=l;i++){
       if (L->elements[i] == x){
           DeleteEleFromList(L,i+1);
           i--;
       }
       else
           continue;
   }
 
 
}
 
 
int main()
{
    int a[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,8,8};
 
 
    LIST L;
    InitList(&L,5);
    for (int i =0;i<15;i++)
        InsertLastList(&L,a[i]);
 
 
    printf("LIST LENGTH = %d Size = %d\n",ListLength(&L),ListSize(&L));
    InsertPosList(&L,8,88);
    DeleteEleFromList(&L,7);
    PrintList(&L);
    printf("the pos of 4 is %d \n",getElem(&L,4));
    DeleteVal(&L,8);
    //clearList(&L);
    PrintList(&L);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_31607113/article/details/79877952
今日推荐