以下是我用c语言实现数据结构中的链式表
#pragma once; #ifndef _STDLIB_H #include <stdlib.h> #endif #ifndef _ELEMTYPE_H typedef int ElemType; #endif #ifndef _SORTTYPE_H typedef int SortType; #endif #ifndef _FUNCTION #define _FUNCTION typedef int(* FunctionN)(ElemType,ElemType); typedef void(* FunctionP)(ElemType); #endif #define SORT_MIN_MAX 0 #define SORT_MAX_MIN 1 typedef struct DulNode { ElemType data; struct DulNode * prior; struct DulNode * next; }DulNode,*DulLinkList; #pragma once; #include "stdafx.h" bool InitList(DulLinkList &L); bool MakeNode(DulLinkList &L,ElemType e); bool FreeNode(DulLinkList &L); bool DestoryList(DulLinkList &L); bool ClearList(DulLinkList &L); bool Append(DulLinkList &L,DulLinkList s); bool Remove(DulLinkList &L,DulLinkList &q); bool InsBefore(DulLinkList &L,DulLinkList &p,DulLinkList s); bool InsAfter(DulLinkList &L,DulLinkList &p,DulLinkList s); bool ListEmpty(const DulLinkList &L); int ListLength(const DulLinkList &L); DulLinkList GetLast(DulLinkList &L); DulLinkList LocatePos(DulLinkList &L,int i); ElemType GetElem(DulLinkList &L,int i); DulLinkList LocateElem(DulLinkList &L,ElemType e,FunctionN p_function); bool ListTraverse(DulLinkList &L,FunctionP p_function); bool ListInsert(DulLinkList &L,int i,ElemType e); bool ListDelete(DulLinkList &L,int i); bool ListSort(DulLinkList &L,SortType e); ElemType GetMax(DulLinkList &L); ElemType GetMin(DulLinkList &L); bool MergeList(DulLinkList La,DulLinkList Lb,DulLinkList &Lc); #include "LinkList.h" bool InitList(DulLinkList &L) { L=(DulLinkList)malloc(sizeof(DulNode)); L->data=NULL; L->next=NULL; L->prior=NULL; return true; } bool MakeNode(DulLinkList &L,ElemType e) { L=(DulLinkList)malloc(sizeof(DulNode)); if(L==NULL) return false; else { L->data=e; L->prior=NULL; L->next=NULL; return true; } } bool FreeNode(DulLinkList &L) { free(L); return true; } bool DestoryList(DulLinkList &L) { if(L->next==NULL) { free(L); return true; } else { DulLinkList p_move=L,temp; while(p_move->next!=NULL) { temp=p_move; p_move=p_move->next; free(temp); } free(p_move); return true; } } bool ClearList(DulLinkList &L) { if(L->next==NULL) return false; else { DulLinkList p_move=L->next,temp; if(p_move->next==NULL) { free(p_move); L->next=NULL; return true; } else { while(p_move->next!=NULL) { temp=p_move; p_move=p_move->next; free(temp); } free(p_move); L->next=NULL; return true; } } } bool ListEmpty(const DulLinkList &L) { if(L->next==NULL) return true; else return false; } int ListLength(const DulLinkList &L) { if(L->next==NULL) return 0; DulLinkList temp=L->next; int count=1; while(temp->next!=NULL) { count++; temp=temp->next; } return count; } bool Append(DulLinkList &L,DulLinkList s) { DulLinkList p_move=L; while(p_move->next!=NULL) { p_move=p_move->next; } p_move->next=s; s->prior=p_move; s->next=NULL; return true; } bool Remove(DulLinkList &L,DulLinkList &q) { if(L->next==NULL) { q=NULL; return false; } else { DulLinkList temp,p_move=L; while(p_move->next!=NULL) { temp=p_move; p_move=p_move->next; } temp->next=NULL; p_move->prior=NULL; q=p_move; return true; } } DulLinkList GetLast(DulLinkList &L) { if(L->next==NULL) return NULL; else { DulLinkList p_move=L; while(p_move->next!=NULL) { p_move=p_move->next; } return p_move; } } DulLinkList LocatePos(DulLinkList &L,int i) { if(i<0||i>ListLength(L)) { return NULL; } DulLinkList p_move=L->next; int temp; for(temp=0;temp<i;temp++) { p_move=p_move->next; } return p_move; } ElemType GetElem(DulLinkList &L,int i) { if(i<0||i>ListLength(L)) { return NULL; } DulLinkList p_move=L->next; int temp; for(temp=0;temp<i;temp++) { p_move=p_move->next; } return p_move->data; } DulLinkList LocateElem(DulLinkList &L,ElemType e,FunctionN p_function) { if(L->next==NULL) { return NULL; } else { DulLinkList p_move=L->next; while(p_move->next!=NULL) { if((* p_function)(p_move->data,e)!=0) { return p_move; } p_move=p_move->next; } if((* p_function)(p_move->data,e)!=0) { return p_move; } return NULL; } } bool ListTraverse(DulLinkList &L,FunctionP p_function) { if(L->next==NULL) return false; else { DulLinkList p_move=L->next; while(p_move->next!=NULL) { (* p_function)(p_move->data); p_move=p_move->next; } (* p_function)(p_move->data); return true; } } bool InsBefore(DulLinkList &L,DulLinkList &p,DulLinkList s) { if(L->next==NULL) { p=NULL; return false; } else { DulLinkList p_move=L->next; if(p_move==s) { p=NULL; return false; } else { while(p_move->next!=NULL) { if(p_move==s) { p=p_move->prior; return true; } p_move=p_move->next; } if(p_move==s) { p=p_move->prior; return true; } p=NULL; return false; } } } bool InsAfter(DulLinkList &L,DulLinkList &p,DulLinkList s) { if(L->next==NULL) { p=NULL; return false; } else { DulLinkList p_move=L->next; while(p_move->next!=NULL) { if(p_move==s) { p=p_move->next; return true; } p_move=p_move->next; } if(p_move==s) { p=NULL; return false; } p=NULL; return false; } } bool ListInsert(DulLinkList &L,int i,ElemType e) { if(i<0||i>ListLength(L)) return false; else { if(L->next==NULL) { DulLinkList pre=(DulLinkList)malloc(sizeof(DulNode)); if(!pre) return false; pre->data=e; L->next=pre; pre->prior=L; pre->next=NULL; return true; } DulLinkList p_move=L->next; int temp; for(temp=0;temp<i;temp++) { p_move=p_move->next; } DulLinkList pre=(DulLinkList)malloc(sizeof(DulNode)); if(!pre) return false; pre->data=e; p_move->prior->next=pre; pre->prior=p_move->prior; pre->next=p_move; p_move->prior=pre; return true; } } bool ListDelete(DulLinkList &L,int i) { if(i<0||i>ListLength(L)) return false; else { DulLinkList p_move=L->next; int temp; for(temp=0;temp<i;temp++) { p_move=p_move->next; } if(p_move==GetLast(L)) { p_move->prior->next=NULL; free(p_move); return true; } p_move->prior->next=p_move->next; p_move->next->prior=p_move->prior; free(p_move); return true; } } ElemType GetMax(DulLinkList &L) { if(L->next==NULL) return NULL; else { DulLinkList p_move=L->next; ElemType temp=p_move->data; while(p_move->next!=NULL) { if(temp<p_move->data) { temp=p_move->data; } p_move=p_move->next; } if(temp<p_move->data) { temp=p_move->data; } return temp; } } ElemType GetMin(DulLinkList &L) { if(L->next==NULL) return NULL; else { DulLinkList p_move=L->next; ElemType temp=p_move->data; while(p_move->next!=NULL) { if(temp>p_move->data) { temp=p_move->data; } p_move=p_move->next; } if(temp>p_move->data) { temp=p_move->data; } return temp; } } bool ListSort(DulLinkList &L,SortType e) { if(L->next==NULL) return false; else { int Length=ListLength(L),i,j; ElemType temp; DulLinkList p_move=L->next; switch (e) { case SORT_MIN_MAX: for(i=0;i<Length-1;i++) { for(j=0;j<Length-1-i;j++) { if(p_move->data>p_move->next->data) { temp=p_move->data; p_move->data=p_move->next->data; p_move->next->data=temp; } p_move=p_move->next; } p_move=L->next; } return true; case SORT_MAX_MIN: for(i=0;i<Length-1;i++) { for(j=0;j<Length-1-i;j++) { if(p_move->data<p_move->next->data) { temp=p_move->data; p_move->data=p_move->next->data; p_move->next->data=temp; } p_move=p_move->next; } p_move=L->next; } return true; default: return false; } } } bool MergeList(DulLinkList La,DulLinkList Lb,DulLinkList &Lc) { if(La->next==NULL||Lb->next==NULL) return false; else { DulLinkList p_move=La->next,p_temp=Lc; while (p_move->next!=NULL) { DulLinkList pre; MakeNode(pre,p_move->data); p_temp->next=pre; pre->prior=p_temp; p_temp=pre; p_move=p_move->next; } DulLinkList pre; MakeNode(pre,p_move->data); p_temp->next=pre; pre->prior=p_temp; p_temp=pre; p_temp->next=Lb->next; Lb->next->prior=p_temp; return true; } }
本文出自 “胡思旺” 博客,请务必保留此出处http://siwanghu.blog.51cto.com/10630140/1685582
原文地址:http://siwanghu.blog.51cto.com/10630140/1685582