#define MAXSIZE 100
#define ElementType int
typedef struct LNode* List;
struct LNode
{
ElementType Data[MAXSIZE];
int Last;
};
struct LNode Lnode;
List PtrL;
//访问下标为 i 的元素:Lnode.Data[i]或Ptrl->Data[i]
//线性表的长度:Lnode.last + 1或Ptrl->last + 1
//初始化(建立空表)
List MakeEmpty()
{
List PtrL;
PtrL = (List)malloc(sizeof(struct LNode));
PtrL->Last = -1;
return PtrL;
}
//查找元素
int Find(ElementType X, List PtrL)
{
int i = 0;
while (i < PtrL->Last && PtrL->Data[i] != X)
{
i++;
}
if (i > PtrL->Last)
return -1;
else
return i;
}
//插入元素(在第 i 个位置上插入元素X)
void Insert(ElementType X, List PtrL, int i)
{
int j;
if (PtrL->Last == MAXSIZE - 1)
{
printf("表满");
return;
}
if (i<1 || i>PtrL->Last + 2)
{
printf("位置越界");
return;
}
for (j = PtrL->Last; j > i; j--)
{
PtrL->Data[j + 1] = PtrL->Data[j];
}
PtrL->Data[i - 1] = X;
PtrL->Last++;
return;
}
//删除某位置上元素
void Delete(List PtrL, int i)
{
int j;
if (i<1 || i>PtrL->Last + 2)
{
printf("位置越界");
return;
}
for (j = i; j <= PtrL->Last; j++)
{
PtrL->Data[j - 1] = PtrL->Data[j];
}
PtrL->Last--;
return;
}
线性表的数组存储方式
最新推荐文章于 2022-05-06 20:52:37 发布