zoukankan      html  css  js  c++  java
  • 抽象数据类型Triplet的C语言实现

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #define ERROR 0
      5 #define OK 1
      6 
      7 typedef int Status;
      8 typedef int Elemtype;
      9 typedef Elemtype * Triplet;
     10 
     11 Status InitTriplet(Triplet *t, Elemtype v0, Elemtype v1, Elemtype v2){ //三元组t的初始化
     12 
     13     *t = (Elemtype *)malloc(3 * sizeof(Elemtype));
     14     if (!*t)
     15         exit(-1);
     16 
     17     (*t)[0] = v0;
     18     (*t)[1] = v1;
     19     (*t)[2] = v2;
     20 
     21     return OK;
     22 }
     23 
     24 Status DestroyTriplet(Triplet *t){//三元组t的释放 int* t=t
     25 
     26     free(*t);
     27     *t = NULL;
     28     return OK;
     29 }
     30 
     31 Status Get(Triplet t, int i, Elemtype *e){//得到三元组t中的某个元素
     32     if (i<1 || i>3 || t == NULL)
     33         return ERROR;
     34     *e = t[i - 1];
     35     //printf("三元组中第%d个元素为%d
    ",(i+1),t[i]);
     36     return OK;
     37 }
     38 
     39 Status Put(Triplet t, int i, Elemtype e){
     40     if (t == NULL)
     41         return ERROR;
     42     if (i<1 || i>3 || t == NULL)
     43         return ERROR;
     44     t[i] = e;
     45     return OK;
     46 }
     47 
     48 Status Show(Triplet t){
     49     if (t == NULL)
     50         return ERROR;
     51     for (int i = 0; i < 3; i++)
     52         printf("第%d个元素为%d
    ", i + 1, t[i]);
     53     return OK;
     54 }
     55 
     56 Status isAscending(Triplet t){
     57     if (t == NULL)
     58         return ERROR;
     59     return(t[0] <= t[1] && t[1] <= t[2]);
     60 }
     61 
     62 Status isDescending(Triplet t){
     63     if (t == NULL)
     64         return ERROR;
     65     return(t[0] >= t[1] && t[1] >= t[2]);
     66 }
     67 
     68 Status Max(Triplet t, Elemtype *e){
     69     if (t == NULL)
     70         return ERROR;
     71     *e = ((t[0] > t[1] ? t[0] : t[1])>t[2]) ? (t[0] > t[1] ? t[0] : t[1]) : t[2];
     72     return OK;
     73 }
     74 
     75 Status Min(Triplet t, Elemtype *e){
     76     if (t == NULL)
     77     {
     78         printf("t为空
    ");
     79         return ERROR;
     80     }
     81     *e = ((t[0] < t[1] ? t[0] : t[1])<t[2]) ? (t[0] < t[1] ? t[0] : t[1]) : t[2];
     82     return OK;
     83 }
     84 
     85 void main(){
     86     Status i;//程序状态
     87     Elemtype p;//用于主函数和子函数的内存共享
     88     Elemtype max;
     89     Elemtype min;
     90     Triplet t;
     91     int di = 1;
     92     i = InitTriplet(&t, 0, 1, 2);
     93     if (i){
     94         i = Get(t, di, &p);
     95     }
     96     printf("三元组中第%d个元素为%d
    ", di, p);
     97     if (i){
     98         i = Put(t, di, 5);
     99     }
    100     if (i){
    101         i = Show(t);
    102     }
    103 
    104     if (isAscending(t))
    105         printf("三元组中的元素是按升序排列
    ");
    106     else
    107         printf("三元组中的元素不是按升序排列
    ");
    108 
    109     if (isDescending(t))
    110         printf("三元组中的元素是按降序排列
    ");
    111     else
    112         printf("三元组中的元素不是按降序排列
    ");
    113 
    114     if (i){
    115         i = Max(t, &max);
    116     }
    117     printf("三元组中元素最大的元素为%d
    ", max);
    118 
    119     if (i){
    120         DestroyTriplet(&t);
    121     }
    122     
    123 
    124     if (i){
    125         i = Min(t, &min);
    126     }
    127 
    128     printf("三元组中元素最小的元素为%d
    ", min);//t所指向的内存空间已被释放,min中存放着未初始化的垃圾数字
    129 }
  • 相关阅读:
    【leetcode】1020. Partition Array Into Three Parts With Equal Sum
    【leetcode】572. Subtree of Another Tree
    【leetcode】123. Best Time to Buy and Sell Stock III
    【leetcode】309. Best Time to Buy and Sell Stock with Cooldown
    【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
    【leetcode】467. Unique Substrings in Wraparound String
    【leetcode】823. Binary Trees With Factors
    【leetcode】143. Reorder List
    【leetcode】1014. Capacity To Ship Packages Within D Days
    【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60
  • 原文地址:https://www.cnblogs.com/gangtiexia/p/4756057.html
Copyright © 2011-2022 走看看