线性表顺序存储结构的创建、插入结点、删除结点、就地逆置。
#include "stdio.h"
#include "malloc.h"
typedef struct
{
int data[30];
int length;
}slist, *List;
void init(List); //线性表初始化
void insert(List S, int p); //线性表插入
void delete(List S, int p); //线性表删除
void reverse( List S); //线性表逆置
int main()
{
List S;
S = (List)malloc(sizeof(slist));
printf("线性表的顺序存储结构的初始化:\n");
init(S);
insert(S, 4);
delete(S, 3);
reverse(S);
return 0;
}
void init(List S)
{
int i;
for(i = 0; i < 5; ++i)
scanf("%d", &S->data[i]);
S->length = 5;
for(i = 0; i < S->length; ++i)
printf("%d ", S->data[i]);
printf("\n");
}
void insert(List S, int p)
{
int i, a;
printf("请输入一个10以内的正整数:\n");
scanf("%d", &a);
for(i = S->length; i >= p; --i)
S->data[i] = S->data[i-1];
S->length++;
S->data[p-1] = a;
printf("在第4个位置插入新元素后的线性表:\n");
for(i = 0; i < S->length; ++i)
printf("%d ", S->data[i]);
printf("\n");
}
void delete(List S, int p)
{
int i,a;
printf("删除元素之前的线性表:\n");
for(i = 0; i < S->length; ++i)
printf("%d ", S->data[i]);
printf("\n");
a = S->data[p-1];
for(i = p; i < S->length; ++i)
S->data[i-1] = S->data[i];
S->length--;
printf("删除第3个元素之后的线性表:\n");
for(i = 0; i < S->length; ++i)
printf("%d ", S->data[i]);
printf("\n");
}
void reverse(List S)
{
int i, a, j = S->length-1;
for(i = 0; i <= S->length/2; ++i,--j)
{
a = S->data[i];
S->data[i] = S->data[j];
S->data[j] = a;
}
printf("逆置之后的线性表:\n");
for(i = 0; i < S->length; ++i)
printf("%d ", S->data[i]);
printf("\n");
}