chenchang12

导航

c顺序链表

#include <stdio.h>
#define MAXSIZE 10
#include <stdbool.h>

typedef struct{
    int data[MAXSIZE];
    int length;
}SqList;

bool initList(SqList *L){
    for(int i=0; i<MAXSIZE; i++){
        L->data[i] = 0;
        L->length = 0;
    }
}

int locateElem(SqList *L, int elem){
    for(int i=0; i<L->length; i++){
        if(L->data[i] == elem)
            return i+1;
    }
    return 0;
}

bool insertList(SqList *L, int location, int elem){
    if(location>10 || location<1 || L->length>=10 || location-1 > L->length)
        return false;
    for(int i=L->length; i>=location; i--){
        L->data[i] = L->data[i-1];
    }
    L->data[location-1] = elem;
    L->length++;
    return true;

}

int deleteElem(SqList *L, int location){
    if(location>L->length || location<1)
        return 0;
    int e = L->data[location-1];
    for(int i=location; i<=L->length-1; i++){
        L->data[i-1] = L->data[i];
    }
    L->length--;
    return e;
}

int main()
{
    SqList L;
    initList(&L);
    bool insert = insertList(&L, 1,4);
    int location = locateElem(&L, 4);
    int delet = deleteElem(&L, 1);
    printf("%d",location);

}
;

posted on 2020-08-04 20:44  chenchang12  阅读(109)  评论(0编辑  收藏  举报