顺序表定义:
typedef struct
{
DataType list[MaxSize];
int length;
}SeqList;
在顺序表的第i个位置插入元素:
void ListInitiate(SeqList* L)
{
L->length = 0;
}
int Sqlist_insert(SeqList* L, int i, DataType x)
{
int j;
if (L->length >= MaxSize)
return 0;
else if (i<1 || i>L->length + 1)
return 0;
else
{
for (j = L->length; j >= i; j--)
L->list[j] = L->list[j - 1];
L->list[i - 1] = x;
L->length++;
return 1;
}
}
删除顺序表中所有值为x的结点:
void ListDelete(SeqList *L, int x)
{
int k = 0;
for (int i = 0; i < L->length; i++)
{
if (L->list[i] == x)
k++;
else
L->list[i - k] = L->list[i];
}
L->length -= k;
}
主函数设计测试:
#include<stdio.h>
#define MaxSize 100
#define MaxLen 10
typedef int DataType;
#include"SeqList.h";
int main()
{
SeqList L;
int i, x, e;
ListInitiate(&L);
Sqlist_insert(&L, 1, 2);
Sqlist_insert(&L, 2, 3);
Sqlist_insert(&L, 3, 5);
Sqlist_insert(&L, 4, 4);
Sqlist_insert(&L, 5, 6);
Sqlist_insert(&L, 6, 1);
Sqlist_insert(&L, 7, 4);
Sqlist_insert(&L, 8, 7);
Sqlist_insert(&L, 9, 4);
Sqlist_insert(&L, 10, 0);
for (i = 0; i < L.length; i++)
{
printf("%d ", L.list[i]);
}
printf("\n");
ListDelete(&L, 4);
for (i = 0; i < L.length; i++)
{
printf("%d ", L.list[i]);
}
}
测试结果: