C语言:数据结构之顺序表

#ifndef _SEQLIST_H
#define _SEQLIST_H

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define N 100
typedef int datatype;

typedef struct{
    datatype data[N];
    int last;
}seqlist;

seqlist *seqlistCreate();
void seqlistInster(seqlist *s,datatype value);
void seqlistPrint(seqlist *s);
datatype seqlistDel(seqlist *s);
bool seqlistEmpty(seqlist *s);
bool seqlistFull(seqlist *s);
void seqlistModityDataByData(seqlist *s,datatype oldValue,datatype newValue);
void seqlistModityDataByPos(seqlist *s,int Pos,datatype value);
datatype seqliseSearchDataByPos(seqlist *s,int Pos);
seqlist* seqlistSearchPosByData(seqlist *s,datatype value);
void seqlistInsterDataByPos(seqlist *s,int Pos,datatype value);
datatype seqlistDelDataByPos(seqlist *s,int Pos);

#endif

seqlist *seqlistCreate(){
    seqlist *s=(seqlist *)malloc(sizeof(seqlist));
    s->last=-1;
    return s;
}

void seqlistInster(seqlist *s,datatype value){
    if(seqlistFull(s)){
        printf("the seqlist is full\n");
        return ;
    }
    s->last++;
    s->data[s->last] = value;
    return ;
}

void seqlistPrint(seqlist *s){
    int i;
    for(i=0;i<=s->last;i++){
        printf("%d ",s->data[i]);
    }
    putchar(10);
    return ;
}

datatype seqlistDel(seqlist *s){
    if(seqlistEmpty(s)){
        printf("the seqlist is empty\n");
        return (datatype)-1;
    }
    datatype del=s->data[s->last];
    s->last--;
    return del;
}
void seqlistModityDataByData(seqlist *s,datatype oldValue,datatype newValue){
    if(seqlistEmpty(s)){
        printf("the seqlist is empty\n");
        return ;
    }
    bool iFind=false;
    int i;
    for(i=0;i<=s->last;i++){
        if(s->data[i]==oldValue){
            s->data[i]=newValue;
            iFind=true;
        }
    }
    if(iFind==false){
        printf("the value you choosed(%d) is not exist\n", oldValue);
    }
    return ;

}


bool seqlistEmpty(seqlist *s){
    return s->last<0?true:false;
}

bool seqlistFull(seqlist *s){
    return s->last==N-1?true:false;
}

void seqlistModityDataByPos(seqlist *s,int Pos,datatype value){
    if(seqlistEmpty(s)){
        printf("the seqlist is empty\n");
        return ;
    }
    if(Pos>s->last){
        printf("the pos you choosed(%d) is not exist\n", Pos);
        return ;
    }
    s->data[Pos]=value;
    return ;
}

datatype seqliseSearchDataByPos(seqlist *s,int Pos){
    if(seqlistEmpty(s)){
        printf("the seqlist is empty\n");
        return (datatype)-1;
    }
    if(Pos>s->last||Pos<0){
        printf("the pos you choosed(%d) is not exist\n", Pos);
        return (datatype)-1;
    }
    return s->data[Pos];
}

seqlist* seqlistSearchPosByData(seqlist *s,datatype value){
    if(seqlistEmpty(s)){
        printf("the seqlist is empty\n");
        return (seqlist*)-1;
    }
    seqlist *allPos=(seqlist*)malloc(sizeof(N*sizeof(int)));
    allPos=seqlistCreate();
    int n=0;
    bool iFind=false;
    for(int i=0;i<=s->last;i++){
        if(s->data[i]==value){
            seqlistInster(allPos,i);
            iFind=true;
        }
    }
    if(iFind==false){
        printf("the date you choosed(%d) is not exist\n",value);
        return (seqlist*)-1;
    }

    return allPos;
}

void seqlistInsterDataByPos(seqlist *s,int Pos,datatype value){
    int i;
    if(seqlistFull(s)){
        printf("the seqlist is full\n");
        return ;
    }
    if(Pos>s->last+1||Pos<0){
        printf("the pos you choosed(%d) is invaild\n",Pos);
        return ;
    }
    s->last++;
    for(i=s->last;i>Pos;i--){
        s->data[i]=s->data[i-1];
    }
    s->data[Pos]=value;
    return ;
}

datatype seqlistDelDataByPos(seqlist *s,int Pos){
    int i;
    if(seqlistEmpty(s)){
        printf("the seqlist is empty\n");
        return (datatype)-1;
    }
    if(Pos>s->last||Pos<0){
        printf("the pos you choosed(%d) is invaild\n",Pos);
        return (datatype)-1;
    }
    datatype del=s->data[Pos];
    for(i=Pos;i<=s->last;i++){
        s->data[i]=s->data[i+1];
    }
    s->last--;
    return del;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值