就是用C语言编写一个小程序 在程序执行的时候可以反复录入数据然后还可以 修改数据 删除数据 查找数据可以再设计几个输入指令 例如 输入 1是 增加数据 输入2为 查找数据 可以使用文件来完成 也可以是临时的请给个源码谢谢 要测试过的哦
很早前帮朋友写的,跟你的需求很像,给你用吧。你可改下main函数,使它更贴近你的需求。如果你不会改,再帮你改吧。-------------------------------------------#include <time.h>#include <stdio.h>#define NULL -2#define ERROR -1#define OK 1#define TRUE 2#define FALSE 3#define Boolen int#define Status int#define LIST_INIT_SIZE 3#define LIST_INCREMENT 2#define NAME_LEN 13#define DES_LEN 30char ErrDescription[DES_LEN];typedef struct{ int NO; char Name[NAME_LEN]; enum{male,female} Sex; int Age; char Tel[15]; char Inserttime[64];}ElemType,*ElemPointer;typedef struct{ ElemPointer base; //基址 int length; //表长 int listsize; //内存占用 int elemcount; //记录数}SqList,*SqPointer;int ErrorEXP(int i){ switch(i) { case 1: strcpy(ErrDescription,"InitList::(ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)) 空间申请失败");break; case 2: strcpy(ErrDescription,"IncreaseList::(ElemType *)realloc(L->base,(L->length + LIST_INCREMENT) * sizeof(ElemType)) 空间申请失败");break; case 3: strcpy(ErrDescription,"if(!L->base) return Error; SqList不存在");break; case 4: strcpy(ErrDescription,"GetElem:: i 越界");break; case 5: strcpy(ErrDescription,"ListInsert:: i 越界");break; case 6: strcpy(ErrDescription,"ListInsert:: CALL IncreaseList(L)==ERROR return Error 邻接空间申请失败,由ListInsert返回");break; case 7: strcpy(ErrDescription,"ListDelete:: i 越界");break; case 8: strcpy(ErrDescription,"KeyInList:: i 越界");break; case 9: strcpy(ErrDescription,"KeyInList:: CALL ListInsert(L,i,temp)==ERROR return Error 邻接空间申请失败,由KeyInList返回");break; case 10: strcpy(ErrDescription,"ScanfList:: CALL KeyInList(L,i++)==ERROR return Error");break; } puts("!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!\n"); puts(ErrDescription); puts("\n!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!\n"); return ERROR;}Status InitList(SqPointer L){ L->base = 0; //不可不要!!! 去掉后即使(ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType))失败,系统也会认为正常 L->base = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)); if(!L->base) return ErrorEXP(1); //空间申请失败,返回 L->length = LIST_INIT_SIZE; L->listsize = L->length * sizeof(ElemType); L->elemcount = 0; return OK;}Status IncreaseList(SqPointer L){ ElemPointer newbase; newbase = (ElemType *)realloc(L->base,(L->length + LIST_INCREMENT) * sizeof(ElemType)); if(!newbase) return ErrorEXP(2); L->base = newbase; L->length += LIST_INCREMENT; L->listsize = L->length * sizeof(ElemType); return OK;}Status DestroyList(SqPointer L){ if(!L->base) return ErrorEXP(3); //L不存在,返回 free(L->base); L->length = NULL; L->listsize = NULL; L->elemcount = NULL; return OK;}Status ClearList(SqPointer L){ if(!L->base) return ErrorEXP(3); //L不存在,返回 L->elemcount = 0; return OK;}Boolen ListEmpty(SqPointer L){ if(!L->base) return ErrorEXP(3); //L不存在,返回 if(L->elemcount == 0) return TRUE; else return FALSE;}int ListElemCount(SqPointer L){ if(!L->base) return ErrorEXP(3); //L不存在,返回 return L->elemcount;}Status GetElem(SqPointer L,int i,ElemType *ret) //调用此函数需将ret指向main函数域某一ElemType变量{ if(!L->base) return ErrorEXP(3); //L不存在,返回 if(i > L->elemcount) return ErrorEXP(4); //i越界,返回 *ret = L->base[i-1]; //i 从1开始 此种方法在main中改变*ret会直接更改链表中数据 return OK;}//重大发现 指针型 temp->base 普通型L.baseint LocateElem(SqPointer L,char Locatename[]) //返回的i从1开始{ int i=0; ElemType *temp; if(!L->base) return ErrorEXP(3); //L不存在,返回 while(i<L->elemcount) { temp=&(L->base[i]); //改为temp=L->base[i++];并去除下面的i++; ?? if(strcmp(temp->Name,Locatename) == 0) return i+1; //不能用temp->Name==locatename来试图比较字符串 i++; } return 0;}Status ListInsert(SqPointer L,int i,ElemType newelem) //插入位置1<=i<=elemcount+1{ ElemPointer newbase; ElemType *temp,*flag; if(!L->base) return ErrorEXP(3); //L不存在,返回 if(i<1 || i>L->elemcount + 1) return ErrorEXP(5); if(L->elemcount == L->length) if(IncreaseList(L)==ERROR) return ErrorEXP(6); flag=&(L->base[i-1]); //插入位置 for(temp=&(L->base[L->elemcount-1]);temp>=flag;temp--) *(temp+1)=*temp; *flag=newelem; L->elemcount++; return OK;}Status ListDelete(SqPointer L,int i,ElemType *ret) //调用此函数需将ret指向main函数域某一ElemType变量{ ElemType *temp; if(!L->base) return ErrorEXP(3); //L不存在,返回 if(i<1 || i>L->elemcount) return ErrorEXP(7); *ret=L->base[i-1]; //删除位置,这里先返回该值 for(temp=&(L->base[i]);temp<=&(L->base[L->elemcount-1]);temp++) *(temp-1)=*temp; L->elemcount--; return OK;}Status KeyInList(SqPointer L,int i){ ElemType temp; time_t t; char tmp[64]; char S; if(!L->base) return ErrorEXP(3); //L不存在,返回 if(i<1 || i>L->elemcount + 1) return ErrorEXP(8); printf("正在输入第%d个元素的值:",i); printf("\n编号:(int)\n"); scanf("%d",&temp.NO); printf("\n姓名:(char *)\n"); scanf("%s",&temp.Name); printf("\n性别:(m or f)\n"); do{ S=getch(); if(S=='m') temp.Sex=male; else if(S=='f') temp.Sex=female; else puts("Key in 'm' or 'f'.\n"); }while(S!='m' && S!='f'); putchar(S); printf("\n年龄:(int)\n"); scanf("%d",&temp.Age); printf("\n电话:(char *)\n"); scanf("%s",&temp.Tel); printf("\n记录时间:\n"); t=time(0); strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t)); puts(tmp); strcpy(temp.Inserttime,tmp); if(ListInsert(L,i,temp)==OK) return OK; else return ErrorEXP(9);}ElemType ScanfElem(){ ElemType temp; time_t t; char tmp[64]; char S; printf("正在录入元素:"); printf("\n编号:(int)\n"); scanf("%d",&temp.NO); printf("\n姓名:(char *)\n"); scanf("%s",&temp.Name); printf("\n性别:(m or f)\n"); do{ S=getch(); if(S=='m') temp.Sex=male; else if(S=='f') temp.Sex=female; else puts("Key in 'm' or 'f'.\n"); }while(S!='m' && S!='f'); putchar(S); printf("\n年龄:(int)\n"); scanf("%d",&temp.Age); printf("\n电话:(char *)\n"); scanf("%s",&temp.Tel); printf("\n记录时间:\n"); t=time(0); strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t)); puts(tmp); strcpy(temp.Inserttime,tmp); return temp;}Status ScanfList(SqPointer L,int i){ char p='c'; while(putchar('\n'),p=='c'||p=='C') { p='\0'; if(KeyInList(L,i++)==ERROR) return ErrorEXP(10); printf("\nPress ESC key to exit or 'C' to continue..."); while(p!='c' && p!='C' && (int)p!=27) p=getch(); } return OK;}Status PrintListProperty(SqPointer L){ puts("SqList L Property:"); if(!L->base) { puts("链表不存在!"); return OK;} else puts("链表已初始化...\n"); printf("%d/%d BASE=%d,MemoryStatus=%d\n",L->elemcount,L->length,L->base,L->listsize); return OK;}Status PrintOnScreen(SqPointer L){ int i; char Stmp[7],t; if(!L->base) return ErrorEXP(3); //L不存在,返回 puts("Push 'C' shell CLS or other key to skip."); t=getch(); if(t=='c' || t=='C') system("cls"); puts("数据表打印:"); for(i=0;i<=L->elemcount-1;i++) { printf("\nElem %d st:\n",i+1); if(L->base[i].Sex == male) strcpy(Stmp,"male"); else if(L->base[i].Sex == female) strcpy(Stmp,"female"); else strcpy(Stmp,"Unknow"); printf("NO:%d\tName:%s\t\tSex:%s\tAge:%d\n\tTel:%s\n\tInsertTime:%s\n",L->base[i].NO,L->base[i].Name,Stmp,L->base[i].Age,L->base[i].Tel,L->base[i].Inserttime); } return OK;}Status PrintElem(ElemPointer elem){ char Stmp[7]; printf("\nPrintElem:\n"); if(elem->Sex == male) strcpy(Stmp,"male"); else if(elem->Sex == female) strcpy(Stmp,"female"); else strcpy(Stmp,"Unknow"); printf("NO:%d\tName:%s\t\tSex:%s\tAge:%d\n\tTel:%s\n\tInsertTime:%s\n",elem->NO,elem->Name,Stmp,elem->Age,elem->Tel,elem->Inserttime); return OK;}void main() //把以上所有函数都串了起来{ SqList TheList; SqPointer ListP; ElemType mylistelem,*elemtemp; ElemPointer mylist; int i; char nameT[20]; elemtemp=&mylistelem; //*ret ListP=&TheList; if(InitList(ListP)==OK) puts("InitList(TheList)==OK"); PrintListProperty(ListP); if(ListEmpty(ListP)==TRUE) puts("ListEmpty==True"); else puts("ListEmpty==False"); ScanfList(ListP,1); PrintListProperty(ListP); PrintOnScreen(ListP); printf("ListElemCount return %d.",ListElemCount(ListP)); puts("\nGetElem index? "); scanf("%d",&i); if(GetElem(ListP,i,elemtemp)==OK) PrintElem(elemtemp); puts("\nLocateElem name? "); scanf("%s",nameT); printf("LocateElem return %d.",LocateElem(ListP,nameT)); puts("\nListDelete index? "); scanf("%d",&i); if(ListDelete(ListP,i,elemtemp)==OK) PrintElem(elemtemp); puts("\nListInsert index? "); scanf("%d",&i); puts("\nListInsert NEWELEM? "); ListInsert(ListP,i,ScanfElem()); PrintListProperty(ListP); PrintOnScreen(ListP);if(ClearList(ListP)==OK) puts("ClearList==OK"); if(ListEmpty(ListP)==TRUE) puts("ListEmpty==True"); if(DestroyList(ListP)==OK) puts("DestroyList==OK"); getch();}/* 函数列表 类型 名称 参数 说明int ErrorEXP (int i) 错误描述符Status InitList (SqPointer L) 初始化SqPointer L... 通过L返回baseStatus IncreaseList (SqPointer L) L当前满时,继续申请空间Status DestroyList (SqPointer L) 销毁LStatus ClearList (SqPointer L) 把L置为空表Boolen ListEmpty (SqPointer L) 判断L是否为空表,是则返回TRUEint ListElemCount (SqPointer L) 返回当前L中记录的元素个数Status GetElem (SqPointer L,int i,ElemType *ret) 通过*ret返回i号元素int LocateElem (SqPointer L,char Locatename[]) 顺序查找表,根据name字段,返回首个匹配元素的i,无则返回0Status ListInsert (SqPointer L,int i,ElemType newelem) 在L中的i号位置插入newelem元素Status ListDelete (SqPointer L,int i,ElemType *ret) 删除L中第i号元素,并用*ret返回该元素Status KeyInList (SqPointer L,int i) 从键盘输入单个元素并插入到i号位置ElemType ScanfElem () 从键盘输入单个元素返回一个ElemType类型的节点Status ScanfList (SqPointer L,int i) 从i号开始递增顺序录入元素到L,直到按'ESC'Status PrintListProperty(SqPointer L) 打印L的属性,打印格式为(已用空间/已申请空间 基址 内存占用)Status PrintOnScreen (SqPointer L) 打印整张L表到屏幕Status PrintElem (ElemPointer elem) 打印单个ElemType类型的元素时间仓促,所以乱了些,书上2章开头 动态线性的顺序表 的基本操作几乎都写了 不知你说的是不是这个,mian函数比较乱,只是把所有的基本操作都串了起来,你 可以根据情况改改主函数的调用过程,就会比较清楚是怎么实现的了。你可以按F10 进行单部跟踪,F11可以进入调用过程,一步一步跟着程序走一遍就好了。 关于动态链表的我之前写过一个,也好象给你看过,这里再附上一起发过去。文件LinkList.c 只实现了构造链表,并打印出来的功能。*/