Code implementation of "Data structure and algorithm" one or three-tuple

Source: Internet
Author: User

began to review data structures and algorithms, for a long time did not write C, by the way review

The representation and realization of ternary group

#include <iostream>#include<stdlib.h>using namespacestd;//function Result status code#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineINFEASIBLE-1#defineOVERFLOW-2//function Result typetypedefintStatus;//data Element typetypedefintElemtype;//define a ternary group, the initialization work has the Inittriplet function to carry ontypedef ELEMTYPE *Triplet;//initialization functionStatus Inittriplet (Triplet &T, Elemtype v1, Elemtype v2, Elemtype v3);//destroying functionsStatus Destroytriplet (Triplet &T);//gets the first element of the ternary group assigned to EStatus Get (Triplet T,intI, Elemtype &e);//change the value of the first element of the ternary group to EStatus Put (Triplet &t,intI, Elemtype &e);//To determine if the triples are arranged in ascending order 1 means 0 means no .Status isascending (Triplet T);//determine if the triples are in descending order 1 means 0 means notStatus isdescending (Triplet T);//returns the maximum value in the ternary group with EStatus Max (Triplet T, Elemtype &e);//returns the minimum value in the ternary group with EStatus Min (Triplet T, Elemtype &e);//initialization functionStatus Inittriplet (Triplet &T, Elemtype v1, Elemtype v2, Elemtype v3) {    //allocating space to triplesT = (Elemtype *)malloc(3*sizeof(Elemtype)); //returns the overflow result status if the allocation fails    if(!T) exit (OVERFLOW); //Initializes a value to the array at the beginning oft[0] =v1; t[1] =v2; t[2] =v3; //return to OK status    returnOK;}//destroying functionsStatus Destroytriplet (Triplet &T) {     Free(T); T=NULL; returnOK;}//gets the first element of the ternary group assigned to EStatus Get (Triplet T,intI, Elemtype &e) {    if(i<1|| I>3)        returnERROR; E= T[i-1]; returnOK;}//change the value of the first element of the ternary group to EStatus Put (Triplet &t,intI, Elemtype &e) {    if(i<1|| I>3)        returnERROR; T[i-1] =e; returnOK;}//To determine if the triples are arranged in ascending order 1 means 0 means no .Status isascending (Triplet T) {if(t[0] > t[1] && t[1] > t[2]){        return 1; }Else{        return 0; }}//determine if the triples are in descending order 1 means 0 means notStatus isdescending (Triplet T) {return(t[0] < t[1] && t[1] < t[2]);}//returns the maximum value in the ternary group with EStatus 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]); returnOK;}//returns the minimum value in the ternary group with EStatus 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]); returnOK;}voidMain () {//define a ternary groupTriplet Triplet; //initialize triples, elements are three-dimensionalInittriplet (triplet,1,2,3); cout<< triplet[0] <<Endl; intA; //assigns the 2nd element of the ternary group to a, so a should be equal to 2Get (triplet,2, a); cout<< a <<Endl; intb =6; //change the third element of the ternary group to 6Put (triplet,1, B); cout<< triplet[0] <<Endl; //should return 0 because the ternary group is 6,2,3cout << isascending (triplet) <<Endl; //It's supposed to be 0 .cout << isdescending (triplet) <<Endl; //maximum value and minimum value    intC;    Max (triplet, c); intD;    Min (triplet, D); cout<<"Maximum value:"<<c <<"Minimum Value"<< D <<Endl;}

Run results

Code implementation of "Data structure and algorithm" one or three-tuple

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.