• step3 . day1 数据结构之线性表顺序表


    大学没有选修数据结构,只是在C语言书最后提到过几种数据的 组织形式,也算眼熟,今天学的顺序表感觉还是很容易理解,写了一个有史以来代码最长、调试时间最短的代码,甚是感觉提高了不少,贴上Mark一下,写注释的习惯也慢慢养起来了,要不首先坑的就是自己。

    吐槽一下,还是分开写模块比较看着舒服。

    #include<stdio.h>
    #include <stdlib.h>

    #define N 32

    typedef int datetpye; //重定义date数据类型


    typedef struct list{ //顺序表结构体声明、重命名

    datetpye date[N];
    datetpye last;

    }seqlist,*seqlist_p;

    //创建顺序表
    seqlist_p Create(){

    seqlist_p L = NULL;
    L = (seqlist_p)malloc(sizeof(seqlist)); //分配空间
    L->last = -1; //声明顺序表为空

    return L;
    }

    //判断是否位满表
    int seqlist_is_full(seqlist_p L){
    return ((L->last) >= (N-1)) ? 1:0;
    }

    //尾部插入
    int seqlist_insert(seqlist_p L,datetpye value){

    if(seqlist_is_full(L) == 1){
    printf("seqlist is full ");
    return 1;
    }

    L->last++; //插入后重定位尾部
    L->date[L->last] = value;

    return 0;
    }

    //遍历顺序表打印
    void seqlist_show(seqlist_p L){

    int i;
    for(i=0;i<=L->last;i++){
    printf("%d ",L->date[i]);
    }
    puts("");
    }

    //头插入
    int seqlist_insert_head(seqlist_p L,datetpye value){

    if(seqlist_is_full(L) == 1){ //判断顺序表是否已满
    printf("seqlist is full ");
    return -1;
    }

    int i;
    for(i=L->last;i>=0;i--){ //插入位置开始内容依次后移
    L->date[i+1] = L->date[i];
    }

    L->date[0] = value;
    L->last++; //尾部记号更新
    return 0;
    }

    //按照位置插入
    int seqlist_insert_pos(seqlist_p L,int pos,datetpye value){

    if(seqlist_is_full(L) == 1){ //判断顺序表是否已满
    printf("seqlist is full ");
    return -1;
    }

    if(pos < 0 || pos > L->last+1){ //输入位置合法判断
    printf("position no allowed ");
    return -2;
    }

    int i;
    for(i=L->last;i>=pos;i--){ //插入位置开始内容依次后移
    L->date[i+1] = L->date[i];
    }

    L->date[pos] = value;
    L->last++; //尾部记号更新
    return 0;
    }

    //判断是否为空函数
    int seqlist_is_empty(seqlist_p L){

    return L->last == -1 ? 1 : 0;
    }

    //按照位置删除
    int seqlist_del_pos(seqlist_p L,int pos){

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -999;
    }

    if(pos < 0 || pos > L->last){ //判断位置是否合法
    printf("position not allowed ");
    return -9999;
    }

    int i;
    datetpye value = L->date[pos];

    for(i=pos;i<L->last;i++){ //位置后继赋前驱
    L->date[i] = L->date[i+1];
    }

    L->last--;
    return value;
    }

    //删除头数据
    int seqlist_del_head(seqlist_p L){

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -999;
    }

    int i;
    datetpye value = L->date[0];

    for(i=0;i<L->last;i++){ //位置后继赋前驱
    L->date[i] = L->date[i+1];
    }

    L->last--;
    return value;
    }


    //删除尾数据
    int seqlist_del_tail(seqlist_p L){

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -999;
    }

    int i;
    datetpye value = L->date[L->last];
    L->last--;
    return value;
    }


    //按值删除
    int seqlist_del_value(seqlist_p L,datetpye value){

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -1;
    }

    int i,pos;
    int flag = 0;
    for(i=0;i<=L->last;i++){ //寻找值所在位置
    if(value == L->date[i]){
    pos = i;
    flag =1;
    seqlist_del_pos(L,pos);
    return pos;
    }
    }
    if(flag == 0){
    printf("value not found ");
    return -1;
    }
    }

    //按值查找
    int seqlist_serach_value(seqlist_p L,datetpye value){

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -1;
    }

    int i,pos;
    for(i=0;i<=L->last;i++){ //寻找值所在位置
    if(value == L->date[i]){
    pos = i;
    return pos;
    }
    }
    printf("value not found ");
    return -1;
    }


    //按照位置查找
    int seqlist_serach_pos(seqlist_p L,int pos){

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -999;
    }

    if(pos < 0 || pos > L->last){ //判断位置是否合法
    printf("position not allowed ");
    return -999;
    }
    return L->date[pos];
    }


    //按照位置修改
    int seqlist_upgrade_pos(seqlist_p L,int pos,datetpye value){

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -999;
    }

    if(pos < 0 || pos > L->last){ //判断位置是否合法
    printf("position not allowed ");
    return -999;
    }

    L->date[pos] = value;
    return 0;
    }


    //按值修改
    int seqlist_upgrade_value(seqlist_p L,datetpye value,datetpye new_value){

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -1;
    }

    int i,pos;
    int flag = 0;
    for(i=0;i<=L->last;i++){ //寻找值所在位置
    if(value == L->date[i]){
    pos = i;
    flag =1;
    }
    }
    if(flag == 0){
    printf("value not found ");
    return -1;
    }
    else{
    L->date[pos] = new_value;
    return 0;
    }
    }


    //删除表中重复值
    int seqlist_repeat_del(seqlist_p L){

    if(seqlist_range(L)){ //判断是为空表
    printf("seqlist is empty ");
    return -999;
    }
    int i;

    for(i=0;i<L->last;i++){
    if(L->date[i] == L->date[i+1]){
    seqlist_del_pos(L,i+1);
    i--;
    }
    }

    }

    int seqlist_range(seqlist_p L){ //排序

    if(seqlist_is_empty(L)){ //判断是为空表
    printf("seqlist is empty ");
    return 1;
    }
    int i,j;
    for(i=0;i<=L->last;i++){
    for(j=0;j<=(L->last-i-1);j++){
    if((L->date[j]) > (L->date[j+1])){
    L->date[j+1] += L->date[j];
    L->date[j] = L->date[j+1] - L->date[j];
    L->date[j+1] -= L->date[j];
    }
    }
    }
    return 0;
    }


    //两个表拼接
    int seqlist_cat(seqlist_p L,seqlist_p L1){

    if((L->last + L1->last)>=32)
    {
    printf("out of list area! ");
    return -1;
    }
    seqlist_repeat_del(L);
    seqlist_repeat_del(L1);
    int i,j=0;
    for(i=(L->last+1);i<(L->last+L1->last+2),j<=L1->last;i++,j++){
    L->date[i] = L1->date[j];
    L->last++;
    }
    seqlist_repeat_del(L);

    return 0;
    }


    int main(int argc, const char *argv[])
    {

    seqlist_p L = Create(); //创建表调用

    seqlist_insert(L,4); //尾部插入
    seqlist_insert(L,2);
    seqlist_insert(L,3);
    seqlist_insert(L,7);
    seqlist_insert(L,6);
    seqlist_insert(L,7);
    seqlist_insert(L,3);
    seqlist_insert(L,3); //尾部插入

    seqlist_show(L); //遍历
    /*
    seqlist_insert_pos(L,4,0); //位置插入调用
    seqlist_show(L);

    seqlist_del_pos(L,4); //位置删除调用
    seqlist_show(L);

    seqlist_del_pos(L,7); //位置删除调用
    seqlist_show(L);

    seqlist_del_head(L); //头数据删除调用
    seqlist_show(L);

    seqlist_del_tail(L); //尾部删除调用
    seqlist_show(L);

    seqlist_del_value(L,7); //非重复值单个删除调用
    seqlist_show(L);

    printf("value's pos is %d ",seqlist_serach_value(L,7)); //非重复值单个删除调用
    seqlist_show(L);

    printf("pos's value is %d ",seqlist_serach_pos(L,9)); //非重复值单个删除调用

    seqlist_show(L);

    seqlist_upgrade_pos(L,3,10); //非重复值单个删除调用
    seqlist_show(L);

    seqlist_upgrade_value(L,4,11); //非重复值单个删除调用
    seqlist_show(L);
    */
    seqlist_repeat_del(L); //非复值单个删除调用
    seqlist_show(L);

    seqlist_p L1 = Create(); //创建l1表
    seqlist_insert(L1,4); //赋值
    seqlist_insert(L1,4);
    seqlist_insert(L1,5);
    seqlist_insert(L1,5);
    seqlist_insert(L1,1);
    seqlist_insert(L1,2);
    seqlist_insert(L1,2);
    seqlist_insert(L1,1); //尾部插入

    seqlist_show(L1);
    seqlist_repeat_del(L1); //非复值单个删除调用
    seqlist_show(L1);

    seqlist_cat(L,L1);
    seqlist_show(L);

    return 0;
    }

  • 相关阅读:
    2. 两数相加
    1. 两数之和
    x-pack elasticsearch
    简单的文档
    PHP imagepng函数 问题
    Nginx 配置
    nginx内置变量
    TCP通信
    mysql 的一些操作
    ubuntu 软件包降级
  • 原文地址:https://www.cnblogs.com/huiji12321/p/11215413.html
Copyright © 2020-2023  润新知