菜鸟之路--线性表__链表实现
//好久没发博客了。今天来水上一发。以后要勤劳耕作!!!!!。!
!!!!
! //在使用指针的时候。要注意指针的维护。。
。
。。 /* 文件link.h 链表的头文件,定义了链表的存储结构。以及操作方法 */ #ifndef LINK_L #define LINk_L 0 #define OK 1 #define FALSE 0 #define ERROR -1 typedef int Status; //typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode * next; }Link,* Position; typedef struct{ Link * head; Link * tail; } LinkList; Status MakeNode(Link **p,ElemType e); void FreeNode(Link **p); Status ListEmpty(LinkList *l); Status InitList(LinkList **l); Status DestroyList(LinkList *l); Status ClearList(LinkList *l); Status InsFirst(LinkList *l,Link *h,Link *s); Status DelFirst(LinkList *l,Link *h,Link *q); Status Append(LinkList *l,Link *s); Status Remove(LinkList *l,Link *q); Status InsBefore(LinkList *l,Link *q,Link *s); Status InsAfter(LinkList *l,Link *q,Link *s); Status SetCurElem(Link *q,ElemType e); ElemType GetCurElem(Link *q); Status ListEmpty(LinkList *l); int ListLength(LinkList *l); Position GetHead(LinkList *l); Position GetLast(LinkList *l); Position PriorPos(LinkList *l,Link * p); Position NextPos(LinkList *l,Link * p); Status LocatePos(LinkList *l,int i,Link *p); Position LocateElem(LinkList *l,ElemType e,Status (* compare)(ElemType,ElemType)); Status LocateTraverse(LinkList *l,Status (* visit)()); Status MergeList_L(LinkList *la,LinkList *lb,LinkList *lc,int (* compare)(ElemType,ElemType)); Status ListSort(LinkList *l,int (* compare)(ElemType,ElemType)); #endif // LINK_L /* 文件link.cpp 包括了方法的实现,以及新增的辅助方法 */ #include<stdio.h> #include<stdlib.h> #include "Link.h" Status MakeNode(Link **p,ElemType e){ *p = (Link *)malloc(sizeof(Link)); if(p != NULL){ (*p)->data = e; (*p)->next = NULL; return OK; } return FALSE; } void FreeNode(LinkList *l,Link **p){ if(*p == l->tail){ l->tail = PriorPos(l,*p); } l->tail->next=NULL; free(*p); *p= NULL; } Status InitList(LinkList **l){ *l = (LinkList *)malloc(sizeof(LinkList)); Link *tnode; MakeNode(&tnode,0); if(*l != NULL){ (*l)->head = tnode; (*l)->tail = tnode; return OK; } return FALSE; } Status DestroyList(LinkList *l){ free(l); return OK; } Status ClearList(LinkList *l){ if(l != NULL){ if(l->head->next){ Link * temp = l->head; l->head = l->head->next; FreeNode(&temp); } l->tail = l->head; return OK; } return FALSE; } //在链表的第一个位置插入节点。h指向头结点 Status InsFirst(LinkList *l,Link *h,Link *s){ s->next = h->next; h->next = s; return OK; } //在删除链表中第一个节点并以q返回,h为头结点指针 Status DelFirst(LinkList *l,Link *h,Link *q){ q = h->next; h->next = h->next->next; return OK; } //再链表尾部插入节点 Status Append(LinkList *l,Link *s){ if(l){ l->tail->next = s; Link *temp = l->head; while(temp->next){ temp= temp->next; } l->tail =temp; return OK; } return ERROR; } //在链表尾部删除节点 Status Remove(LinkList *l,Link *q){ Link * temp = PriorPos(l,l->tail); temp->next = NULL; q = l->tail; l->tail = temp; return OK; } //在q所指向的节点前面插入节点 Status InsBefore(LinkList *l,Link *q,Link *s){ Link * ptemp = PriorPos(l,q); ptemp->next = s; s->next = q; return OK; } //在q所指向的节点后面插入节点 Status InsAfter(LinkList *l,Link *q,Link *s){ s->next = q->next; q->next = s; if(q == l->tail){ l->tail = s; } return OK; } //设置q所指向节点的值 Status SetCurElem(Link *q,ElemType e){ q->data = e; return OK; } //获取q所指向节点的值 ElemType GetCurElem(Link *q){ return q->data; } //推断链表长度是否为空 Status ListEmpty(LinkList *l){ if(l->head->next==NULL){ return OK; } return FALSE; } //计算链表的长度 int ListLength(LinkList *l){ int length=0; Link * p= l->head->next; while(p){ length++; p=p->next; } return length; } //获取链表的头指针 Position GetHead(LinkList *l){ return l->head; } //获取链表的尾指针 Position GetLast(LinkList *l){ return l->tail; } //获取p所指的元素的前一个元素 Position PriorPos(LinkList *l,Link * p){ Link * temp = l->head; while(temp->next != p){ temp = temp->next; } return temp; } //获取p元素所指的后一个元素 Position NextPos(LinkList *l,Link * p){ return p->next; } //查找链表中第i个元素并以p返回 Status LocatePos(LinkList *l,int i,Link *p){ p=NULL; if(i>0 && i<ListLength(l)){ int count = 0 ; p = l->head; while(count != i){ p = p->next; count++; } return OK; } return ERROR; } //查找与e相等的节点并返回该节点 Position LocateElem(LinkList *l,ElemType e,Status (* compare)(ElemType,ElemType)){ Link *temp = l->head->next; while((* compare)(temp->data,e) != 0 && temp){ temp = temp->next; } return temp; } //检查是否有错误节点 Status LocateTraverse(LinkList *l,Status (* visit)()){ Link * temp = l->head->next; while(temp){ if((* visit)() == ERROR){ return FALSE; } temp = temp->next; } return OK; } //合并俩个链表并以lc新链表返回。la,lb会被破坏 Status MergeList_L(LinkList * la,LinkList *lb,LinkList * lc,int (* compare)(ElemType,ElemType)){ Link *ha,*hb,*pa,*pb; ha = la->head; hb = lb->head; pa = NextPos(la,ha); pb = NextPos(lb,hb); while(pa && pb){ if((*compare)(pa->data,pb->data) == 1){ //pa.data >pb.data DelFirst(lb,hb,pb); Append(lc,pb); pb = NextPos(lb,hb); }else if((*compare)(pa->data,pb->data) == 0){ DelFirst(la,ha,pa); DelFirst(lb,hb,pb);Append(lc,pa); pa = NextPos(la,ha);pb = NextPos(lb,hb); }else{ DelFirst(la,ha,pa); Append(lc,pa); pa = NextPos(la,ha); } } if(pa) Append(lc,pa); else Append(lc,pb); FreeNode(&ha); FreeNode(&hb); return OK; } //打印链表信息 Status List_Print(LinkList * l){ Link *p = l->head->next; printf("长度=%d\n",ListLength(l)); while(p){ printf("%d ",p->data); p = p->next; } printf("\n"); return OK; } Status ListSort(LinkList *l,int (*compare)(ElemType,ElemType)){ Link * ptemp1 = l->head->next; Link * ptemp2 = ptemp1->next; //冒泡排序 while(ptemp1!=l->tail){ ptemp2 = ptemp1->next; while(ptemp2){ if((*compare)(ptemp1->data,ptemp2->data) == 1){ printf("%d--%d ",ptemp1->data,ptemp2->data); ElemType ntemp = ptemp1->data; ptemp1->data = ptemp2->data; ptemp2->data = ntemp; } ptemp2 = ptemp2->next; } ptemp1 = ptemp1->next; } return OK; } //合并两个链表,不破坏lb的结构,终于合并到la表中 Status MergeList(LinkList *la,LinkList *lb,int (* compare)(ElemType,ElemType)){ Link *pa,*pb; pa = la->head->next; pb = lb->head->next; while(pa && pb){ if((*compare)(pa->data,pb->data) == 1){ Link * temp; MakeNode(&temp,pb->data); //printf("%x ",temp); InsBefore(la,pa,temp); pb=NextPos(lb,pb); // printf("%d--%d ",pa->data,pb->data); } else if((*compare)(pa->data,pb->data) == -1){ pa= NextPos(la,pa); }else{ pb=NextPos(lb,pb); } } if(pb) Append(la,pb); return OK; } int compare(ElemType a,ElemType b){ if(a>b){ return 1; }else if(a==b){ return 0; }else{ return -1; } } int main() { Link *temp; int i,m; LinkList * la,*lb,*lc; InitList(&la); InitList(&lb); InitList(&lc); for(i=0;i<5;i++){ scanf("%d",&m); MakeNode(&temp,m); Append(la,temp); } List_Print(la); ListSort(la,compare); //Remove(la,temp); //printf("你删除的元素是:%d\n",temp->data); List_Print(la); for(i=4;i>0;i--){ MakeNode(&temp,i*3); Append(lb,temp); } List_Print(lb); MergeList(la,lb,compare); List_Print(la); ListSort(la,compare); List_Print(la); List_Print(lb); MergeList_L(la,lb,lc,compare); List_Print(lc); ClearList(la); DestroyList(la); ClearList(lb); DestroyList(lb); ClearList(lc); DestroyList(lc); return 0; } //1 8 6 4 7