数据结构——抽象数据类型

 stdafx.h:

#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的三个值是:\n",i);
	//cout<<T[0]<<' '<<T[1]<<' '<<T[2]<<endl;
	printf("%d,%d,%d\n",T[0],T[1],T[2]);
	i=Get(T,2,m);
	if(i==OK)
	{
		printf("第二个值是:%d\n",m);
	}
	i=Put(T,2,6);
	if(i==OK)
	{
		printf("将T的第二个元素改为6后的数组的值为:%d,%d,%d\n",T[0],T[1],T[2]);
	}
	i=IsAscending(T);
	printf("是否是降序数组:%d(1是0否)\n",i);
	i=IsDescending(T);
	printf("是否是升序函数:%d(1是0否)\n",i);
	if(i=Max(T,m)==OK)
	{
		printf("T中的最大值是:%d\n",m);
	}
	if(i=Min(T,m)==OK)
	{
		printf("T中的最小值是:%d\n",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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值