数据结构
顺序表代码练习题:
1.删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,否则返回0,并将被删除元素的值赋给e。

/**************************************************************************************
*
*
* file name: demo_2.c
* author : Tongyaqi1110@163.com
* date : 2024/04/21
* function : 设计一个算法,顺序表中删除指定位置
* note : None
*
* CopyRight (c) 2024 Tongyaqi1110@163.com All Right Reserved
*************************************************************************************/
#include <stdio.h>
typedef int DataType_t;
//构造记录顺序表SequenceList各项参数(顺序表的首地址 + 顺序表的容量 + 顺序表中最后有效元素的下标)的结构体
typedef struct SequenceList
{
DataType_t * Addr; //记录顺序表首地址
unsigned int Size; //记录顺序表的容量
int Last; //顺序表中最后元素的下标
}SeqList_t;
//创建顺序表并对顺序表进行初始化
SeqList_t * SeqList_Create(unsigned int size)
{
//1.利用calloc为顺序表的管理结构体申请一块堆内存
SeqList_t *Manager = (SeqList_t *)calloc(1,sizeof(Manager));
if(NULL == Manager)
{
perror("calloc memory for manager is failed");
exit(-1); //程序异常终止
}
//2.利用calloc为所有元素申请堆内存
Manager->Addr = (DataType_t *)calloc(size,sizeof(DataType_t));
if (NULL == Manager->Addr)
{
perror("calloc memory for element is failed");
free(Manager);
exit(-1); //程序异常终止
}
//3.对管理顺序表的结构体进行初始化(元素容量 + 最后元素下标)
Manager->Size = size; //对顺序表中的容量进行初始化
Manager->Last = -1; //由于顺序表为空,则最后元素下标初值为-1
return Manager;
}
bool DelElement(Seqlist_t*L,int p,int e)
{
*e=L->data[p];
//向前移动元素
for(int i=p;i<=length-1;i++)
{
L->data[i]=L->data[i+1];
}
L->length--;
return 1;
if(p<0||p>=L->length-1){
return 0;
}
}