데이터 구조 삼원 조 C 언어 실현

#include 
#include 

#define OK        1
#define ERROR     0
#define OVERFLOW  -2
typedef int       Status;
typedef int       ElemType;
typedef ElemType* Triplet;

Status InitTriplet(Triplet *T, ElemType v1, ElemType v2, ElemType v3);
Status DestroyTriplet(Triplet *T);
Status Get(Triplet T, int i, ElemType *e);
Status Put(Triplet *T, int i, ElemType e);
Status IsAscending(Triplet T);
Status IsDescending(Triplet T);
Status Max(Triplet T, ElemType *e);
Status Min(Triplet T, ElemType *e);

int main()
{
	Triplet T;
	ElemType e, e1=11, e2=55, e3=99;

	if (!InitTriplet(&T, e1, e2, e3))
        return 0;

	Max(T, &e);
	printf("     : %d
", e); Min(T, &e); printf(" : %d
", e); printf(" : %d
", IsAscending(T)); printf(" : %d
", IsDescending(T)); DestroyTriplet(&T); return 0; } /**< , v1, v2, v3 */ Status InitTriplet(Triplet *T, ElemType v1, ElemType v2, ElemType v3) { *T = (ElemType *)malloc(3*sizeof(ElemType)); if (!T) exit(OVERFLOW); (*T)[0] = v1; (*T)[1] = v2; (*T)[2] = v3; return OK; } /**< , */ Status DestroyTriplet(Triplet *T) { free(*T); return OK; } /**< e T i */ Status Get(Triplet T, int i, ElemType *e) { if (i<1 || i>3) return ERROR; *e = T[i-1]; return OK; } /**< T i e */ Status Put(Triplet *T, int i, ElemType e) { if (i<1 || i>3) return ERROR; (*T)[i-1] = e; return OK; } /**< T , 1, 0 */ Status IsAscending(Triplet T) { return T[0]<=T[1] && T[1]<=T[2]; } /**< T , 1, 0 */ Status IsDescending(Triplet T) { return T[0]>=T[1] && T[1]>=T[2]; } /**< e T */ Status Max(Triplet T, ElemType *e) { *e = T[0]>=T[1] ? (T[0]>=T[2] ? T[0] : T[2]) : (T[1]>=T[2] ? T[1] : T[2]); return OK; } /**< e T */ Status Min(Triplet T, ElemType *e) { *e = T[0]<=T[1] ? (T[0]<=T[2] ? T[0] : T[2]) : (T[1]<=T[2] ? T[1] : T[2]); return OK; }

좋은 웹페이지 즐겨찾기