顺序表seq.c

#include "seq.h"
//1、在堆区申请顺序表的空间
seq_p create_seq_list()
{
    //从堆区申请一个顺序表的空间
    seq_p L = (seq_p)malloc(sizeof(seq_list));
    if(L==NULL)
    {
        printf("空间申请失败\n");
        return NULL;
    }
    //刚申请顺序表,表中没有内容,给 len置0
    L->len=0;
    return L;
}
//2 判空
int empty_seq(seq_p L)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return -1;
    }
    return L->len==0?1:0;
}
//3 判满
int full_seq(seq_p L)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return -1;
    }
    return L->len==MAX?1:0;
}
//4 头插
void insert_head(seq_p L,datatype data)
{
    //入参检查
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    //判满
    if(full_seq(L))
    {
        printf("表已满,不能插入\n");
        return;
    }
    //循环后移元素
    for(int i=L->len-1;i>=0;i--)
    {
        L->data[i+1]=L->data[i];
    }
    L->data[0]=data;
    L->len++;
}
//5 输出
void show_seq(seq_p L)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    for(int i=0;i<L->len;i++)
    {
        printf("%d\n",L->data[i]);
    }
}
// 6 尾插
void insert_tail(seq_p L,datatype data)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    L->data[L->len]=data;
    L->len++;
}
//7 头删
void dele_head(seq_p L)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
        //判空
    if(empty_seq(L))
    {
        printf("表已空,无需删除\n");
        return;
    }
    for(int i=1;i<L->len;i++)
    {
        L->data[i-1]=L->data[i];
    }
    L->len--;
}
//8 尾删
void dele_tail(seq_p L)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
        //判空
    if(empty_seq(L))
    {
        printf("表已空,无需删除\n");
        return;
    }
    L->len--;//将长度减一
}
//9 按位置查找元素的值
int search_pos(seq_p L,int pos)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return -1;
    }
        //判空
    
    if(pos<0||pos>L->len)
    {
        printf("位置不合理,重新输入\n");
        return -2;
    }
    int i=pos;
    return L->data[i];
}
//10 按位置查找,并更改数据的值
void updata_pos(seq_p L,int pos,int new_data)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
        
    
    if(pos<0||pos>L->len-1)
    {
        printf("位置不合理,重新输入\n");
        return;
    }
    L->data[pos]=new_data;
}
//11 按位置插入
void insert_pos(seq_p L,int pos,datatype data)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
        
    
    if(pos<0||pos>L->len)
    {
        printf("位置不合理,重新输入\n");
        return;
    }
    if(full_seq(L))
    {
        printf("表已满\n");
        return;
    }
    for(int i=L->len-1;i>=pos;i--)
    {
        L->data[i+1]=L->data[i];
    }
    L->data[pos]=data;
    L->len++;
}
//12 按位置删除
void dele_pos(seq_p L,int pos)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
        
    
    if(pos<0||pos>L->len-1)
    {
        printf("位置不合理,重新输入\n");
        return;
    }
    for(int i=pos;i<=L->len-1;i++)
    {
        L->data[i]=L->data[i+1];
    }
    L->len--;
}
//13 按值查找元素,返回下标
int search_data(seq_p L,datatype key)
{
    //判空
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    //判断位置合理性        
    if(pos<0||pos>L->len-1)
    {
        printf("位置不合理,重新输入\n");
        return;
    }
    for(int i=0;i<=L->len-1;i++)
    {
        if(L->data[i]=key)
        {
            return i;
        }
        else
            printf("查无此数\n");
            return;
    }

}
//14 清空顺序表
void clear_seq(seq_p L)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    //给实际长度置0
        L->len=0;
    

}
//15 释放顺序表,释放堆区申请的空间
void free_seq(seq_p *L)
{
    if(L==NULL||*L==NULL) //*L必须在后面
    {
        printf("入参为空,请检查\n");
        return;
    }
    //释放前先清空顺序表
    clear_seq(*L); //传(*L)
    free(*L);// 获取二级指针L中保存的一级指针的值
    *L=NULL;// 让主函数中的实参L,指向NULL
}
//16 顺序表去重
void dele(seq_p L)
{
    if(L==NULL)
    {
        printf("入参为空,请检查\n");
        return;
    }
    for(int i=0;i<L->len;i++)
    {
        for(int j=i+1;j<L->len;j++)
        {
            if(L->data[i]==L->data[j])
            {
                for(int n=j;n<L->len;n++)
                {
                    L->data[n]=L->data[n+1];
                }
            }
        }
    }
}
ubun

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值