数据结构之顺序表的建立和增删改查

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXDATA 50
int n;
typedef struct{
        int data[MAXDATA];
        int len;
    }seqList;
//声明函数
seqList *Init_seqList(seqList *L); //初始化顺序表(清空)
int insert_seqList(seqList *L,int i,int ele); //向顺序表中插入元素
int Delete_seqList(seqList *L,int i);  //删除顺序表中的元素
void Output_seqList(seqList *L);    //输出顺序表
void Input_seqList(seqList *L);  //输入顺序表
int Revise_seqList(seqList *L,int i,int ele);//修改顺序表
//声明结束
seqList *Init_seqList(seqList *L)
{
    L = (seqList*)malloc(sizeof(seqList));
    L->len = -1;
    return L;
}

int insert_seqList(seqList *L,int i,int ele)
{
      //L = (seqList*)malloc(sizeof(seqList));
      if(i<1){
        printf("please input right");
        return 0;
      }
      else if(i>L->len+1)
      {
          printf("exceed len");
          return 0;
      }
      <span style="color:#ff0000;">i--;</span>//把逻辑转换为物理
      for(int j=L->len;j>=i;j--)
      {
          L->data[j+1] = L->data[j];
      }
      L->data[i] = ele;
      L->len++;
      return 1;
}

int Delete_seqList(seqList *L,int i)
{
    if(L->len == -1)
    {
        printf("this chain no exist");
        return false;
    }
    else if(i <1 || i>L->len)
    {
        printf("i element does not exist");
        return false;
    }
   <span style="color:#cc0000;"> i--;</span>
    for(int j=i;j<=L->len;j++)
    {
        L->data[j]=L->data[j+1];
    }
    L->len--;
    return 1;
}

void Output_seqList(seqList *L)
{
    if(L->len <=0 )
        printf("this chain is none");
    for(int i=0;i<L->len;i++)
        printf("%d ",L->data[i]);
    printf("\n");
}

void Input_seqList(seqList *L)
{
    printf("please input n ele \n");
    scanf("%d",&n);
    L->len=n;
    for(int i=0;i<L->len;i++)
    {
        scanf("%d",&L->data[i]);
    }
}

int Revise_seqList(seqList *L,int i,int ele)
{
    if(i<0 || i >L->len)
    {
        printf("this ele no exist");
        return -1;
    }
    <span style="color:#ff0000;">i--;</span>
    L->data[i] = ele;
    return 1;
}
int main()
{
    seqList *L;
    int tar=2,ch=0,i=3,ele=90;
    L = (seqList *)malloc(sizeof(seqList));

    Init_seqList(L);
    Input_seqList(L);
    insert_seqList(L,tar,ch);
    Output_seqList(L);
    Delete_seqList(L,i);
    Output_seqList(L);
    Revise_seqList(L,4,ele);
    Output_seqList(L);
    Init_seqList(L);
    Output_seqList(L);

}

你可能感兴趣的:(数据结构之顺序表的建立和增删改查)