主函数
#include "student.h"
int main(int argc, const char* argv[])
{
//创建顺序表
stuListPtr p = stu_create();
//增加数据
stu_add(p,"hellon",18,1.8,120.5);
stu_add(p,"jack",25,1.5,131.5);
stu_add(p,"rob", 99, 1.3, 66.6);
stu_add(p,"bob", 56, 0.2, 77.5);
stu_add(p,"james", 66, 2.3, 180);
stu_add(p,"curry", 32, 1.9, 160);
//遍历顺序表
show(p);
//任意位置插入数据
insert(p, 3, "tom", 23, 230, 130);
show(p);
insert(p, 2, "jarry", 13, 187, 96);
show(p);
//任意位置删除数据
del(p, 2);
show(p);
return 0;
}
函数
#include "student.h"
//顺序表的创建
stuListPtr stu_create()
{
//堆区申请顺序表的空间,返回给主函数
stuListPtr p = (stuListPtr)malloc(sizeof(stuList));
//判断空间是否合法
if (NULL == p)
{
printf("创建失败\n");
return NULL;
}
//顺序表的长度为0
p->len = 0;
//数组清空
memset(p->n, 0, sizeof(p->n));
printf("创建成功\n");
return p;
}
int empty(stuListPtr p)//判空
{
//判断顺序表是否合法
if (NULL == p)
{
printf("创建失败\n");
return -1;
}
return p->len == 0;
}
int fill(stuListPtr p)//判满
{
//判断顺序表是否合法
if (NULL == p)
{
printf("判满失败\n");
return -1;
}
return p->len == MAX;
}
//顺序表增加
int stu_add(stuListPtr p, char n[32], int a, float h, float w)
{
//判断是否合法,判满
if (NULL == p || fill(p))
{
printf("增加失败\n");
return 0;
}
//放入数据
strcpy(p->n[p->len].name, n);
p->n[p->len].age = a;
p->n[p->len].height = h;
p->n[p->len].weight = w;
//顺序表的长度增加
p->len++;
return 1;
}
//遍历顺序表
void show(stuListPtr p)
{
printf("名字\t年龄\t身高\t体重\n");
//判断顺序表是否合法,判空
if (NULL == p || empty(p))
{
printf("遍历失败\n");
return;
}
for (int i = 0; i < p->len; i++)
{
printf("%s\t%d\t%.1f\t%.1f\n", p->n[i].name, p->n[i].age, p->n[i].height, p->n[i].weight);
}
putchar(10);
}
//任意位置插入数据
int insert(stuListPtr p, int index, char n[32], int a, float h, float w)
{
//判断顺序表是否合法,判满
//判断插入的位置是否合法,
//index 位置
if (NULL == p || fill(p) || index <= 0 || index > MAX)
{
printf("插入失败\n");
return 0;
}
index = index - 1;
for (int i = 0; i < p->len - index; i++)
{
p->n[p->len - i] = p->n[p->len - i - 1];
}
//插入数据
strcpy(p->n[index].name, n);
p->n[index].age = a;
p->n[index].height = h;
p->n[index].weight = w;
//顺序表长度+1
p->len++;
return 1;
}
//任意位置删除
int del(stuListPtr p, int index)
{
//判断插入的位置是否合法
//index 位置
if (NULL == p || empty(p) || index <= 0 || index > MAX)
{
printf("删除失败\n");
return 0;
}
index = index - 1;
//index+1坐标数据覆盖index坐标数据
for (int i = 0; i < p->len - index; i++)
{
p->n[index + i] = p->n[index + 1 + i];
}
//顺序表长度-1
p->len--;
}
头文件
#ifndef __STUDENT_H__
#define __STUDENT_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//宏定义线性表的最大容量
#define MAX 40
//类型重定义,表示要存放数据的类型
typedef char DataType;
//定义顺序表的结构体类型
typedef struct per_information
{
char name[32];
int age;
float height;
float weight;
}person;
typedef struct student
{
person n[MAX];//定义数组存放学生序号
int len;//顺序表的长度,学生的个数
}stuList, *stuListPtr;
//顺序表的创建
stuListPtr stu_create();
//判空
int empty(stuListPtr p);
//判满
int fill(stuListPtr p);
//顺序表的增加
int stu_add(stuListPtr p, char n[32], int a, float h, float w);
//顺序表遍历
void show(stuListPtr p);
//任意位置插入
int insert(stuListPtr p, int index, char n[32], int a, float h, float w);
//任意位置删除
int del(stuListPtr p, int index);
#endif