• C语言数据结构-三元数组Triplet


    #include <stdio.h>
    #include<stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLF -1
    #define OVERFLOW -2
    
    typedef int Status;
    
    typedef int ElemType;
    typedef int *Triplet;
    
    Status InitTriplet(Triplet &T, ElemType v1, ElemType v2, ElemType v3){
        //操作结果:构造三元组T,元素e1,e2,e3分别赋值给v1,v2,v3
        T = (ElemType *) malloc (3 * sizeof(ElemType));//分配三个元素的存储空间
        if (!T) exit(OVERFLOW);//分配空间失败
        T[0] = v1;
        T[1] = v2;
        T[2] = v3;
        return OK;
    }//InitTriplet
    
    Status DestoryTriplet(Triplet &T) {
        free(T);
        T = NULL;
        return OK;
    }
    
    Status Get(Triplet &T, int i, ElemType &e){
        if(i <1 || i > 3) {
            return ERROR;
        }
    
        e = T[i - 1];
        return OK;
    }
    
    Status Put(Triplet &T, int i, ElemType e) {
         if(i <1 || i > 3) {
            return ERROR;
        }
    
        T[i - 1] = e;
        return OK;
    }
    
    Status IsAscending(Triplet &T) {
        return (T[0] <= T[1]) && (T[1] <= T[2]);
    }
    
    Status IsDescending(Triplet &T) {
        return (T[0] >= T[1]) && (T[1] >= T[2]);
    }
    
    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;
    }
    
    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;
    }
    
    int main() {
    
    	Triplet T;
    	ElemType e, e1, e2, e3, r;
    	e1 = 3;
    	e2 = 5;
    	e3 = 7;
    
    	InitTriplet(T, e1, e2, e3);
    
        printf("====== e1 ========
    ");
        Get(T, 1, e);
        printf("%-4d
    ", e);
        printf("
    ");
    
        printf("====== e2 ========
    ");
        Get(T, 2, e);
        printf("%-4d
    ", e);
        printf("
    ");
    
        printf("====== e3 ========
    ");
        Get(T, 3, e);
        printf("%-4d
    ", e);
        printf("
    ");
    
        e = 6;
        printf("====== put 6 e3 ========
    ");
        Put(T, 3, e);
        printf("%-4d
    ", e);
        printf("
    ");
    
        printf("====== r = 1 ========
    ");
        r = IsAscending(T);
        printf("%-4d
    ", r);
        printf("
    ");
    
        printf("====== r = 0 ========
    ");
        r = IsDescending(T);
        printf("%-4d
    ", r);
        printf("
    ");
    
        printf("======  Max ========
    ");
        Max(T, e);
        printf("%-4d
    ", e);
        printf("
    ");
    
        printf("======  Min ========
    ");
        Min(T, e);
        printf("%-4d
    ", e);
        printf("
    ");
    
        DestoryTriplet(T);
    
        printf("====== T is null ? ========
    ");
        if(T == NULL) {
            printf("t is null
    ");
        } else {
            printf("t is not null
    ");
        }
    
    	return 0;
    }
    

      

  • 相关阅读:
    洛谷 P3808 【模板】AC自动机(简单版) 题解
    O3优化模板
    洛谷 P3909 异或之积 题解
    洛谷 P3870 [TJOI2009]开关 题解
    洛谷 P1891 疯狂LCM 题解
    洛谷 P5221 Product 题解
    洛谷 P2568 GCD 题解
    洛谷 P5639 【CSGRound2】守序者的尊严 题解
    扩展kmp板子
    [JZOJ3167] 【GDOI2013模拟3】查税
  • 原文地址:https://www.cnblogs.com/maduar/p/13195493.html
Copyright © 2020-2023  润新知