The C language implementation of the abstract data type Triplet, and the abstract data type triplet

Source: Internet
Author: User

The C language implementation of the abstract data type Triplet, and the abstract data type triplet

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) {// initialization 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) {// triples t release int * t = t 25 26 free (* t); 27 * t = NULL; 28 return OK; 29} 30 31 Status Get (Triplet t, int I, Elemtype * e) {// obtain an element 32 if (I <1 | I> 3 | t = NULL) 33 return ERROR in Triple t; 34 * e = t [I-1]; 35 // printf ("the % d element in the triple is % d \ n", (I + 1 ), t [I]); 36 return OK; 37} 38 39 St Atus 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 ("the % d element is % d \ n", 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 is blank \ n"); 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; // program status 87 Elemtype p; // used for Memory sharing between the main function and sub-function 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 ("the % d element in the triple is % d \ n", 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 ("the elements in the three tuples are in ascending order \ n"); 106 else107 printf ("the elements in the three tuples are not in ascending order \ n "); 108 109 if (isDescending (t) 110 printf ("the elements in the three tuples are sorted in descending order \ n "); 111 else112 printf ("elements in the three tuples are not sorted in descending order \ n"); 113 114 if (I) {115 I = Max (t, & max ); 116} 117 printf ("% d \ n", max); 118 119 if (I) {120 DestroyTriplet (& t ); 121} 122 123 124 if (I) {125 I = Min (t, & min); 126} 127 128 printf ("the smallest element in the triple is % d \ n ", min); // the memory space pointed to by t has been released, and the uninitialized spam number 129 is stored in min}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.