请老师赐教线性表搜索问题
程序代码:
#include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 100 #define LIST_INCREAMENT 10 typedef const char * string; typedef int ElemType; enum Status{ success=1,fail=0,fatal=-1,range_error=-2 }; typedef struct Sqlist{ ElemType *elem; int Length; int List_Size; }Sqlist,*Ptr; typedef Ptr SqlListPtr; enum Status List_Init(SqlListPtr L){ enum Status s = success; L->List_Size = LIST_INIT_SIZE; L->Length = 0; L->elem = (ElemType *)malloc(sizeof(ElemType)*L->List_Size); if(L->elem==NULL)s=fatal; return s; } enum Status List_Insert(SqlListPtr L,int pos,ElemType elem){ enum Status s = range_error; if ((pos-1)>=0 && (pos-1)<= L->Length) { if (L&&L->Length<L->List_Size) { for (int i = L->Length-1; i >pos-1; --i) { L->elem[i+1]=L->elem[i]; } L->elem[pos-1]=elem; L->Length++; s=success; } } else s = fail; return s; } enum Status List_delete(SqlListPtr L,int pos) { enum Status s = range_error; if ((pos-1)>=0 && (pos-1)<L->Length) { if (L&&L->Length>0) { for (int i = pos; i < L->Length; ++i) { L->elem[i-1] = L->elem[i]; L->Length--; s = success; } } } else s = fail; return s; } enum Status List_Retrival(SqlListPtr L, int pos, ElemType *elem) { enum Status s = range_error; if(L) { if ((pos-1)>=0 && (pos-1)< L->Length) { *elem = L->elem[pos-1]; s=success; } } else s=fatal; return s; } enum Status List_Locate(SqlListPtr L, ElemType elem, int *pos) { enum Status s =range_error; if (L) { for (int i = 0; i < L->Length; ++i) { if (L->elem[i]==elem) { *pos = i+1; s = success; break; } } } else s = fatal; return s; } int main(){ SqlListPtr L; int *save; printf("%d\n",List_Init(L)); List_Insert(L,1,10); List_Insert(L,2,20); //List_delete(L,1); for (int i = 0; i < L->Length; ++i) { printf("%d\n",L->elem[i]); } printf("%d\n",List_Locate(L,20,save)); return 0; }[local]1[/local]
创建线性表 用的顺序存储结构 插入2个值 在搜索刚插入的一个值时报错 请指教。