2006-11-01
设计一个算法,求出线性表中数据域的值为x的结点的序号。序号从表头算起,若链表中无此结点则序号为零.
必须是调试后的程序.
#include#include#include#define ERROR 0#define OK 1#define EQUAL 1#define OVERFLOW -1#define LIST_INIT_SIZE 100#define LISTINCREMENT 10struct STU{char name[20];char stuno[10];int age;int score;}stu[50];typedef struct STU ElemType;struct LIST{ElemType *elem;int length;int listsi...全部
#include#include#include#define ERROR 0#define OK 1#define EQUAL 1#define OVERFLOW -1#define LIST_INIT_SIZE 100#define LISTINCREMENT 10struct STU{ char name[20]; char stuno[10]; int age; int score;}stu[50];typedef struct STU ElemType;struct LIST{ ElemType *elem; int length; int listsize;};typedef struct LIST List;int init(List *L){ L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L->elem) exit(OVERFLOW); L->length=0; L->listsize=LIST_INIT_SIZE; return OK;}/*init */int ListLength(List *L){ return L->length;}void GetElem(List L,int i,ElemType *e){ *e=L。
elem[i];}int EqualList(ElemType *e1,ElemType *e2){ if (strcmp(e1->name,e2->name)==0) return 1; else return 0;}int Less_EqualList(ElemType *e1,ElemType *e2){ if (strcmp(e1->name,e2->name)length;i++) if(EqualList(&La->elem[i],&e)) return 1;break; default:break; } return 0;}void MergeList(List *La,List *Lb,List *Lc){ ElemType *pa,*pb,*pc,*pa_last,*pb_last; pa=La->elem;pb=Lb->elem; Lc->listsize = Lc->length = La->length + Lb->length; pc = Lc->elem = (ElemType *)malloc(Lc->listsize * sizeof(ElemType)); if(!Lc->elem) exit(OVERFLOW); pa_last = La->elem + La->length - 1; pb_last = Lb->elem + Lb->length - 1; while(paL->length+1) return ERROR; q=&(L->elem[i-1]); for(p=&L->elem[L->length-1];p>=q;--p) *(p+1)=*p; *q=e; ++L->length; return OK;}/*ListInsert Before i */main(){ struct STU e; List La,Lb,Lc; clrscr(); printf("\n\n-------------------List Demo is running。
。。----------------\n\n"); printf("First is InsertList function。\n"); init(&La); strcpy( ,"stu1"); strcpy( uno,"100001"); e=80; ore=1000; ListInsert(&La,1,e); strcpy( ,"stu3"); strcpy( uno,"100002"); e=80; ore=1000; ListInsert(&La,2,e); printlist(La); printf("List A length now is %d。
\n\n",La。length); getch(); strcpy( ,"stu5"); strcpy( uno,"100003"); e=80; ore=1000; ListInsert(&La,3,e); printlist(La); printf("List A length now is %d。
\n\n",La。length); getch(); init(&Lb); strcpy( ,"stu2"); strcpy( uno,"100001"); e=80; ore=1000; ListInsert(&Lb,1,e); strcpy( ,"stu4"); strcpy( uno,"100002"); e=80; ore=1000; ListInsert(&Lb,2,e); strcpy( ,"stu6"); strcpy( uno,"100001"); e=80; ore=1000; ListInsert(&Lb,3,e); printlist(Lb); printf("List B length now is %d。
\n\n",Lb。length); getch(); MergeList(&La,&Lb,&Lc); printlist(Lc); getch(); printf("Second is UnionList function。
\n"); printf("Now union List A and List B。。。。。\n"); UnionList(&La,&Lb); printlist(La); printf("List A length now is %d。
\n\n",La。length); getch(); printf("\n\n\n\n\n\nWelcome to visit !\n\n\n\n\n\n\n");}。
收起