老师布置了作业,大佬们帮看看,一脸mengbia
//#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;{:5_107:}
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);
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=L->slist;
}
L->slist=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;
for(p=i-1;p<L->length-1;p++)
L->slist=L->slist;
L->length--;
return OK;
}
/*在顺序表中查找指定元素值,返回其序号,还有这个*/
int ListLocate(Sqlist*L,ElemType e){
int i=0;
while(i<L->length&&L->slist!=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;
} 就是这个程序实现以上顺序表功能怎么改啊,主函数不会写,删除和查找也不知道对不对。。。。。。是不是没充钱,怎么没老哥管我啊{:5_92:} insert()函数:头插法、尾插法
查找:根据指针为遍历元素进行遍历,时间复杂度为O(n)
main函数里面,先创建一个空表的head,不要放暑数据;接着让head指向first元素,指定first的data域……这都可以调用封装好的函数
页:
[1]