上一篇定义了线性表的抽象数据类型以及它所具有的操作,这一篇给出顺序表示的实现代码,主要是C语言实现的
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define ListLength 100
typedef struct {
int content;
}DataType;
typedef struct {
int n;
DataType *element;
}initList,*List;
//创建并返回一个空的线性表
List createList(void) {
List list = (List)malloc(sizeof(initList));
if (list != NULL) {
list->element = (DataType *)malloc(sizeof(DataType) * ListLength);
if (list->element != NULL) {
list->n = 0;
return list;
}
else {
free(list);
}
}
printf("Out of space!!!\n");
return NULL;
}
//向list中末尾插入元素x,返回值:1成功,0失败
int insert(List list, DataType x) {
if (list->n >= ListLength) {
printf("Overflow!!!\n");
return 0;
}
list->element[list->n++] = x;
return 1;
}
//向list中指定下标位置插入元素x,返回值:1成功,0失败
int insertByLocation(List list, int position, DataType x) {
if (list->n >= ListLength) {
printf("Overflow!!!\n");
return 0;
}
if (position < 0 || position > list->n) {
printf("Not Exist!!!\n");
return 0;
}
int p;
for (p = list->n; p >= position;p --) {
list->element[p] = list->element[p - 1];
}
list->element[position] = x;
list->n++;
return 1;
}
//判断两个DataType元素是否相等,返回值:1相等,0不等
int equal(DataType x1, DataType x2) {
if (x1.content == x2.content)
return 1;
return 0;
}
//根据元素x在list中查找,返回值:返回对应下标位置,-1表示没有找到
int find(List list, DataType x) {
int index;
for (index = 0; index < list->n; index++) {
if (equal(x, list->element[index]))
break;
}
if (index == list->n) {
printf("Not exist!!!\n");
return -1;
}
return index;
}
//根据下标位置在list中查找,返回值:返回对应角标位置的元素
DataType findByLocation(List list, int position) {
if (position < 0 || position > list->n) {
printf("Not Exist!!!\n");
DataType data;
data.content = 0;
return data;
}
return list->element[position];
}
//返回值:返回list中元素个数
int size(List list) {
return list->n;
}
//判断list是否为空,返回值:1空,0非空
int isEmpty(List list) {
if (list->n == 0)
return 1;
return 0;
}
//在list中查找与元素x相等的元素并删除,返回值:1成功,0失败
int delete(List list, DataType x) {
int index = find(list, x);
if (index == 0)
return 0;
for (; index < list->n; index++) {
list->element[index] = list->element[index + 1];
}
list->n--;
return 1;
}
//在list中查找对应下标位置元素并删除,返回值:1成功,0失败
int deleteByLocation(List list,int position) {
if (position < 0 || position > list->n) {
printf("Not Exist!!!\n");
return 0;
}
for (; position < list->n; position++) {
list->element[position] = list->element[position + 1];
}
list->n--;
return 1;
}
//销毁list,返回值:空
void destroy(List *list) {
free((*list)->element);
free(*list);
*list == NULL;
}
//打印list,返回值:空
void printList(List list) {
int index;
printf("list:");
for (index = 0; index < list->n; index++) {
printf("%d\t",list->element[index].content);
}
printf("size:%d", size(list));
printf("\n");
}
int main() {
DataType data1,data2;
List list = createList();
data1.content = 1;
insert(list, data1);
printList(list);
data2.content = 3;
insertByLocation(list, 0, data2);
printList(list);
delete(list, data1);
printList(list);
data1.content = 1;
insert(list, data1);
printList(list);
deleteByLocation(list, 0);
printList(list);
DataType data = findByLocation(list, 0);
printf("findByLocation:(location:%d, value:%d)\n",0,data.content);
destroy(&list);
system("pause");
return 0;
};
代码对应着上一章的抽象数据类型定义,main函数中是测试