|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//#include"stdafx.h",主要是主函数完全不会写,怎么实现全部功能
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define INIT_SIZE 5
#define INCREM 5
typedef int ElemType;
typedef struct Sqlist{
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 PrintfList_sq(Sqlist *L);
int ListDelete_sq(Sqlist *L,int i,ElemType e);
int ListLocte_sq(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;
}/*InitList*/
//创建线性表
int CreateList_sq(Sqlist *L,int n){
ElemType e;
int i;
for(i=0;i<n;i++)
{
printf("input data %d",i+1);
printf(":");
scanf("%d",&e);
if(!ListInsert_sq(L,i+1,e))
return ERROR;
}
return OK;
}/*CreatList*/
//输出顺序表中的元素
int PrintList_sq(Sqlist *L){
int i;
for(i=1;i<=L->length;i++)
printf("%5d",L->slist[i-1]);
return OK;
}/*PrintLis*/
//在顺序表中插入
int Listinsert_sq(Sqlist*L,int i,ElemType e){
int k;
if(i<1||i>L->length+1)
return ERROR;
if(L->length>=L->listsize){
L->slist=(ElemType*)realloc(L->slist,(INIT_SIZE+INCREM)*sizeof(ElemType));
if(!L->slist)
return ERROR;
L->listsize+=INCREM;
}
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;
}/*ListInsert*/
/*在顺序表中删除第i个元素,这个帮我看看*/
int ListDelete_sq(Sqlist *L,int i,ElemType e){
int p;
if((i<1)||(i>L->length))return ERROR;
i--;
e=L->slist[i];
for(p=i-1;p<L->length-1;p++)
L->slist[p]=L->slist[p+1];
L->length--;
return OK;
}
/*在顺序表中查找指定元素值,返回其序号,还有这个*/
int ListLocate(Sqlist*L,ElemType e){
int i=0;
while(i<L->length&&L->slist[i]!=e)
i++;
if(i>=L->length)
return ERROR;
else
return i+1;
}
int main(){
Sqlist sl;
int n;
printf("please input n:"); /*输入顺序表的元素个数*/
scanf("%d",&n);
if(n>0){
printf("\n1-Create Sqlist:\n");
InitList_sq(&sl);
CreateList_sq(&sl,n);
ListInsert_sq(&sl,3,&k);//这几行我编的,老哥帮改改
ListDelete_sq(&sl,4,5);
ListLocate(&sl,2);
printf("\n2-Print Sqlist:\n");
PrintList_sq(&sl);
}else
printf("ERROR");
return 0;
} |
|