数据结构实验2
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《数据结构》实验报告
实验序号:2 实验项目名称:顺序表的操作
l->length=0;
l->ListSize=LIST_INIT_SIZE;
printf("ok");
return 0;
}
}
int ListInsert_Sq(sqlist *L,int i, ElemType e)
{ ElemType *q,*p;
if(i<1 || i>L->length+1) return 1;
if(L->length>=L->ListSize) {
ElemType *newbase;
newbase=( ElemType *)realloc(L->elem,
(L->ListSize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) return 1;
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 0;}
【要求】1、实现顺序表的插入、删除、按值查找等操作;
2、假设构建的是非递减顺序表,设计算法实现从该有序顺序表中删除所有其值重复的元素,使得表中所有元素的值均不同。
四、实验结果与数据处理
1:
2:
附源程序清单:
1.
#include
#include
#define LIST_INIT_SIZE100 // 数组最大值
#define INIT_ERROR-1 //初始化出错判断用
typedef struct tadDATALIST//顺序表结构体
{
int nLength; //长度
int anData[LIST_INIT_SIZE]; //数组
int *pElem;
}List, *pList;
List C; //A和B组合后的新数组
int InitList(pList D)
{
int nLength = 0;
int nInput = 0;
char cOverSign ;
//读取所有整数,输入回车结束输入
while (1)
{
if (scanf("%d", &nInput) != 1) //对于非法出入给出判断
{
printf("非法输入\n");
return INIT_ERROR;
}
D-> anData[nLength] = nInput; //赋值
nLength ++;
if ((cOverSign = getchar()) == '\n') //按下回车键,结束输入
{
return nLength; //返回长度
}
}
}
int Alternate(pList anData_A, pList anData_B)
{
C.nLength = 0; //初始化新数组的长度为0
int nIndex1 = 0;
int nIndex2 = 0;
while((nIndex1 < anData_A -> nLength) && (nIndex2 < anData_B -> nLength)) //当有一个数组到达长度限制后,退出执行后两个while语句中的一个
{
C.anData[C.nLength] = anData_A->anData[nIndex1]; //先把A的第一个元素给c
nIndex1 ++;
C.nLength++;
C.anData[C.nLength] = anData_B->anData[nIndex2]; //接着把B的第一个元素给C
nIndex2 ++;
C.nLength++;
}
while (nIndex1 < anData_A->nLength)
{
C.anData[C.nLength] = anData_A->anData[nIndex1];
nIndex1 ++;
C.nLength++;
}
while (nIndex2 < anData_B->nLength)
{
C.anData[C.nLength] = anData_B->anData[nIndex2];
nIndex2 ++;
C.nLength++;
}
return 0;
}
int main()
{
int nIndex = 0;
List A; //创建结构体的对象
List B;
printf("输入顺序表A的元素,以回车结束输入: ");
if ((A.nLength = InitList(&A)) == INIT_ERROR) //判断是否初始化出错
{
printf("初始化出错\n");
return 0;
}
printf("输入顺序表B的元素,以回车结束输入: "); //判断是否初始化出错if ((B.nLength = InitList(&B)) == INIT_ERROR)
{
printf("初始化出错\n");
return 0;
}
/* printf("%d\n", A.nLength);
printf("%d\n", B.nLength);
for (nIndex = 0; nIndex < A.nLength; nIndex++)
{
printf("%d ", A.anData[nIndex]);
}
printf("\n");
for (nIndex = 0; nIndex { printf("%d ", B.anData[nIndex]); } printf("\n");*/