Triple -- Data Structure

Source: Internet
Author: User

C language (this book does not directly use classes, objects, and other facilities, but selects a core subset from the C language, and adds the reference call parameter transmission method of C ++ language, etc, the source code is as follows:

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Define TRUE 1
# Define FALSE 0
# Define OK 1
# Define ERROR 0
# Define INFREASIBLE-1
# Define OVERFLOW-2
Typedef int Status;
Typedef int ElemType;
 
Typedef ElemType * Triplet;
 
Status InitTriplet (Triplet & T, ElemType v1, ElemType v2, ElemType v3 );
Status DestroyTiplet (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 );
Status Show (Triplet T );
 
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 DestroyTiplet (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;
}
Status Show (Triplet T)
{
Int I;
For (I = 0; I <3; I ++ ){
Printf ("% d", T [I]);
}
Putchar ('\ n ');
Return OK;
}
 
Int main (void)
{
Triplet T;
InitTriplet (T, 9, 3, 0 );
Show (T );
ElemType e;
Get (T, 2, e );
Printf ("% d \ n", e );
Put (T, 1, 5 );
Show (T );
Printf ("% d \ n", IsAscending (T), IsDescending (T ));
ElemType e1, e2;
Max (T, e1 );
Min (T, e2 );
Printf ("% d \ n", e1, e2 );
}

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.