【无标题】

#include <stdio.h>
#include <malloc.h>

#define LIST_MAX_LENGTH 10

typedef struct SequentialList {
int actualLength;
int data[LIST_MAX_LENGTH];
} *SequentialListPtr;
//定义结构体

void outputList(SequentialListPtr paraList) {
for (int i = 0; i < paraList->actualLength; i++) {
printf(“%d “, paraList->data[i]);
}
printf(”\r\n”);
}
//输出数组中各个数据

SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
SequentialListPtr resultPtr = (SequentialList*)malloc(sizeof(SequentialList));
for (int i = 0; i < paraLength; i++)
{
resultPtr->data[i] = paraData[i];
}
resultPtr->actualLength = paraLength;

return resultPtr;

}
//初始化数组

int locateElement(SequentialListPtr paraListPtr, int paraValue)
{
for (int i = 0; i < paraListPtr->actualLength; i++)
{
if (paraListPtr->data[i] == paraValue)
{
return i;
}
}
return -1;
}
//定位给出的数字在数组中的位置

int getElement(SequentialListPtr paraListPtr, int paraPosition) {
if (paraPosition < 0) //检查输入的数字是否小于0
{
printf(“Invalid position: %d.\r\n”, paraPosition);
return -1;
}
if (paraPosition > paraListPtr->actualLength) //检查输入的数字是否超出数组大小
{
printf(“Cannot delete element: the position %d is beyond the list length %d.\r\n”, paraPosition, paraListPtr->actualLength);
return -1;
}
return paraListPtr->data[paraPosition-1];
}
//找出所给顺序表位置上的数字

void clearList(SequentialListPtr paraListPtr)
{
paraListPtr->actualLength = 0;
}
//清空顺序表

void locateElementTest()
{
int tempArray[5] = { 3, 5, 2, 7, 4 };
SequentialListPtr tempList = sequentialListInit(tempArray, sizeof(tempArray)/4);
printf(“---- locateElementTest begins. ----\r\n”);
printf("After initialization, the list is: ");
outputList(tempList);
printf(“now test 3\n”);
int a=locateElement(tempList,3);
if (a != -1)
{
printf(“inuput value rank %d\n”,a+1);
}
else
{
printf(“the value you input dosen’t exit\n”);
}
outputList(tempList);
}

void getElementTest()
{
int tempArray[5] = { 3, 5, 2, 7, 4 };
SequentialListPtr tempList = sequentialListInit(tempArray, sizeof(tempArray)/4);
printf(“---- getElementTest begins. ----\r\n”);
printf("After initialization, the list is: ");
outputList(tempList);
printf(“now test 1\n”);
int b=getElement(tempList, 1);
if (b != -1)
{
printf(“you get %d from that position\n”,b);
}
outputList(tempList);
}

void clearListTest()
{
int tempArray[5] = { 3, 5, 2, 7, 4 };
SequentialListPtr tempList = sequentialListInit(tempArray, sizeof(tempArray)/4);
printf(“---- clearListTest begins. ----\r\n”);
printf("After initialization, the list is: ");
outputList(tempList);
printf(“now clear it”);
clearList(tempList);
outputList(tempList);
printf(“clear sucess”);
}

void main()
{
locateElementTest();
getElementTest();
clearListTest();
}

运行结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值