1、《数据结构》课程设计报告 一、课程设计的内容、要求 1 线性表的另一种实现. 对顺序表空间被耗尽问题的一个解决办法是:当数组溢出时,用一个更大的数组替换该数组。一个较好的法则是:当出现溢出时,数组长度加长一倍具有较高的时间和空间效率。参照教材中顺序表的有关内容,按上面的要求实现顺序表,并测试当数组溢出时你的实现的运作情况。 二、所采用的数据结构 ADT List { 数据对象: D = {ai|ai ∈ElemSet, i=1,2…n>=0} 数据关系: R1={〈ai-1, ai>|ai-1, ai∈D, i=1,2,…,n} 基本操作: void IniList(S
2、qList& L); void DestroyList(SqList& L); bool ListEmpty(SqList L); int ListLength(SqList L); void GetElem(SqList L, int i, Elem &e); bool PriorElem(SqList L, Elem cur_e, Elem &pre_e); bool NextElem(SqList L, Elem cur_e, Elem &next_e); void ListInsert(SqList &L, int i, Elem e); void ListDelete
3、SqList &L, int i); void ClearList(SqList& L); } 三、主要模块(或函数)及其功能 typedef struct LIST { ElemType *data; int size; int max_size; }LIST; void InitList(LIST *list)//初始化 { list—〉data = (int*)malloc(sizeof(ElemType)*INIT_SIZE); list—〉size = 0; list->max_size = INIT_SIZE; } void Dest
4、royList(LIST &list) { list.size = 0; list。max_size = 0; free(list.data); } bool ListEmpty(LIST list) { if(list。size 〉 0) return false; else return true; } int ListLength(LIST list) { return list.size; } bool GetElem(LIST list,int i,ElemType &e) { if(i < 1 || i > li
5、st。size) return false; else { e = list。data[i]; return true; } } int LocateElem(LIST list, ElemType e,bool (*compare)(ElemType, ElemType)) { // 在顺序线性表L中查找第1个值与e满足compare()的元素的位序. // 若找到,则返回其在L中的位序,否则返回0。 int i; ElemType *p; i = 0; p = list。data; while (i <=
6、list.size && !(*compare)(*p++, e)) ++i; if (i <= list。size) return i; else return 0; } bool PriorElem(LIST list,int cur_e,int &pre_e)//前驱 { if(cur_e < 0 || cur_e 〉 list.size ) return false; else { pre_e = cur_e - 1; return true; } } bool NextElem(LIST list,int cur_e,int &
7、next_e)//后继
{
if(cur_e < 0 || cur_e > list。size) return false;
else
{
next_e = cur_e + 1;
return true;
}
}
void Insert(LIST *list,ElemType value)
{
if(list—〉size>=list—〉max_size)
{
int i;
ElemType *temp = (int*)malloc(sizeof(ElemType)*list—〉size*2);
cout〈 8、量改变:原大小为”〈〈list—〉max_size;
for(i=0;i〈list->size;i++)
{
temp[i] = list->data[i];
}
free(list—〉data);
list-〉data = temp;
list-〉max_size*=2;
cout〈<"改变后大小"< 9、pe value)
{
if(list-〉size>=list->max_size)
{
int i;
ElemType *temp = (int*)malloc(sizeof(ElemType)*list-〉size*2);
cout〈〈endl〈<”线性表原容量改变:原大小为”〈 10、e*=2;
cout〈<”改变后大小”〈 11、emType *e)//删除一个元素
{
int j;
*e=list—〉data[i];
for(j=i+1;j〈=list-〉size—1;j++)
list—〉data[j-1]=list-〉data[j];
list—>size—-;
}
void Print_list(LIST *list)
{
int i;
if(list—>size == 0)
{
cout〈〈"当前线性表内没有元素。"<〈endl;
}
else
{
cout〈〈”线性表内的元素有:”;
for(i=0;i 12、
{
cout<〈list—〉data[i]〈<” ";
}
cout〈〈endl;
}
}
void ClearList(LIST *list)
{
list—〉size = 0;
free(list—〉data);
InitList(list);
cout〈〈"线性表已清空,当前元素个数为"〈〈list->size<<",线性表容量为”〈〈list—>max_size< 13、 {
cout<<"循环输入整数元素(输入#结束):”;
while(1)
{
int n;
fflush(stdin);//清空输入缓冲区,避免缓冲区内残存读取函数无法取走的内容
if(!((cin>>n)。good()))
{
cin.clear();
char ch;
cin〉〉ch;
if(ch==’#’)
break;
else
{
cout<〈"您的输入有误,请重新输入"< 14、 else
{
Insert(&list,n);
cout<<"当前线性表的元素个数为"〈〈list.size〈〈”,线性表的容量为"<〈list.max_size〈 15、 else if(i == 1)
{
int idx;
do
{
if(!cin.good())
{
cin.clear();
cout〈〈"您的输入有误,请重新输入:"〈 16、大于元素的个数,将插入到末端.”〈 17、e if(i ==2)
{
int i;
do
{
if(!cin.good())
{
cin。clear();
cout<<”您的输入有误,请重新输入:”〈 18、效。"< 19、容量为2
2
当前线性表的元素个数为2,线性表的容量为2
3
线性表原容量改变:原大小为2改变后大小4
当前线性表的元素个数为3,线性表的容量为4
#
线性表内的元素有:1 2 3
输入0 清空线性表,输入1指定位置插入元素,输入2指定位置删除元素,输入3退出:
1
请输入要插入元素的位置:0
请输入要插入整数的值:5
线性表内的元素有:5 1 2 3
输入0 清空线性表,输入1指定位置插入元素,输入2指定位置删除元素,输入3退出:
1
请输入要插入元素的位置:6
警告:输入的位置大于元素的个数,将插入到末端.
请输入要插入整数的值:10
线性表原容量改变:原大小为4改变后大小8
线性表内的元素有:5 1 2 3 10
输入0 清空线性表,输入1指定位置插入元素,输入2指定位置删除元素,输入3退出:
2
请输入要删除元素的位置:0
线性表内的元素有:1 2 3 10
输入0 清空线性表,输入1指定位置插入元素,输入2指定位置删除元素,输入3退出:
3
循环输入整数元素(输入#结束):#
线性表内的元素有:1 2 3 10
输入0 清空线性表,输入1指定位置插入元素,输入2指定位置删除元素,输入3退出:
第 8 页 共 8 页
size;i++)
{
temp[i] = list—〉data[i];
}
free(list->data);
list—>data = temp;
list—>max_siz
size;i++






