包含的文件
c2-1.h是动态分配的顺序表存储结构。
bo2-1.cpp是基于顺序表的基本操作
1.InitList():初始化线性表。
2.DestroyList():销毁线性表。
3.ClearList():置空线性表。
4.ListEmpty():是否为空表。
5.ListLength():表的长度。
6.GetElem():获取第i个元素值。
7.LocateElem():获取与e判定正确的位置。
8.PriorElem():返回前驱。
9.NextElem():返回后驱。
10.ListInsert():插入。
11.ListDelete():删除。
12.ListTraverse():依次访问每个元素。main2-1.cpp是检验bo2-1.cpp各项操作是否正确的主函数。
代码(3个)
1.c2-1.h
/************************************
FileName: c2-1.h
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/6
Description: Dynamic allocation sequence storage structure.
Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/
#define LIST_INIT_SIZE 10 // init allocation memory
#define LISTINCREMENT 2 // increase allocation memory
struct SqList
{
ElemType *elem; // allocation memory base
int length; // the allocated memory size
int listsize; // the allocation memory size
};
2.bo2-1.cpp
/************************************
FileName: bo2-1.cpp
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/7
Description: This file represent the page 19 in the book,
include 12 operations
Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/
//#include "Header.h"
//#include "c2-1.h"
Status InitList(SqList &L)
{
// crate List
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem)
{
exit(OVERFLOW);
}
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
Status DestroyList(SqList &L)
{
// destroy List
free(L.elem);
L.elem = NULL;
L.length = 0;
L.listsize = 0;
return OK;
}
Status ClearList(SqList &L)
{
// clear list
L.length = 0;
return OK;
}
Status ListEmpty(SqList &L)
{
// whether the list is empty
if (L.length == 0)
return TRUE;
else
return FALSE;
}
Status ListLength(SqList &L)
{
// get the length of the list
return L.length;
}
Status GetElem(SqList &L, int i, ElemType &e)
{
// get eth of the list
if (i<1 || i>L.length)
{
exit(ERROR);
}
e = *(L.elem + i - 1);
return OK;
}
Status LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType))
{
// find the position with match
ElemType *p;
int i = 1;
p = L.elem;
while (i <= L.length&&!compare(*p++, e))
{
i++;
}
if (i <= L.length)
return i;
else
return 0;
}
Status PriorElem(SqList L, ElemType cur_e, ElemType &pre_e)
{
// return the front element of cur_e
int i = 2;
ElemType *p = L.elem + 1;
while (i <= L.length&&*p != cur_e)
{
p++;
i++;
}
if (i > L.length)
{
return INFEASIBLE;
}
else
{
pre_e = *--p;
return OK;
}
}
Status NextElem(SqList L, ElemType cur_e, ElemType &next_e)
{
// return the front element of cur_e
int i = 1;
ElemType *p = L.elem;
while (i < L.length&&*p != cur_e)
{
p++;
i++;
}
if (i == L.length)
{
return INFEASIBLE;
}
else
{
next_e = *++p;
return OK;
}
}
Status ListInsert(SqList &L, int i, ElemType e)
{
// insert e in the ith of the list
ElemType *newbase, *q, *p;
if (i < 1 || i>L.length + 1)
{
return ERROR;
}
if (L.length >= L.listsize)
{
newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(ElemType) );
if (!newbase)
exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
q = L.elem+i - 1;
for (p = L.elem+L.length - 1; p >= q; --p)
*(p + 1) = *p;
*q = e;
++L.length;
return OK;
}
Status ListDelete(SqList &L, int i, ElemType &e)
{
// delete ith of list
ElemType *q, *p;
if (i < 1 || i>L.length)
{
return ERROR;
}
p = L.elem + i - 1;
e = *p;
q = L.elem + L.length - 1;
for (++p; p <= q; ++p)
{
*(p - 1) = *p;
}
L.length--;
return OK;
}
Status ListRraverse(SqList L, void(*vi)(ElemType&))
{
// use vi function to L in sequence
ElemType *p;
int i;
p = L.elem;
for (i = 1; i <= L.length; i++)
{
vi(*p++);
}
cout << endl;
return OK;
}
3.main2-1.cpp
/************************************
FileName: main2-1.cpp
Author: Zhengjun Zhao
Version: V1.0
Date: 2016/3/8
Description: bo2-1.cpp is verified by correct this file
Ohter: NULL
History:
1.Date:
Author:
Modification:
/************************************/
#include "Header.h"
typedef int ElemType;
#include "c2-1.h"
#include "bo2-1.cpp"
// compare
Status comp(ElemType c1, ElemType c2)
{
if (c1 == c2*c2)
return TRUE;
else
return FALSE;
}
void visit(ElemType &c)
{
cout << c << endl;
}
// information of List
void InfoList(SqList L)
{
cout << "L.elem = " << L.elem << endl;
cout << "L.length = " << L.length << endl;
cout << "L.listsize = " << L.listsize << endl;
}
void main()
{
SqList L;
Status i;
ElemType e, e0;
int j, k;
k = 20;
// initialize list
i = InitList(L);
if (i)
{
cout << "initialize success.\n";
InfoList(L);
}
cout << endl;
// inset elements
for (j = 1; j <= 12; j++)
{
k = k + 2;
i = ListInsert(L, j, k);
if (i)
cout << "*(L.elem+" << j - 1 << ") = "
<< *(L.elem + j - 1) << endl;
}
cout << endl;
InfoList(L);
cout << endl;
GetElem(L, 5, e);
cout << "the 5th element is " << e << endl;
ListDelete(L, 5, e);
cout << "the delete element is " << e << endl;
InfoList(L);
PriorElem(L, 36, e);
cout << " before the element 36 is " << e << endl;
NextElem(L, 36, e0);
cout << " next the element 36 is " << e0 << endl;
e = LocateElem(L, 6, comp);
cout << "the locate of the square of 6 in " << e << "th." << endl;
ListRraverse(L, visit);
ClearList(L);
cout << "after clear: " << endl;
InfoList(L);
i = ListEmpty(L);
cout << "EmptyList?\n" << i << endl;
DestroyList(L);
InfoList(L);
}