List MakeEmpty()
{
//malloc
List L;
L = (List)malloc(sizeof(struct LNode));
if (L == NULL)
{
printf("顺序表创建失败\n");
return L;
}
//initialize
memset(L, 0, sizeof(struct LNode));
L->Last = -1;
//return
return L;
}
void PrintL(List L)
{
Position i;
if (L == NULL)
return ;
if (L->Last == -1)
printf("顺序表为空\n");
else
printf("顺序表的值为:");
for (i = 0; i <= L->Last; i++)
{
printf("%d ", L->Data[i]);
}
puts("");
return ;
}
Position Find(List L, ElementType X)
{
for (Position i = 0; i <= L->Last; i++)
{
if (L->Data[i] == X) {
return i;
}
}
return ERROR;
}
bool Insert(List L, ElementType X, int i)
{
Position j;
//full
if (L->Last == MAXSIZE - 1 )
{
printf("顺序表满,无法插入\n");
return false;
}
//check para 0<=i<=last+1
if ((i < 0) || (i - 1 > (L->Last + 1)))
{
printf("插入位置不合法\n");
return false;
}
//move
for (j = L->Last; j >= i - 1; j--)
{
L->Data[j + 1] = L->Data[j];
}
//update value last
L->Data[i - 1] = X;
L->Last = L->Last + 1;
return true;
}
bool Delete(List L, int i)
{
Position j;
if (L->Last == -1)
{
printf("顺序表为空\n");
return false;
}
//pos [0, last]
if (i < 0 || i - 1 > L->Last)
{
printf("位序%d不存在元素\n",i);
return false;
}
//move [pos+1, last]
for (j = i ; j <= L->Last; j++)
{
L->Data[j - 1] = L->Data[j];
}
//update
L->Last--;
return true;
}
1111111111
最新推荐文章于 2024-08-29 20:46:17 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)