PS:是模仿别人的来写的,调试成功,注意指针的指针用法,还有函数指针作为实参的调用形式
#include<iostream.h> #include<stdlib.h> typedef struct Person { char name[20]; int age; } ElemType; typedef struct LinkNode { ElemType data; struct LinkNode *next; } *Point; typedef struct { Point head,tail; int length; }List; bool MakeNode(Point *p,ElemType e); void FreeNode(Point *p); void InitList(List *L); void DestoryList(List *L); bool ListEmpty(List L); int ListLength(List L); ElemType GetElem(Point p); int Compare(Point p,ElemType e); int LocateElem(List L,ElemType e,int *n,int (*Compare)(Point,ElemType)); void ClearList(List *L); void SetElem(Point *p,ElemType e); void ListInsert(List *L,Point *p,Point s); void Visit(Point *p,ElemType e ); void ListTraverse(List*L,void (*Visit)(Point *,ElemType)); void Print(List L); //******************************************主函数************************************************************// void main() { ElemType e1={"yang1",20},e2={"yang2",21},e3={"yang3",22},e4={"yang4",23},e5={"yang5",24},e6={"yang6",25},e0={"yang0",0}; Point q1,q2,q3,q4,q5,q6,q0; MakeNode(&q1,e1); MakeNode(&q2,e2); MakeNode(&q3,e3); MakeNode(&q4,e4); MakeNode(&q5,e5); MakeNode(&q6,e6); MakeNode(&q0,e0); // SetElem(&q1,e3); List L1; InitList(&L1); L1.head=L1.tail=q1; L1.length=1; ListInsert(&L1,&q1,q2); ListInsert(&L1,&q2,q3); ListInsert(&L1,&q3,q4); ListInsert(&L1,&q4,q5); ListInsert(&L1,&q5,q6); ListInsert(&L1,q0); Print(L1); ListTraverse(&L1,e0,Visit); //注意函数指针的调用形式 cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`\n"; Print(L1); int s; int b=LocateElem(L1,e4,&s,Compare ); //注意函数指针的调用形式 if(b) { cout<<"在"<<s<<"处找到!\n"; } } //*******************************函数**************************************************// bool MakeNode(Point *p,ElemType e) { *p=(Point)malloc(sizeof(LinkNode)); if(!(*p)) { cout<<"分配内存失败\n"; return false; } else { (*p)->data=e; (*p)->next=NULL; return true; } } void FreeNode(Point *p) { free(*p); *p=NULL; } void InitList(List *L) { L->head=NULL; L->tail=NULL; L->length=0; } void DestoryList(List *L) { free(L->head); free(L->tail); L->length=0; } bool ListEmpty(List L) { if(L.length==0) return true; else return false; } int ListLength(List L) { return L.length; } ElemType GetElem(Point p) { return p->data; } int Compare(Point p,ElemType e) { int i=0; ElemType tmp=p->data; for( i=0;tmp.name[i]!='\0';i++) { if(tmp.name[i]!=e.name[i]) return 0; } if (tmp.name[i]!=e.name[i]) { return false; } if(tmp.age!=e.age) return false; else return true; } // int b=LocateElem(L1,Compare ); int LocateElem(List L,ElemType)) { Point tmp ; tmp=L.head; // for(int i=0;(tmp)!=NULL;i++) // { // if(!Compare(tmp,e)) // { /// tmp=tmp->next; // continue; // } // else // { // *n=i+1; // return true; // } // } // return false; for(int i=0;i<L.length;i++) { if(Compare(tmp,e)) { *n=i+1; return 1; } tmp=tmp->next; } cout<<"木有找到"<<endl; return 0; } void ClearList(List *L) { Point p=L->head,pp; while(p) { pp=p->next; free(p); p=pp; } L->length=0; L->head=NULL; L->tail=NULL; } //SetElem(&q1,e3); void SetElem(Point *p,ElemType e) { (*p)->data=e; } // ListInsert(&L1,q2); void ListInsert(List *L,Point s) { if((*p)==L->tail) { s->next=L->tail->next; L->tail->next=s; // (*p)=s; L->tail=s; } else { s->next=(*p)->next; (*p)->next=s; // (*p)=s; } L->length++; } void Visit(Point *p,ElemType e ) { int i; for( i=0;e.name[i]!='\0';i++) { (*p)->data.name[i]=e.name[i]; } (*p)->data.name[i]=e.name[i]; (*p)->data.age=e.age; } void ListTraverse(List*L,ElemType)) { Point p=L->head; for(int i=0;i<L->length;i++) { Visit(&p,e); p=p->next; } } void Print(List L) { if(L.length==0) { cout<<"空表!!!\n"<<endl; return; } Point p=L.head; for(int i=0;i<L.length;i++) { cout<<"第"<<i+1<<"个链表:\n"; cout<<"name:"<<p->data.name<<" "<<"age:"<<p->data.age<<endl<<endl;; p=p->next; } }