顺序表的表示和实现(实验要求及源代码)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

顺序表的表示和实现

须完成的操作:
初始化 Init
销毁 Destroy
查找 Locate
插入 Insert
删除 Delete, DeleteAllx, DeleteSame
输出 ShowList,一行最多5个元素,
测试 testSqList
1. 从文件读入 n (n>10)个整数,填入顺序表;
2. 输出 n 个整数至屏幕;
3. 查找值为某个整数的元素的位序;
4. 在顺序表的头,尾,中间各插入一个元素
5. 输入修改后的表至屏幕,并保存至文件
6. 删除顺序表的任意两个元素,删除所有 x,删除相同元素
7. 输入修改后的表至屏幕,并保存至文件
8. 逆序排列顺序表,输出至屏幕,并保存至文件

#define Elemtype int
#define STATUS int
#define OK 1
#define error 0
#define MEM -1

typedef struct
{
Elemtype *pData;
int nLength;
int nSize;
}Sqlist;

STATUS Init(Sqlist *list,int nSize); /*初始化线性表*/

STATUS Destroy(Sqlist *list); /*销毁线性表*/

STATUS Locate(Sqlist *list,int e); /*查找值为e的元素的位序*/

STATUS Insert(Sqlist *list,int I,int x); /*在I位置之前插入元素x*/

STATUS Delete(Sqlist *list,int D); /*删除第D个元素*/

STATUS DeleteAllx(Sqlist *list,int x); /*删除所有元素x*/

STATUS DeleteSame(Sqlist *list); /*删除相同元素*/

STATUS Showlist(Sqlist *list); /*输出线性表*/

STATUS Getlist(Sqlist *list,int n); /*从文件中读取n个元素至线性表*/

STATUS Save(Sqlist *list); /*将线性表保存在文件中*/

STATUS cShow(Sqlist *list); /*逆序排列顺序表*/





#include
#include
#include
#include "sd1.h"

/*初始化线性表*/

STATUS Init(Sqlist *list,int nSize)
{
list->pData=(Elemtype*)malloc(nSize*sizeof(Elemtype));
list->nLength=0;
if(list->pData==NULL) return error;
list->nSize=nSize;
return OK;
}

/*销毁线性表*/

STATUS Destroy(Sqlist *list)
{
if(list->pData!=NULL)
free(list->pData);
list->nLength=0;
list->nSize=0;
return OK;
}

/*查找值为e的元素的位序,并用w返回其值*/

STATUS Locate(Sqlist *list,int e)
{
int i,x,w;
w=0;
for(i=1;i<=list->nLength;i++)
{
x=list->pData[i];
if(x==e)
{
w=i+1;
printf("\nthe locate of %d is %d .\n",e,w);
}
}
if(w==0) printf("\nthere is no %d in this list .\n",e);
return OK;
}

/*在I位置之前插入元素x*/

STATUS Insert(Sqlist *list,int I,int x)
{
int *p,*q;
if(I<1||I>list->nLength+1) return error;
q=&(list->pData[I-1]);
for(p=&(list->pData[list->nLength-1]);p>=q;--p)
*(p+1)=*p;
*q=x;
++list->nLength;
return OK;
}

/*删除第D个元素*/

STATUS Delete(Sqlist *list,int D)
{
Elemtype *p,*q;
if(D<1||D>list->nLength) return error;
p=&(list->pData[D-1]);
q=list->pData+lis

t->nLength-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--list->nLength;
return OK;
}

/*删除所有元素x*/

STATUS DeleteAllx(Sqlist *list,int x)
{
int i;
for(i=0;inLength;i++)
if(list->pData[i]==x) Delete(list,i+1);
return OK;
}

/*删除相同元素*/

STATUS DeleteSame(Sqlist *list)
{
int i,j;
Elemtype t;
for(i=0;inLength;i++)
{
t=list->pData[i];
for (j=i+1;jnLength;)
{
if (list->pData[j]==t)
Delete(list,j+1);
else
j++;
}
}
return OK;
}

/*输出线性表*/

STATUS Showlist(Sqlist *list)
{
int i,j,k;
printf("\nThese numbers are :\n");
k=list->nLength%5;
for(i=0;inLength-k;)
{
for(j=i;jprintf("%d ",list->pData[j]);
printf("\n");
i=j;
}
for(i=j;inLength;i++)
{
printf("%d ",list->pData[i]);
}
printf("\n");
return OK;
}

/*从文件中读取n个元素至线性表*/

STATUS Getlist(Sqlist *list,int n)
{
int i;
FILE *fp;
fp=fopen("sd1.txt","r+");
for(i=0;ifscanf(fp,"%d ",&list->pData[i]);
list->nLength=n;
fclose(fp);
Showlist(list);
return OK;
}

/*将线性表保存在文件中*/

STATUS Save(Sqlist *list)
{
FILE *fp;
int i;
fp=fopen("sd1.txt","r+");
for(i=0;inLength;i++)
fprintf(fp,"%d ",list->pData[i]);
fclose(fp);
return OK;
}

/*逆序排列顺序表*/

STATUS cShow(Sqlist *list)
{
int i;
Elemtype t;
for (i=0;i<(list->nLength)/2;i++)
{
t=list->pData[i];
list->pData[i]=list->pData[list->nLength-i-1];
list->pData[list->nLength-i-1]=t;
}
return OK;
}

/*主函数*/

STATUS main()
{
int ch,ch1,n,D;
char option;
Elemtype e,I,x,X;
Sqlist list;
Init(&list,100);

printf("\n********顺序表的表示和实现******** \n\n");
printf("从文件读取n个元素至线性表\n");
printf("Please enter n :\n");
scanf("%d",&n);
Getlist(&list,n);
getch();
system("cls");
printf("\nDo you want to continue (Y/N) ?\n");
scanf("%s",&option);

while (option=='y'||option=='Y')
{
printf("\n******菜单******\n");
printf("1.查找位序\n");
printf("2.插入\n");
printf("3.删除\n");
printf("4.逆序排列顺序表\n");

printf("\nPlease enter your choose :\n");
scanf("%d",&ch);
switch (ch)
{
case 1: printf("\nPlease enter the element you want to fine :\n");
scanf("%d",&e);
Locate(&list,e); break;
case 2: printf("\nplease enter the locate and the element you want to insert :\n");
scanf("%d%d",&I,&x);
Insert(&list,I,x);
Save(&list);
printf("\nsucess!\n");
Showlist(&list); break;
case 3: printf("\n*****删除*****\n");
printf("1.删除位序为D的元素\n");
printf("2.删除所有元素X\n");
printf("3.删除相同元素\n");
printf("\nplea

se enter your choose :\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1: printf("\nplease enter the locate of the number you want to delete :\n");
scanf("%d",&D);
Delete(&list,D);
Save(&list);
printf("\nsucess!\n");
Showlist(&list); break;
case 2: printf("\nplease enter the number you want to delete :\n");
scanf("%d",&X);
DeleteAllx(&list,X);
Save(&list);
printf("\nsucess!\n");
Showlist(&list); break;
case 3: DeleteSame(&list);
Save(&list);
printf("\nsucess!\n");
Showlist(&list); break;
}break;
case 4: cShow(&list);
Save(&list);
printf("\nsucess!\n");
Showlist(&list); break;

}
printf("\nDo you want to continue (Y/N) ?\n");
scanf("%s",&option);
system("cls");
}

Destroy(&list);
printf("\nsucess!\n");


return 0;
}

相关文档
最新文档