首页 > 其他 > 详细

c顺序链表

时间:2020-08-05 00:09:05      阅读:87      评论:0      收藏:0      [点我收藏+]
#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);

}
;

c顺序链表

原文:https://www.cnblogs.com/Archerme/p/13435808.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!