建立线性表
来源:互联网 发布:ubuntu界面怎么启动u盘 编辑:程序博客网 时间:2024/05/20 20:02
#include <stdlib.h>#include <stdio.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define LIST_INIT_SIZE 100typedef int ElemType;typedef struct{ ElemType *elem; int length; int listsize;}SqList;Status InitList(SqList &L){ L.elem=(ElemType *)malloc(LIST_INIT_SIZE* sizeof(ElemType)); if(!L.elem) exit(OVERFLOW); L.length=0; L.listsize=LIST_INIT_SIZE; return OK;}int ListLength(SqList L){ return L.length;}Status getElem(SqList L,int i,ElemType &e){ if(i<=0 || i>L.length) return ERROR; e=L.elem[i-1]; return OK;}Status fun(ElemType a,ElemType b){ if(a==b) return TRUE; else return FALSE;}int LocateElem(SqList L,ElemType e, Status (*compare)(ElemType,ElemType)){ int i=1; ElemType *p; p=L.elem; while(i<=L.length && !(*compare)(*p++,e)) ++i; if(i<=L.length) return i; else return 0;}int main(){ SqList List; int i; ElemType e; InitList(List); for(i=0;i<5;i++) List.elem[i]=i+10; List.length=5; printf("%d\n",ListLength(List)); getElem(List,3,e); printf("%d\n",e); e=11; i=LocateElem(List,e,fun); printf("%d\n",i);}