数据结构顺序表的初始化,插入删除,取数据元素,查找遍历,顺序表的长度

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

#include
#include
#define NULL 0
#define MAXSIZE 1024 /*顺序表可能的最大长度,假设为1024 */
typedef int elemtype; /*elemtype可为任意类型,假设为int型 */
typedef struct sequlist
{ elemtype data[MAXSIZE]; /*定义顺序表为一维数组*/
int last; /*last为表中最后一个数据元素的下标位置*/
}SequenList; /*顺序表的结构类型为SequenList*/

/*函数声明*/
SequenList * Init_SequenList( );
int SequenList_Length(SequenList*L);
int Insert_SequenList(SequenList*L,elemtype x,int i);
void menu();
int Delete_SequenList(SequenList*L,int i);
elemtype GetData_SequenList(SequenList*L,int i);
int Search_SequenList(SequenList*L,elemtype key);
void Print_SequenList(SequenList*L);
void SequenList_Iuput(SequenList*L);
/*主函数*/
void main()
{ char cmd;
int i,Length,isdo;
elemtype x ,data,key;

SequenList *L=NULL;
system("cls");
menu();
while((cmd=getchar())!='#')
{ switch(cmd)
{ case '1': L=Init_SequenList( );
printf("\nCreating the list!\n");
printf("\n\n\n\t\t\t");
break;
case '2': L=SequenList_Iuput(L);

printf("\nInputing data ....\n");
printf("\n\n\n\t\t\t");
break;
case '3': Length=SequenList_Length(L);
printf("\nThe Length of the list is %d\n",Length);
printf("\n\n\n\t\t\t");
break;
case '4': printf("input....",i,x);
scanf("%d%d",&i,&x);
x=Insert_SequenList( L,x,i);
printf("\nInserting the data into the list!\n");
printf("\n\n\n\t\t\t");
break;
case '5':
isdo=Delete_SequenList(L,i);
printf("\nDeleting the data in the list......\n");
printf("\n\n\n\t\t\t");
break;
case '6':
printf("%d",i);
scanf("%d",&i);
x= GetData_SequenList(L,i);
if(x!=0)
printf("%d%d",i,x);
printf("\nGeting the data of the position...\n");
printf("\n\n\n\t\t\t");
break;
case '7': data=Search_SequenList(L,key);
printf("\nSearching the data ...\n");
printf("\n\n\n\t\t\t");
break;
case '8':
printf("\nDisplaying the all data of the list!...");
printf("\n\n\n\t\t\t");
break;
}
fflush(stdin);
system("pause");
menu();
}
}
void menu()
{ system("cls");
printf("\t\t1-initial list\n");
printf("\t\t2-input data\n");
printf("\t\t3-get length\n");
printf("\t\t4-insert a value\n");
printf("\t\t5-delete\n");
printf("\t\t6-get the data of the i position\n");
printf("\t\t7-search a data\n");
printf("\t\t8-display\n");
printf("\t\t#-quit\n");
printf("Please select: ");
}
/*函数定义*/
SequenList * Init_SequenList( )
{ SequenList *L; /*定义顺序表指针变量*/
L = (SequenList *) malloc ( sizeof( SequenList ) );
/*申请分

配内存空间*/
L->last = -1; /*设置顺序表的长度last*/
return L; /*返回顺序表的首地址*/
}
/*其他函数定义*/

int SequenList_Length(SequenList*L)
{
return(L->last+1);
}
int Insert_SequenList(SequenList*L,elemtype x,int i)
/*在顺序表中指定的位置插入值为x的结点*/
/*L是SequenList类型的指针变量*/
/*x是待插入结点的数据元素值,i是在顺序表中的插入位置*/
{ int j;
if(L->last>=MAXSIZE-1) /*检查顺序表是否已满*/
{ printf("表满!数据溢出\n"); /*顺序表满,输出溢出错误信息*/
return NULL; /*插入失败,函数返回0*/
}
if(i<1||i>L->last+2) /*插入位置有效性检查,若位置非法,则失败*/
{ printf("非法位置!\n"); /*输出非法位置出错信息*/
return NULL; /*插入失败,函数返回0*/
}
for(j=L->last;j>=i-1;j--) /*在第i个位置插入新结点x*/
L->data[j+1]=L->data[j]; /*结点依次向后移动一个位置*/
L->data[i-1]=x; /*插入x到第i个位置*/
L->last=L->last+1; /*将顺序表长度加1*/
return(1); /*插入成功,函数返回1*/
}

int Delete_SequenList(SequenList*L,int i)
{ int j;
if(i<1||i>L->last+1)
{ printf("非法位置!\n");
return NULL;
}
else
{ for(j=i;j<=L->last;j++)
L->data[j-1]=L->data[j];
L->last--;
}
return(1);
}

elemtype GetData_SequenList(SequenList*L,int i)
{
if(i<1||i>L->last+1)
{printf("error\n");
return NULL;}
else
return(L->data[i-1]);
}

int Search_SequenList(SequenList*L,elemtype key)
{int i;
for(i=0;i<=L->last;i++)
if(L->data[i]==key)
return(i+1);
return(0);
}

void Print_SequenList(SequenList*L)
{int i;
for(i=0;i<=L->last;i++)
{printf("a[%2d]=%4d\t",i+1,L->data[i]);
if((i+1)%5==0) printf("\n");
}
}


void SequenList_Iuput(SequenList*L)
{
int iy,ip;
printf("请输入数据直到输入0结束:\n");
scanf("%d",&ip);
while(ip!=0)
{
iy=L->last+1;
L->data[iy]=ip;
L->last++;

scanf("%d",&ip);
}

}

相关文档
最新文档