严蔚敏版数据结构例1-7实例

来源:互联网 发布:淘宝商品品控下架 编辑:程序博客网 时间:2024/06/11 00:12
#include<cstdio>
#include<cstdlib>
using namespace std; 


#define OK   1       //程序正常结束 
#define ERROR 0      //程序失败
typedef int Status;  //返回整数
typedef int* Triplet; //定义Triplet为整形指针


Status InitTriplet(Triplet &T,int v1,int v2,int v3)
{
       T=(Triplet)malloc(3*sizeof(int));//分配三个存储空间
       if(!T)exit(ERROR);  //分配存储空间失败
       T[0]=v1;
       T[1]=v2;
       T[2]=v3;
       return OK; 

Status DestroyTriplet(Triplet &T)
{
       free(T);
       T=NULL;
       return OK;
}
Status Get(Triplet T,int i,int &e)
{
       if(i<1 || i>3)return ERROR;
       e=T[i-1];
       return OK; 
}
Status Put(Triplet T,int i,int 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,int &e)
{
       e=(T[0]>=T[1])?T[0]:T[1];
       e=(e>=T[2])?e:T[2];
       return OK; 
}
Status Min(Triplet T,int &e)
{
       e=(T[0]<=T[1])?T[0]:T[1];
       e=(e<=T[2])?e:T[2];
       return OK; 
}


int main()
{
    Triplet L;
    InitTriplet(L,3,4,5);
    int e;
    Get(L,2,e);
    printf("%d\n",e);
    Put(L,1,2);
    Get(L,1,e);
    printf("%d\n",e);
    if(IsAscending(L))printf("%s\n","OK");
    if(IsDescending(L))printf("%s\n","Ok");
    Max(L,e);
    printf("%s%d\n","Max is ",e);
    Min(L,e);
    printf("%s%d\n","Min is ",e);
    system("pause");
    return 0;
}

i=(x>y)?x:y


当x>y时,i=x;否则x=y.