데이터 구조 - 추상 데이터 형식
2172 단어 데이터 구조
#include <stdio.h>
#include <tchar.h>
#include <math.h>
#include <malloc.h>
#include <process.h>
//#include<iostream.h> // cout,cin
//
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
typedef int Status;// , OK
typedef int Boolean;//Boolean , TRUE FALSE
ds.c:
#include "stdafx.h"
typedef int ElemType;// ElemType
typedef ElemType* Triplet;
void TripletTest()
{
Triplet T;
ElemType m;
Status i;
i=InitTriplet(T,1,3,2);
printf(" i=%d,T :
",i);
//cout<<T[0]<<' '<<T[1]<<' '<<T[2]<<endl;
printf("%d,%d,%d
",T[0],T[1],T[2]);
i=Get(T,2,m);
if(i==OK)
{
printf(" :%d
",m);
}
i=Put(T,2,6);
if(i==OK)
{
printf(" T 6 :%d,%d,%d
",T[0],T[1],T[2]);
}
i=IsAscending(T);
printf(" :%d(1 0 )
",i);
i=IsDescending(T);
printf(" :%d(1 0 )
",i);
if(i=Max(T,m)==OK)
{
printf("T :%d
",m);
}
if(i=Min(T,m)==OK)
{
printf("T :%d
",m);
}
}
Status InitTriplet(Triplet &T,ElemType v1,ElemType v2,ElemType v3)
{
if(!(T=(ElemType *)malloc(3*sizeof(ElemType))))
{
exit(OVERFLOW);
}
T[0]=v1;
T[1]=v2;
T[2]=v3;
return OK;
}
Status DestoryTriplet(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;
}