抽象資料型別的表示與實現

2022-09-08 09:57:08 字數 2392 閱讀 5750

各種字元的定義**如下:

////////////////////////////////

// liyuechao

// 2014.8.7

// c1.h

////////////////////////////////

檔案名字#include#include#include// malloc()等

#include// int_max等

#include// eof(=^z或f6),null

#include// atoi()

#include// eof()

#include// floor(),ceil(),abs()

#include// exit()

#include// cout,cin

#define true 1 // 函式結果狀態**

#define false 0

#define ok 1

#define error 0

#define infeasible -1

//#define overflow -2 因為在math.h中已定義overflow的值為3,故去掉此行

typedef int status; // status是函式的型別,其值是函式結果狀態**,如ok等

typedef int boolean; // boolean是布林型別,其值是true或false

// c1-1.h 採用動態分配的順序儲存結構

typedef elemtype *triplet; // 由inittriplet分配3個元素儲存空間

// triplet型別是elemtype型別的指標,存放elemtype型別的位址

// bo1-1.cpp 抽象資料型別triplet和elemtype(由c1-1.h定義)的基本操作(8個)

status inittriplet(triplet &t,elemtype v1,elemtype v2,elemtype v3)

status destroytriplet(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)

// main1-1.cpp 檢驗基本操作bo1-1.cpp的主函式

#include "c1.h" // 要將程式中所有#include命令所包含的檔案拷貝到當前目錄下

// 以下2行可根據需要選用乙個(且只能選用乙個),而不需改變基本操作bo1-1.cpp

typedef int elemtype; // 定義抽象資料型別elemtype在本程式中為整型

//typedef double elemtype; // 定義抽象資料型別elemtype在本程式中為雙精度型

#include "c1-1.h" // 在此命令之前要定義elemtype的型別

#include "bo1-1.cpp" // 在此命令之前要包括c1-1.**件(因為其中定義了triplet)

void main()

{triplet t;

elemtype m;

status i;

i=inittriplet(t,5,7,9); // 初始化三元組t,其3個元素依次為5,7,9

//i=inittriplet(t,5.0,7.1,9.3); // 當elemtype為雙精度型時,可取代上句

printf("呼叫初始化函式後,i=%d(1:成功) t的3個值為",i);

cout<

呼叫初始化函式後,i=1(1:成功) t的3個值為5 7 9

t的第2個值為7

將t的第2個值改為6後,t的3個值為5 6 9

呼叫測試公升序的函式後,i=1(0:否1:是)

呼叫測試降序的函式後,i=0(0:否1:是)

t中的最大值為9

t中的最小值為5

銷毀t後,t=0x00000000(null)

抽象資料型別的表示與實現

各種字元的定義 如下 liyuechao 2014.8.7 c1.h 檔案名字 include include include malloc 等 include int max等 include eof z或f6 null include atoi include eof include floor...

抽象資料型別的表示與實現

typedef elemtype triplet 由inittriplet分配3個元素儲存空間 triplet型別是elemtype型別的指標,存放elemtype型別的位址 status inittriplet triplet t,elemtype v1,elemtype v2,elemtype ...

抽象資料型別的表示與實現

預定義常量和型別 函式結果狀態 define true 1 define false 0 define ok 1 define error 0 define ineasible 1 define overflow 2 status是函式的型別,其值是函式結果狀態 typedef int status...