顺序表实现

来源:互联网 发布:决裂电影知乎 编辑:程序博客网 时间:2024/05/20 16:43
#ifndef HEAD_H__#define HEAD_H__#include<stdlib.h>#define ERROR 0#define OK 1#define FALSE 0#define TRUE 1typedef int status;typedef int boolean;typedef int ElementType;#define MAX 100void view(ElementType *);status compare(ElementType ,ElementType);void buildSet(ElementType a[],int length,int beg,int end);void printSet(ElementType a[],int length);#endif


#ifndef LIST_H__#define LIST_H__#include"head.h"#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef struct{ElementType *elem;int length;int listSize;}sqList;status initSqList(sqList *L);status destroySqList(sqList *L);status clearSqList(sqList *L);status empty(sqList L);int length(sqList L);status getElement(sqList L,int i,ElementType *e);int locateElement(sqList L,ElementType e,status (*compare)(ElementType ,ElementType));status priorElement(sqList L,ElementType cur_e,ElementType *pre_e);status nextElement(sqList L,ElementType cur_e,ElementType *next_e);status myInsert(sqList *L,int i,ElementType e);status myDelete(sqList *L,int i,ElementType *e);status myTraverse(sqList L,void(*view)(ElementType*));#endif

#include"head.h"#include<stdio.h>#include<stdlib.h>#include<time.h>void view(ElementType *e){printf("%d\t",*e);}status compare(ElementType e1,ElementType e2){if(e1==e2)return TRUE;elsereturn FALSE;}void buildSet(ElementType a[],int length,int beg,int end){int i;srand((unsigned)time(NULL));for(i=0;i<length;i++)a[i]=rand()%(end-beg+1)+beg;}void printSet(ElementType a[],int length){int i,count=1;for(i=0;i<length;i++,count++){printf("%d\t",a[i]);if(count%10==0)printf("\n");}}

//#include"head.h"#include"sqList.h"status initSqList(sqList *L){L->elem=(ElementType *)malloc(LIST_INIT_SIZE*sizeof(ElementType));if(!L->elem)exit(1);L->length=0;L->listSize=LIST_INIT_SIZE;return OK;}status destroySqList(sqList *L){free(L->elem);L->elem=NULL;L->length=0;L->listSize=0;return OK;}status clearSqList(sqList *L){L->length=0;return OK;}status empty(sqList L){if(L.length==0)return TRUE;elsereturn FALSE;}int length(sqList L){return L.length;}status getElement(sqList L,int i,ElementType *e){if(i<0 || i>=L.length)exit(1);*e=*(L.elem+i);return OK;}int locateElement(sqList L,ElementType e,status (*compare)(ElementType ,ElementType)){ElementType *p=L.elem;int i;for(i=0;i<L.length && !compare(*p,e);i++,p++);if(i<L.length)return i;elsereturn -1;}status priorElement(sqList L,ElementType cur_e,ElementType *pre_e){ElementType *p=L.elem+1;int i;for(i=1;i<L.length && *p!=cur_e;i++,p++);if(i<L.length){*pre_e=*--p;return OK;}elsereturn FALSE;}status nextElement(sqList L,ElementType cur_e,ElementType *next_e){ElementType *p=L.elem;int i;for(i=0;i<L.length-1 && *p!=cur_e;p++,i++);if(i<L.length-1){*next_e=*++p;return OK;}elsereturn FALSE;}status myInsert(sqList *L,int i,ElementType e){ElementType *base,*q,*p;if(i<0 || i>L->length)return ERROR;if(L->length>=L->listSize){base=(ElementType *)realloc(L->elem,(L->listSize+LISTINCREMENT)*sizeof(ElementType));if(!base)exit(1);L->elem=base;L->listSize+=LISTINCREMENT;}q=L->elem+i;for(p=L->elem+L->length-1;p>=q;--p)*(p+1)=*p;*q=e;++L->length;return OK;}status myDelete(sqList *L,int i,ElementType *e){ElementType *p,*q;if(i<0 || i>=L->length)return ERROR;p=L->elem+i;*e=*p;q=L->elem+L->length-1;for(++p;p<=q;p++)*(p-1)=*p;L->length--;return OK;}status myTraverse(sqList L,void(*view)(ElementType*)){ElementType *p;int i;p=L.elem;for(i=0;i<L.length;i++)view(p++);return OK;}

#include<stdio.h>#include"head.h"#include"sqList.h"/*status initSqList(sqList *L);status destroySqList(sqList *L);status clearSqList(sqList *L);status empty(sqList L);int length(sqList L);status getElement(sqList L,int i,ElementType *e);int locateElement(sqList L,ElementType e,status (*compare)(ElementType ,ElementType));status priorElement(sqList L,ElementType cur_e,ElementType *pre_e);status nextElement(sqList L,ElementType cur_e,ElementType *next_e);status myInsert(sqList *L,int i,ElementType e);status myDelete(sqList *L,int i,ElementType *e);status myTraverse(sqList L,void(*view)(ElementType*));*/int main(){int i;ElementType a[MAX],next_e,pre_e,e,index;sqList L;if(initSqList(&L))printf("build a sqList : L\n");printf("the sqList length is %d\n",L.length);printf("the sqList listSize is %d\n",L.listSize);printf("the address of sqList is %xd\n",L.elem);if(empty(L))printf("the sqList is empty\n");buildSet(a,MAX,0,100);//printSet(a,MAX);for(i=0;i<MAX;i++)myInsert(&L,L.length,a[i]);myTraverse(L,view);if(nextElement(L,a[1],&next_e))printf("the next element of %d is : %d\n",a[1],next_e);if(priorElement(L,a[1],&pre_e))printf("the previous element of %d is : %d\n",a[1],pre_e);for(i=0;i<10;i++){myDelete(&L,0,&e);printf("%d\t",e);}printf("the new sqList is : \n");myTraverse(L,view);printf("the getElement is :\n");for(i=0;i<20;i++){getElement(L,20-i-1,&e);printf("%d\t",e);}printf("locate element :\n");if((index=locateElement(L,55,compare))!=-1)printf("the element 55's index is :%d and the value is %d\n",index,*(L.elem+index));printf("clear the sqList\n");clearSqList(&L);printf("the length of the sqList is: %d\n",L.length);printf("destroy the sqList\n");destroySqList(&L);printf("the sqList's address is : %x",L.elem);return 0;}


原创粉丝点击