#include<stdio.h>
#include<stdlib.h>
#define Max 20
int length = 0;
typedef struct node
{
int date[Max];
int length;
}Seqlist,*PSeqlist;
//线性表的初始化
PSeqlist Init_Seqlist(void)
{
PSeqlist PL;
PL = (PSeqlist)malloc(sizeof(Seqlist));
if (NULL==PL)
{
return NULL;
}
else
{
PL->length = 0;
}
return (PL);
}
//求线性表的长度:若找到则返回长度;没找到返回-1;
int Length_Seqlist(PSeqlist SeqlistPoint)
{
if (SeqlistPoint)
{
return (SeqlistPoint->length);
}
return (-1);
}
//线性表的检索:-1表示表不存在,0表示查找失败
int Location_Seqlist(PSeqlist PL, int x)
{
int i = 0;
if (!PL)
{
printf("表不存在\n");
return -1;
}
else
{
while (i < PL->length && x != PL->date[i])
{
i++;
}
}
if (i >= PL->length)
{
return 0;
}
else
{
return (i + 1);
}
}
//线性表的插入运算
int Insert_Seqlist(PSeqlist PL, int i, int num)
{
int j;
if (!PL)
{
printf("NO\n");
return -1;
}
if (i > PL->length || i < 1)
{
printf("插入位置不合适\n");
return 0;
}
for (j = PL->length - 1; j >= i-1; j--)
{
PL->date[j + 1] = PL->date[j];
}
PL->date[i-1] = num;
PL->length++;
return 1;
}
//线性表的删除
int Delete_Seqlist(PSeqlist PL, int i)
{
int j;
if (!PL)
{
printf("NO\n");
return -1;
}
if (i > PL->length || i < 1)
{
printf("删除位置不合适\n");
return 0;
}
for (j = i; j < PL->length; j++)
{
PL->date[j - 1] = PL->date[j];
}
PL->length--;
return 1;
}
void PrintSeqlist(PSeqlist PL)
{
int i;
for (i = 0; i < PL->length ; i++)
{
printf("%d ", PL->date[i]);
}
}
int main()
{
int i=0;
int x;
PSeqlist PL = Init_Seqlist();
for ( i = 0; i < 5; i++)
{
scanf_s("%d", &PL->date[i]);
PL->length++;
}
printf("初始化的线性表示为:\n");
PrintSeqlist(PL);
int length = Length_Seqlist(PL);
printf("\n线性表长度为:%d\n", length);
int locate = Location_Seqlist(PL, 5);
printf("\n该元素在:%d\n", locate);
Insert_Seqlist(PL, PL -> length, 8);
printf("插入后:");
PrintSeqlist(PL);
Delete_Seqlist(PL, 3);
printf("\n删除后:");
PrintSeqlist(PL);
return 0;
}