以下是我用c语言实现的顺序表
/ #pragma once; #ifndef _STDLIB_H #include#endif #ifndef _ELEMTYPE_H typedef int ElemType; #endif #ifndef _SORTTYPE_H typedef int SortType; #endif #ifndef _FUNCTION #define _FUNCTION typedef void(* FunctionP)(int); typedef int(* FunctionN)(ElemType,ElemType); #endif #define LIST_INT_SIZE 100 #define LIST_IN_CREATE 20 #define SORT_MIN_MAX 0 #define SORT_MAX_MIN 1 typedef struct { ElemType * elm; int Length; int ListSize; }SqList; #include "stdafx.h" #pragma once; bool InitList(SqList &L); void DestoryList(SqList &L); bool ClearList(SqList &L); bool ListEmpty(const SqList &L); int ListLength(const SqList &L); bool GetElem(const SqList &L,int i,ElemType &e); bool PriorElem(const SqList &L,ElemType cur,ElemType &pre); bool NextElem(const SqList &L,ElemType cur,ElemType &next); bool ListInsert(SqList &L,int i,ElemType e); bool ListDelete(SqList &L,int i,ElemType &e); bool ListPush(SqList &L,ElemType e); bool ListSort(SqList &L,SortType Type); bool GetMax(const SqList &L,ElemType &e); bool GetMin(const SqList &L,ElemType &e); bool MergeList(SqList La,SqList Lb,SqList &Lc); bool ListEqual(const SqList &La,const SqList &Lb); int LocateElem(const SqList &L,ElemType e,FunctionN p_function); bool ListTraverse(const SqList &L,FunctionP p_function); bool UnionList(SqList &La, const SqList &Lb); #include "List.h" bool InitList(SqList &L) { L.elm=(ElemType *)malloc(LIST_INT_SIZE*sizeof(ElemType)); if(!L.elm) return false; L.Length=0; L.ListSize=LIST_INT_SIZE; return true; } void DestoryList(SqList &L) { free(L.elm); L.Length=0; L.ListSize=0; } bool ClearList(SqList &L) { ElemType * temp=(ElemType *)malloc(L.ListSize*sizeof(ElemType)); if(temp==NULL) return false; else { L.Length=0; free(L.elm); L.elm=temp; return true; } } bool ListEmpty(const SqList &L) { if(L.Length==0) return true; else return false; } int ListLength(const SqList &L) { return L.Length; } bool GetElem(const SqList &L,int i,ElemType &e) { if(i<0||i>L.Length-1) { e=NULL; return false; } e=L.elm[i];; return true; } bool PriorElem(const SqList &L,ElemType cur,ElemType &pre) { ElemType * p=L.elm,* q=L.elm+L.Length-1; while(p<=q) { if(*p==cur) { if(p==L.elm) { pre=NULL; return false; } pre=*(p-1); return true; } p++; } pre=NULL; return false; } bool NextElem(const SqList &L,ElemType cur,ElemType &next) { ElemType *p=L.elm,*q=L.elm+L.Length-1; while(p<=q) { if(*p==cur) { if(p==L.elm+L.Length-1) { next=NULL; return false; } next=*(p+1);; return true; } p++; } next=NULL; return false; } bool ListInsert(SqList &L,int i,ElemType e) { if(i<0||i>L.Length) return false; if(L.Length>=L.ListSize) { ElemType * newbase; newbase=(ElemType *)realloc(L.elm,(L.ListSize+LIST_IN_CREATE)*sizeof(ElemType)); if(!newbase) { return false; } else { L.elm=newbase; L.ListSize+=LIST_IN_CREATE; } } int j; for (j = L.Length; j > i; j--) { L.elm[j] = L.elm[j - 1]; } L.elm[i] = e; L.Length++; return true; } bool ListDelete(SqList &L,int i,ElemType &e) { if(i<0||i>L.Length) return false; int j; e=L.elm[i]; for(j=L.Length-1;j>i;i++) { L.elm[i]=L.elm[i+1]; } L.Length--; return true; } bool ListPush(SqList &L,ElemType e) { if(L.Length>=L.ListSize) { ElemType * newbase; newbase=(ElemType *)realloc(L.elm,(L.ListSize+LIST_IN_CREATE)*sizeof(ElemType)); if(!newbase) { free(newbase); return false; } else { L.elm=newbase; L.ListSize+=LIST_IN_CREATE; } } L.elm[L.Length]=e; L.Length++; return true; } bool ListSort(SqList &L,SortType Type) { if(L.Length==0) return false; else { ElemType temp; int i,j; switch (Type) { case SORT_MIN_MAX: //óò for(i=0;i L.elm[j+1]) { temp=L.elm[j]; L.elm[j]=L.elm[j+1]; L.elm[j+1]=temp; } } } return true; case SORT_MAX_MIN: //óò for(i=0;i L.elm[i]) temp=L.elm[i]; } e=temp; return true; } } bool MergeList(SqList La,SqList Lb,SqList &Lc) { if(La.Length==0&&Lb.Length==0) return false; else { Lc.elm=(ElemType *)malloc((La.Length+Lb.Length)*sizeof(ElemType)); if(!Lc.elm) return false; int i,j; Lc.ListSize=La.Length+Lb.Length; Lc.Length=La.Length+Lb.Length; for(i=0;i La.elm[j]) { j++; } } return true; } }