提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
`
提示:以下是本篇文章正文内容,下面案例可供参考
操作步骤
函数声明
void outputList(Sqlist paraList)//打印线性表
void outputMemory(SqlistPtr paraListPtr) //打印数据地址
Sqlist sqListInit(int paraData[], int paraLength)//初始化线性表
void sqListInsert(SqlistPtr paraListPtr, int paraPosition, int paraValue)//插入数据
void sqInsertTest()
int sqListDelete(SqListPtr paraListPtr, int paraPosition)//删除数据
void sqDeleteTest()//测试删除操作
int locateElement(SqlistPtr paraListPtr, int paraValue)//由值查找位置
int getElement(SqlistPtr paraListPtr, int paraPosition)//由位置查找值
void clearList(SqlistPtr paraListPtr)//删除顺序表
1。创建线性表
typedef struct Sqlist
{
int actualLength;
int data[MAX];
} *Sqlist;
2.打印线性表
void outputList(SqlistPtr paraList)
{
for(int i = 0; i < paraList->actualLength; i ++)
{
printf("%d ", paraList->data[i]);
}
printf("\r\n");
}
3.打印数据地址
void outputMemory(SqlistPtr paraListPtr)
{
printf("The address of the structure: %ld\r\n", paraListPtr);
printf("The address of actualLength: %ld\r\n", ¶ListPtr->actualLength);
printf("The address of data: %ld\r\n", ¶ListPtr->data);
printf("The address of actual data: %ld\r\n", ¶ListPtr->data[0]);
printf("The address of second data: %ld\r\n", ¶ListPtr->data[1]);
}
4.初始化线性表
Sqlist sqListInit(int paraData[], int paraLength)
{
SqlistPtr resultPtr = (SqlistPtr)malloc(sizeof(struct Sqlist));
for (int i = 0; i < paraLength; i ++)
{
resultPtr->data[i] = paraData[i];
}
resultPtr->actualLength = paraLength;
return resultPtr;
}
插入数据(开始)
void sqListInsert(SqlistPtr paraListPtr, int paraPosition, int paraValue) {
//1.检查空间
if (paraListPtr->actualLength >= LIST_MAX_LENGTH)
{
printf("不能插入数据:空间已满.\r\n");
return;
}
//2.检查位置
if (paraPosition < 0)
{
printf("不能插入数据:位置错位.\n");
return;
}
if (paraPosition > paraListPtr->actualLength)
{
printf("不能插入数据: 位置 %d 是大于表的长度 %d.\r\n", paraPosition, paraListPtr->actualLength);
return;
}
//3.移动数据
for (int i = paraListPtr->actualLength; i > paraPosition; i --)
{
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
//4。插入数据
paraListPtr->data[paraPosition] = paraValue;
//5.更新长度
paraListPtr->actualLength ++;
}
6.插入数据(结束)
void sqInsertTest()
{
int i;
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- 开始顺序插入测试. ----\r\n");
SqlistPtr tempList = sqlistInit(tempArray, 5);
printf("初始化的线性表: ");
outputList(tempList);
printf("在首位插入后,线性表: ");
sqListInsert(tempList, 0, 8);
outputList(tempList);
printf("在尾部插入后,线性表: ");
sqListInsert(tempList, 6, 9);
outputList(tempList);
printf("在尾部之外插入. \r\n");
sqlistInsert(tempList, 8, 9);
printf("线性表:");
outputList(tempList);
for (i = 0; i < 5; i ++) {
printf("插入 %d.\r\n", (i + 10));
sequentialListInsert(tempList, 0, (i + 10));
outputList(tempList);
}
printf("---- 顺序插入测试结束. ----\r\n");
}
7.删除数据
int sqListDelete(SqListPtr paraListPtr, int paraPosition) {
//检查位置
if (paraPosition < 0) {
printf("错误位置: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength) {
printf("不能删除: 位置 %d 是超出线性表长度 %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->actualLength; i ++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
paraListPtr->actualLength --;
return resultValue;
}
8.测试删除操作
void sqDeleteTest() {
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- 开始数据删除测试. ----\r\n");
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("初始化的线性表为: ");
outputList(tempList);
printf("删除首位数据: ");
sequentialListDelete(tempList, 0);
outputList(tempList);
printf("删除尾部数据: ");
sequentialListDelete(tempList, 3);
outputList(tempList);
printf("删除第二位数据: ");
sequentialListDelete(tempList, 1);
outputList(tempList);
printf("删除第五位数据: ");
sequentialListDelete(tempList, 5);
outputList(tempList);
printf("删除倒数第六位数据: ");
sequentialListDelete(tempList, -6);
outputList(tempList);
printf("---- 删除数据测试结束. ----\r\n");
outputMemory(tempList);
}
9.完整代码
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 10
//创建线性表
typedef struct Sqlist
{
int actualLength;
int data[MAXSIZE];
} Sqlist;
//打印线性表
void outputList(SqlistPtr paraList)
{
for(int i = 0; i < paraList->actualLength; i ++)
{
printf("%d ", paraList->data[i]);
}
printf("\r\n");
}
//打印数据地址
void outputMemory(SqlistPtr paraListPtr)
{
printf("The address of the structure: %ld\r\n", paraListPtr);
printf("The address of actualLength: %ld\r\n", ¶ListPtr->actualLength);
printf("The address of data: %ld\r\n", ¶ListPtr->data);
printf("The address of actual data: %ld\r\n", ¶ListPtr->data[0]);
printf("The address of second data: %ld\r\n", ¶ListPtr->data[1]);
}
//初始化线性表
Sqlist sqListInit(int paraData[], int paraLength)
{
SqlistPtr resultPtr = (SqlistPtr)malloc(sizeof(struct Sqlist));
for (int i = 0; i < paraLength; i ++)
{
resultPtr->data[i] = paraData[i];
}
resultPtr->actualLength = paraLength;
return resultPtr;
}
//插入数据
void sqListInsert(SqlistPtr paraListPtr, int paraPosition, int paraValue) {
//1.检查空间
if (paraListPtr->actualLength >= LIST_MAX_LENGTH)
{
printf("不能插入数据:空间已满.\r\n");
return;
}
//2.检查位置
if (paraPosition < 0)
{
printf("不能插入数据:位置错位.\n");
return;
}
if (paraPosition > paraListPtr->actualLength)
{
printf("不能插入数据: 位置 %d 是大于表的长度 %d.\r\n", paraPosition, paraListPtr->actualLength);
return;
}
//3.移动数据
for (int i = paraListPtr->actualLength; i > paraPosition; i --)
{
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
//4。插入数据
paraListPtr->data[paraPosition] = paraValue;
//5.更新长度
paraListPtr->actualLength ++;
}
void sqInsertTest()
{
int i;
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- 开始顺序插入测试. ----\r\n");
SqlistPtr tempList = sqlistInit(tempArray, 5);
printf("初始化的线性表: ");
outputList(tempList);
printf("在首位插入后,线性表: ");
sqListInsert(tempList, 0, 8);
outputList(tempList);
printf("在尾部插入后,线性表: ");
sqListInsert(tempList, 6, 9);
outputList(tempList);
printf("在尾部之外插入. \r\n");
sqlistInsert(tempList, 8, 9);
printf("线性表:");
outputList(tempList);
for (i = 0; i < 5; i ++) {
printf("插入 %d.\r\n", (i + 10));
sequentialListInsert(tempList, 0, (i + 10));
outputList(tempList);
}
printf("---- 顺序插入测试结束. ----\r\n");
}
//删除数据
int sqListDelete(SqListPtr paraListPtr, int paraPosition) {
//检查位置
if (paraPosition < 0) {
printf("错误位置: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength) {
printf("不能删除: 位置 %d 是超出线性表长度 %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->actualLength; i ++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
paraListPtr->actualLength --;
return resultValue;
}
//测试删除操作
void sqDeleteTest() {
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- 开始数据删除测试. ----\r\n");
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("初始化的线性表为: ");
outputList(tempList);
printf("删除首位数据: ");
sequentialListDelete(tempList, 0);
outputList(tempList);
printf("删除尾部数据: ");
sequentialListDelete(tempList, 3);
outputList(tempList);
printf("删除第二位数据: ");
sequentialListDelete(tempList, 1);
outputList(tempList);
printf("删除第五位数据: ");
sequentialListDelete(tempList, 5);
outputList(tempList);
printf("删除倒数第六位数据: ");
sequentialListDelete(tempList, -6);
outputList(tempList);
printf("---- 删除数据测试结束. ----\r\n");
outputMemory(tempList);
}
//由值查找位置
int locateElement(SqlistPtr paraListPtr, int paraValue) {
for (int i = 0; i < paraListPtr->actualLength; i ++) {
if (paraListPtr->data[i] == paraValue) {
return i;
}
}
return -1;
}
//由位置查找值
int getElement(SqlistPtr paraListPtr, int paraPosition) {
if (paraPosition < 0) {
printf("无效位置: %d.\r\n", paraPosition);
return -1;
}
if (paraPosition >= paraListPtr->actualLength) {
printf("错误的数据: 位置 %d 是超过线性表长度 %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}
return paraListPtr->data[paraPosition];
}
//删除顺序表
void clearList(SqlistPtr paraListPtr) {
paraListPtr->actualLength = 0;
}
void main() {
sqInsertTest();
sqDeleteTest();
}