编译工具 :Dev-c++5.11
本文参照数据结构(C语言)严蔚敏教材,使用顺序表实现教材第19页线性表操作函数接口(SqList.h),并且配有简单的测试项目(main.cpp), 接口函数中除个别函数外其他函数都按照C语言编写习惯,函数正常退出时返回0,错误时返回-1.测试项目只对基本操作进行了基本测试,可能存在少量bug。
测试项目实现的基本功能:
1.程序刚启动时初始化一个顺序表;
2.输入对顺序表执行操作所对应的函数,执行相应操作;
3.输入10可以让顺序表恢复到程序刚运行时的数据;
4.输入11重新打印各函数对应的序号。
SqList.h文件内容
#include <stdio.h>
#include <stdlib.h>#include <malloc.h>
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 5
#define ElemType int
typedef struct {
int *elem; //顺序表基地址
int length; //顺序表当前长度
int list_size; //顺序表容量
}SqList;
//1.初始化顺序表函数
int InitList(SqList &L)
{
L.elem = (ElemType *)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
if(!L.elem){
fprintf(stderr, "malloc error");
return -1;
}
L.length = 0;
L.list_size = LIST_INIT_SIZE;
return 0;
}
//销毁顺序表(L==NULL)
int DestroyList(SqList &L)
{
if(!L.elem){
fprintf(stderr, "The SqList is null\n");
return -1;
}
free(L.elem);
L.elem = NULL;
L.length = 0;
L.list_size = 0;
return 0;
}
//清空顺序表(顺序表首地址可能会变)
int ClearList(SqList &L)
{
/*
if(DestroyList(L) == -1){
return -1;
}
if(InitList(L) == -1){
return -1;
}
*/
L.length = 0;
return 0;
}
//判断顺序表是否为空 为空返回1 不为空返回0
int ListEmpty(const SqList &L)
{
if(L.length == 0){
return 1;
}
else{
return 0;
}
}
//获取顺序表中某个位置的元素
int GetElem(const SqList &L, int loc, ElemType &e)
{
if(loc < 1 || loc > L.length){
fprintf(stderr, "The loc must be [1,%d]\n",L.length);
return -1;
}
e = L.elem[loc -1];
return 0;
}
//获取顺序表中第一个与指定元素满足某种条件的元素下标
int LocateElem(const SqList &L, ElemType &e, int (*compare)(const ElemType first, const ElemType second))
{//int(*compare)(const ElemType first, const ElemType second)为指向函数的指针
int i;
for(i = 0; i < L.length; i++){
if( (*compare)(L.elem[i], e) )
//if( L.elem[i] == e)
return i+1;
}
return 0;//若函数退出for循环 说明在L中没有找到满足compare关系的数据,故返回0;
}
//返回元素的前驱元素
int PriorElem(const SqList &L, ElemType cur_e, ElemType &prior_e)
{
int i;
for(i = 0; i < L.length; i++){
if(L.elem[i] == cur_e){
if(i-1 >= 0){
prior_e = L.elem[i-1];
return 0;
}
else{
fprintf(stderr, "The %d is first element in SqList.\n",cur_e);
return -1;
}
}
}
fprintf(stderr, "The %d is not in SqList.\n", cur_e);
return -1;
}
//返回元素的后继元素
int NextElem(const SqList &L, ElemType cur_e, ElemType &next_e)
{
int i;
for(i = 0; i < L.length; i++){
if(L.elem[i] == cur_e){
if(i != L.length-1){
next_e = L.elem[i+1];
return 0;
}
else{
fprintf(stderr, "The %d is the last element in Sqlist.\n", cur_e);
return -1;
}
}
}
fprintf(stderr, "The %d is not in SqList.\n", cur_e);
return -1;
}
//2.在顺序表末尾添加元素函数
int ListInsert(SqList &L, ElemType e)
{
//若顺序表元素已满,则增加顺序表长度
if(L.length >= L.list_size){
ElemType *newbase =(ElemType*) realloc(L.elem, sizeof(ElemType)*(L.list_size + LIST_INCREMENT));
if(!newbase) {
perror("remalloc error");
return -1;
}
L.elem = newbase;
L.list_size = L.list_size + LIST_INCREMENT;
}
L.elem[L.length] = e;
L.length++;
return 0;
}
//3.在顺序表指定位置添加元素
int ListInsert(SqList &L, ElemType e, int loc)
{
if(loc < 1 || loc > L.length+1) {//顺序表元素位置的插入范围[1,L.length+1]
fprintf(stderr,"the loc must be [1,%d] !!!\n",L.length+1);
return -1;
}
if(L.length >= L.list_size){//若当前长度已达到顺序表容量,则扩充顺序表
ElemType *newbase = (ElemType*)realloc(L.elem, sizeof(ElemType)*(L.list_size + LIST_INCREMENT));
if(!newbase) {
perror("realloc error");
return -1;
}
L.elem = newbase;
L.list_size = L.list_size + LIST_INCREMENT;
}
if(ListEmpty(L)){//当顺序表为空时 直接插入即可
L.elem[0] = e;
L.length++;
return 0;
}
int i;
for(i = L.length-1; i > loc-1; i--){//插入元素需要移动元素, 故遍历顺序表次序从后到前
L.elem[i+1] = L.elem[i];
}
L.elem[i+1] = L.elem[i];
L.elem[i] = e;
L.length++;//注意:插入完元素之后一定使顺序表长度加1
return 0;
}
//删除顺序表指定位置的元素
int ListDelete(SqList &L, int loc, ElemType &e)
{
if(ListEmpty(L)){
fprintf(stderr, "The loc is null\n");
return -1;
}
if(loc < 1 || loc > L.length){
fprintf(stderr, "The loc must be [1, %d]\n", L.length);
return -1;
}
e = L.elem[loc-1];
int i;
for(i = loc; i < L.length; i++){//i < L.length-1 注意不要把顺序表最后一个元素后面的值(不存在于顺序表中)
//赋值给顺序表 最后一个元素
L.elem[i-1] = L.elem[i] ;
}
L.length--;//删除完元素一定要使顺序表长度减1
return 0;
}
//4.显示顺序表内容函数
void ShowList(const SqList &L)
{
int i = 0;
printf("%d elements:", L.length);
for(i = 0; i < L.length; i++){
printf("%d ",L.elem[i]);
}
return;
}
main.cpp内容
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "SqList.h"
/*当ElemType类型改变是需要注意以下几点:
*1.对ElemType输出均采用%d形式,输出的地方为ShowList()和GetElemType()之后的元素输出
*2.在调用compary函数时,应该注意ElemType能否进行算术比较运算
* 若不能进行,则需要修改ElemType比较类型 或者重载算术比较符
*
*/
//比较两个数大小的函数 用于指针函数的 函数传递
int moreThen(const ElemType first, const ElemType second)
{
//ElemType能够进行比较运行
if(first > second)
return 1;
else
return 0;
}
int equal(const ElemType first, const ElemType second)
{
return( (first==second)?1:0 );
}
int lessThen(const ElemType first, const ElemType second)
{
return( (first<second)?1:0 );
}
//构造初始顺序表并显示
void List_init_data (SqList &L)
{
if(InitList(L) == -1){
perror("InitList error");
exit(EXIT_FAILURE);
}
int i = 0;
for(i = 0; i < 10; i++){
if(ListInsert(L, i * i) == -1){
perror("ListInsert error");
exit(EXIT_FAILURE);
}
}
if(!ListEmpty(L)){
ShowList(L);
printf("\n");
}
}
//测试ListInsert()
void list_insert(SqList &L)
{
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
int loc;
ElemType e;
printf("Enter the location and data(loc data):") ;
scanf("%d%d", &loc, &e);
if( ListInsert(L, e, loc) == -1 ){
fprintf(stderr, "List Insert error\n");
//exit(1);
}
else{
printf("After insert:");
ShowList(L);
printf("\n");
}
return ;
}
//测试 DestroyList()
void destroy_list(SqList &L){
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
if(DestroyList(L) == -1){
fprintf(stderr, "destroy list error\n");
//exit(EXIT_FAILURE);
}
else {
printf("After destroy list:");
printf("elem=%p length=%d list_size=%d\n", L.elem, L.length, L.list_size);
}
return;
}
//测试ClearList()
void clear_list(SqList &L)
{
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
if(ClearList(L) == -1){
fprintf(stderr, "clear list error\n");
//exit(EXIT_FAILURE);
}
else
printf("elem=%p length=%d list_size=%d\n", L.elem, L.length, L.list_size);
return;
}
//测试locateElem()
void locate_elem(const SqList &L)
{
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
ElemType second;
int funCount;
int (*fun[])(ElemType ,ElemType ) = {//此处用到指向函数的指针,并把指向函数的指针用作fun数组成员
moreThen,
equal,
lessThen
};
printf("Enter the second element:");
scanf("%d", &second);
printf("Enter the fun(0->moreThen,1->equal, 2->lessThen):");
scanf("%d", &funCount);
printf("The loc of %d(The compare is %d) in SqList is %d\n", second, funCount, LocateElem(L,second,(*fun[funCount])));
}
//测试PriorElem()
void prior_elem(const SqList &L)
{
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
ElemType cur_e;
ElemType prior_e;
printf("Enter your curent element:");
scanf("%d", &cur_e);
if( PriorElem(L, cur_e, prior_e) == -1){
fprintf(stderr, "Not find the prior of %d.\n", cur_e);
//exit(EXIT_FAILURE);
}
else
printf("The prior of %d in SqList is %d\n", cur_e, prior_e);
}
//测试NextPrior()
void next_prior(const SqList &L)
{
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
ElemType cur_e;
ElemType next_e;
printf("Enter your element:");
scanf("%d", &cur_e);
if( NextElem(L, cur_e, next_e) == -1){
fprintf(stderr, "Not find the next of %d.\n", cur_e);
//exit(EXIT_FAILURE);
}
else
printf("The next of %d in SqList is %d.\n", cur_e, next_e);
}
//测试ListDelete()
void list_delete(SqList &L)
{
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
int loc;
ElemType e;
printf("Enter the loc in SqList that you want to delete:") ;
scanf("%d", &loc);
if( ListDelete(L, loc, e) == -1){
fprintf(stderr, "The element of loc in SqList delete fail.\n");
//exit(EXIT_FAILURE);
}
else{
printf("The element of %d in SqList that you delete is %d\n", loc, e);
if(!ListEmpty(L)){
printf("After list delete, The List is:\n");
ShowList(L);
printf("\n");
}
else
printf("After list delete, The List is null.\n");
}
}
//测试GetElem()
void get_elem(const SqList &L)
{
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
int loc;
ElemType e;
printf("\nEnter loc of the list that you want to get:");
scanf("%d", &loc);
if( GetElem(L,loc, e) == -1){
fprintf(stderr, "Get elem of list error\n");
//exit(EXIT_FAILURE);
}
else
printf("The element of list in %d is %d\n", loc, e);
}
//测试list_show()
void list_show(const SqList &L)
{
if(!L.elem){
printf(" The SqList is not exit.\n");
return ;
}
ShowList(L);
printf("\n");
}
void print()
{
printf("\nthe number of command:\n");
printf("\t0: Quit ");
printf("\t1: ListInsert\n");
printf("\t2: DestroyList");
printf("\t3: ClearList\n");
printf("\t4: LocateElem");
printf("\t5: PriorElem\n");
printf("\t6: NextElem");
printf("\t7: ListDelete\n");
printf("\t8: GetElem");
printf("\t9: ListShow\n");
printf("\t10:ListInit");
printf("\t11:print_cmd\n");
}
int main(int argc, char *argv[])
{
SqList L;
List_init_data(L);
int cmd;
print();
printf("\n======Please enter your command:");
scanf("%d", &cmd);
while(cmd) {
switch(cmd){
case 0:
cmd = 0;
break;
case 1:
list_insert(L);
break;
case 2:
destroy_list(L);
break;
case 3:
clear_list(L);
break;
case 4:
locate_elem(L);
break;
case 5:
prior_elem(L);
break;
case 6:
next_prior(L);
break;
case 7:
list_delete(L);
break;
case 8:
get_elem(L);
break;
case 9:
list_show(L);
break;
case 10:
List_init_data(L);
break;
case 11:
print();
break;
default:
printf("Unknown number %d\n", cmd);
}
printf("\n======Please enter your command:");
scanf("%d", &cmd);
}
//程序正常退出时输出标志
printf("\nThe end, GoodBye...\n");
return 0;
}