当前位置: 首页 > news >正文

关于一个简单的顺序表代码

1.首先是头文件SeqList.h的代码:

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int SXBint;
typedef struct SL
{SXBint* a;int size;int capacity;
}SLnode;
//初始化
void SeqLsitInit(SLnode* ps);
//尾插
void SeqPushback(SLnode* ps, SXBint x);
//头插
void SeqPushFront(SLnode* ps, SXBint x);
//打印
void Seq_dayin(SLnode ps);
//尾删
void SeqListPopbank(SLnode* ps);
//头删
void SeqListPopFront(SLnode* ps);
//指定插入
void SeqlList_charu(SLnode* ps, int pos, SXBint x);
//指定删除
void SeqList_shan(SLnode* ps, int pos);
//销毁
void SeqList_xiaohui(SLnode* ps);
//查找
int SeqList_chazhao(SLnode* ps, SXBint x);
//修改
void SeqList_xiugai(SLnode* ps, int pos, SXBint x);

2.源文件SeqList.c的实现方法函数的代码

#include"SeqList.h"
//初始化
void SeqLsitInit(SLnode* ps)
{ps->a = NULL;ps->capacity = ps->size = 0;
}
//申请空间
void kuorong(SLnode* ps)
{if (ps->capacity == ps->size){int Newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;SXBint* temp = (SXBint*)realloc(ps->a, sizeof(SLnode) * Newcapacity);if (temp == NULL){perror("error:");exit(1);}ps->a = temp;ps->capacity = Newcapacity;}
}
//尾插
void SeqPushback(SLnode* ps, SXBint x)
{assert(ps);kuorong(ps);ps->a[ps->size++] = x;
}
//打印数据
void Seq_dayin(SLnode ps)
{for (int i = 0; i < ps.size; i++){printf("%d->", ps.a[i]);}printf("NULL\n");
}
//头插
void SeqPushFront(SLnode* ps, SXBint x)
{assert(ps);kuorong(ps);for (int i = ps->size; i >= 0; i--){ps->a[i+1] = ps->a[i];}ps->a[0] = x;ps->size++;
}
//尾删
void SeqListPopbank(SLnode* ps)
{assert(ps);ps->size--;
}
//头删
void SeqListPopFront(SLnode* ps)
{assert(ps);for (int i = 0; i < ps->size - 1; i++){ps->a[i] = ps->a[i+1];}ps->size--;
}
//指定插入
void SeqlList_charu(SLnode* ps, int pos, SXBint x)
{assert(ps);assert(pos < ps->size);kuorong(ps);for (int i = ps->size; i >= pos; i--){ps->a[i + 1] = ps->a[i];}ps->a[pos] = x;ps->size++;
}
//指定删除
void SeqList_shan(SLnode* ps, int pos)
{assert(pos < ps->size);int start = pos + 1;while (start < ps->size){ps->a[start - 1] = ps->a[start];++start;} ps->size--;
} 
//销毁
void SeqList_xiaohui(SLnode* ps)
{free(ps->a);ps->a = NULL;ps->capacity = ps->size = 0;
}
//查找
int SeqList_chazhao(SLnode* ps, SXBint x)
{for (int i = 0; i < ps->size; i++){if (ps->a[i] == x){return i;}}return -1;
}
//修改
void SeqList_xiugai(SLnode* ps, int pos, SXBint x)
{assert(pos < ps->size);assert(ps);ps->a[pos] = x;
}

3.测试的源文件test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void menu()
{printf("\n*********************\n");printf("1.尾插数据 2.头插入数据\n");printf("3.尾删数据 4.头删数据\n");printf("5.打印数据 6.修改数据\n");printf("7.指定插入 8.指定删除\n ");printf("9.查找     10.退出   \n");printf("*******************\n");printf("\n请输入你的操作\n");
}
int main()
{SLnode S;SeqLsitInit(&S);int x = 0, pos;int input = 0;do{menu();scanf("%d", &input);switch (input){case 1:printf("请输入x的值,输入-1结束尾插数据\n");do{scanf("%d", &x);if (x!=-1){SeqPushback(&S, x);}} while (x!=-1);break;case 2:printf("请输入x的值,输入-1结束头插\n");do{scanf("%d", &x);if (x != -1){SeqPushFront(&S, x);}} while (x != -1);break;case 3:SeqListPopbank(&S);printf("尾删成功\n");break;case 4:SeqListPopFront(&S);printf("头删成功\n");break;case 5:Seq_dayin(S);break;case 6:printf("请输入你要修改的数据位置\n");scanf("%d", &pos);printf("请输入你要修改的数\n");scanf("%d", &x);SeqList_xiugai(&S, pos, x);printf("修改成功\n");break;case 7:printf("请输入你要指定插入的位置\n");scanf("%d", &pos);printf("请输入你要插入的数\n");scanf("%d", &x);SeqlList_charu(&S, pos, x);printf("插入成功\n");break;case 8:printf("请输入你要删除的位置下标\n");scanf("%d", &x);SeqList_shan(&S, x);break;case 9:printf("请输入你要查找的数\n");scanf("%d", &x);int ret = SeqList_chazhao(&S, x);if (ret != -1){printf("找到了,下标为%d", ret);}else {printf("找不到\n");}break;case 10:input = -1;printf("退出中....");break;default:printf("请输入有效数字\n");break;}} while (input != -1);return 0;
}

http://www.lryc.cn/news/409843.html

相关文章:

  • 【资料分享】2024第三届钉钉杯大学生大数据挑战赛B题思路解析+双语言代码
  • Typescript学习笔记(2.0)
  • 【IJHE】:微通道反应器中全氢二苄基甲苯脱氢产氢
  • Spring踩坑:抽象类作为父类,使用子类@Autowired属性进行填充,属性值为null
  • C#网络连接:TCP/IP模式下的网络连接与同步
  • 基于树莓派(Raspberry Pi) 的智能电表监测系统设计:集成 Home Assistant、SQLite 和 MQTT 协议
  • C语言程序设计(二)
  • Oracle对数据库行和数据库的监控
  • 论文阅读:面向自动驾驶场景的多目标点云检测算法
  • Vite + Vue3 + TS项目配置前置路由守卫
  • 设计模式-备忘录
  • openEuler安装docker,加速镜像拉取
  • angular入门基础教程(七)系统路由
  • Unity Canvas动画:UI元素的动态展示
  • apache.commons.pool2 使用指南
  • 【Python面试题收录】Python编程基础练习题②(数据类型+文件操作+时间操作)
  • typescript 定义类型
  • 基于Java+SpringBoot+Vue的的课程作业管理系统
  • 分布式日志分析系统--ELK
  • Linux初学基本命令
  • 如何优化PyTorch以加快模型训练速度?
  • 用最简单的方法对大数据进行处理 vs spark(不需要安装大数据处理工具)
  • 非线性校正算法在红外测温中的应用
  • python----线程、进程、协程的区别及多线程详解
  • 将 magma example 改写成 cusolver example eqrf
  • 微信小程序教程007:数据绑定
  • Git -- git stash 暂存
  • 基于YOLO的植物病害识别系统:从训练到部署全攻略
  • 数据库开发:MySQL基础(二)
  • 实现物理数据库迁移到云上