|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我这么编 编译通不过
#include<stdio.h>
#include<malloc.h>
#define INIT_SIZE 5
#define INCREM 5
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef struct { //×
ElemType *slist;
int length;
int listsize; //× //不知道定义来干嘛
}Sqlist;
int InitList_sq(Sqlist *L);
int CreateList_sq(Sqlist *L,int n);
int ListInsert_sq(Sqlist *L,int i,ElemType e);
int PrintList_sq(Sqlist *L);
int ListDelete_sq(Sqlist *L,int i);
void ListLocate(Sqlist *L,ElemType e);
//初始化顺序表(建立空表)
int InitList_sq(Sqlist *L){
L.slist=(ElemType*)malloc(INIT_SIZE*sizeof(ElemType));
if(!L.slist) return ERROR;
L.length=0;
// L.listsize=INIT_SIZE; //不知道什么用处
return OK;
}
//创建顺序表
int CreateList_sq(Sqlist *L,int n){
ElemType e;
int i;
for(i=0;i<n;i++)
{
printf("input data %d:",i+1);
scanf("%d",&e);
/* if(!ListInsert_sq(L,i+1,e))
return ERROR; */
}
return OK;
}
// 输出顺序表中的元素
int PrintList_sq(Sqlist *L){
int i;
for(i=1;i<=L.length;i++)
printf("%5d",L.slist[i-1]);
return OK;
}
//在顺序表中插入
int ListInsert_sq(Sqlist *L,int i,ElemType e)
{
int k;
if(i<1||i>L.length+1)
return ERROR;
else
{
for(k=L.length-1;k>=i-1;k--)
L.slist[k+1]=L.slist[k];
L.slist[i-1]=e;
L.length++;
return OK;
}
return OK;
}
//在顺序中删除第i个元素
int ListDelete_sq(Sqlist *L,int i)
{
int j;
if((i<1)||(i >L.length))
return ERROR;
for(j=i;j<=L.length-1;j++)
L.slist[j-1] =L.slist[j];
--L.length;
return OK;
}
//在顺序表中查找指定值元素,返回其序号
void ListLocate(Sqlist *L,ElemType e)
{
int i,z=0;
for(i=0;i<L.length-1;i++)
{
if(L.slist[i]==e)
printf("该值所在表中的序号为:第%d位",i+1);
z=1;
}
if(z==0)
printf("没有该值\n");
}
//主函数
int main()
{
Sqlist sl,L;
int n,i,e;
printf("*********************欢迎来到该界面******************");
printf("please input n(输入顺序表个数):");
scanf("%d",&n);
if(n>0)
{
printf("\n1-Create Sqlist:\n");
InitList_sq(&sl);
CreateList_sq(&sl,n);
printf("输入要插入的值和序号:");
scanf("%d %d",&i,&e);
ListInsert_sq(&sl,i,e);
printf("输出插入后的顺序表:");
PrintList_sq(&sl);
printf("\n");
printf("输入要删除的值元素的序号:");
scanf("%d",&i);
ListDelete_sq(&sl,i);
printf("输出删除后的元素的顺序表:\n");
PrintList_sq(&sl);
printf("\n");
printf("输入要查找的值:");
scanf("%d",&e);
ListLocate(&sl, e);
}
else
printf("ERROR");
return 0;
}
敢问代码是怎么写出来的。指针操作怎么用起 .(点) 来了;L->slist
|
|