以下是我用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 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.Length-1;i++) { for(j=0;j<L.Length-1-i;j++) { if(L.elm[j]>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.Length-1;i++) { for(j=0;j<L.Length-1-i;j++) { if(L.elm[j]<L.elm[j+1]) { temp=L.elm[j]; L.elm[j]=L.elm[j+1]; L.elm[j+1]=temp; } } } return true; default: return false; } } } bool GetMax(const SqList &L,ElemType &e) { if(L.Length==0) { e=NULL; return false; } else { ElemType temp=L.elm[0]; int i; for(i=0;i<L.Length;i++) { if(temp<L.elm[i]) temp=L.elm[i]; } e=temp; return true; } } bool GetMin(const SqList &L,ElemType &e) { if(L.Length==0) { e=NULL; return false; } else { ElemType temp=L.elm[0]; int i; for(i=0;i<L.Length;i++) { if(temp>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.Length;i++) { Lc.elm[i]=La.elm[i]; } for(j=0;j<Lb.Length;j++) { Lc.elm[i]=Lb.elm[j]; i++; } return true; } } bool ListEqual(const SqList &La,const SqList &Lb) { if(La.Length!=Lb.Length) return false; else { int i=0; while(i<La.Length&&i<Lb.Length) { if(La.elm[i]!=Lb.elm[i]) return false; i++; } return true; } } int LocateElem(const SqList &L,ElemType e,FunctionN p_function) { if(L.Length==0) return 0; //±í·0 int i,temp=0; for(i=0;i<L.Length;i++) { temp=(* p_function)(L.elm[i],e); if(temp!=0) return i; //·ú×ó±í } return -1; //ó·-1 } bool ListTraverse(const SqList &L,FunctionP p_function) { if(L.Length==0) return false; int i; for(i=0;i<L.Length;i++) { (* p_function)(i); } return true; } bool UnionList(SqList &La, const SqList &Lb) { if (La.Length == 0) return false; else { int i=0, j=0; ElemType e; while (i<Lb.Length&&j<La.Length) { if (Lb.elm[i] < La.elm[j]) { ListInsert(La, j, Lb.elm[i]); i++; j++; } if (Lb.elm[i] == La.elm[j]) { ListDelete(La, j, e); i++; j++; } if (Lb.elm[i]>La.elm[j]) { j++; } } return true; } }
本文出自 “胡思旺” 博客,请务必保留此出处http://siwanghu.blog.51cto.com/10630140/1685579
原文地址:http://siwanghu.blog.51cto.com/10630140/1685579