Representation and implementation of abstract data type Triplet (Chapter 1: P12)

Posted by Jenling on Sun, 15 Mar 2020 05:10:28 +0100

Preface

Note: when using reference for function parameters in data structure textbook, it uses &, while I use * for reference parameters implemented in C. I will analyze the reasons below.

Reference, pointer and address are closely related concepts. The address is the address in the computer memory (generally the storage location of the values of some variables in the memory), and the pointer is the variable of the storage address, so the pointer can "point" to the memory address. Conceptually, a reference variable is essentially another name for a pointer (but it cannot be instantiated by the compiler)

In C + +, the use of & can refer to variable (in short, reference) as variable type, which does not exist in C. But in C language, pointer can also achieve similar functions.

Example:

(1) In C + + (references are used in function parameters)

void main(void)  
{  
int i=3; 
f(i);  
cout << i;  
} 
void f(int& r)  
{  
r = 2*r;  
}

This program outputs "6" (2*r doubles the variables referenced by R, that is, i)

 

(2) In C language

To achieve the same function, we can declare f() as void f(int *r), where R is a pointer to integer type, then call the parameter &i (I address) to call function f(), use function R () to use the reference of R.

void main(void)  
{  
int i=3; 
f(&i);  
cout << i;  
} 
void f(int *r)  
{  
r = 2*r;  
}

 

Representation and implementation of abstract data type Triplet (C language)

typedef int ElemType; /* Define abstract data type ElemType as integer in this program */
/*typedef double ElemType; /* Define abstract data type ElemType as double precision in this program */

typedef ElemType *Triplet; /* Three element storage space allocated by InitTriplet */
/* Triplet Type is a pointer of type ElemType, which holds the address of type ElemType */

#Include < malloc. H > / * malloc(), etc*/
#Include < stdio. H > / * EOF (= ^ Z or F6),NULL*/
#include<process.h> /* exit() */

/* Function result status code */
#define OK 1
#define ERROR 0
#define OVERFLOW -2 

typedef int Status; /* Status Is the type of function whose value is the function result status code, such as OK */
typedef int Boolean; /* Boolean Is a boolean type with a value of TRUE or FALSE */


/* Basic operations of abstract data types triple and ElemType (8) */

/* -------------        Function prototype description of basic operation---------------*/
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);



/* -------------           Implementation of basic operation---------------*/

Status InitTriplet(Triplet *T, ElemType v1, ElemType v2, ElemType v3)
{ /* Operation result: construct the triple T, and set the initial values of the three elements of T in turn as v1,v2 and v3 */
	*T = (ElemType *)malloc(3 * sizeof(ElemType));
	if (!*T)
		exit(OVERFLOW);
	(*T)[0] = v1, (*T)[1] = v2, (*T)[2] = v3;
	return OK;
}

Status DestroyTriplet(Triplet *T)
{ /* Operation result: triple T is destroyed */
	free(*T);
	*T = NULL;
	return OK;
}

Status Get(Triplet T, int i, ElemType *e)
{ /* Initial condition: triple t already exists, 1 ≤ i ≤ 3. Operation result: return the value of the ith element of T with e */
	if (i < 1 || i>3)
		return ERROR;
	*e = T[i - 1];
	return OK;
}

Status Put(Triplet T, int i, ElemType e)
{ /* Initial condition: triple T already exists, 1 ≤ I ≤ 3. Operation result: change the value of the i-th element of T to e */
	if (i < 1 || i>3)
		return ERROR;
	T[i - 1] = e;
	return OK;
}

Status IsAscending(Triplet T)
{ /* Initial condition: triple t already exists. Operation result: if the three elements of T are arranged in ascending order, return 1, otherwise return 0 */
	return(T[0] <= T[1] && T[1] <= T[2]);
}

Status IsDescending(Triplet T)
{ /* Initial condition: triple t already exists. Operation result: if the three elements of T are arranged in descending order, return 1, otherwise return 0 */
	return(T[0] >= T[1] && T[1] >= T[2]);
}

Status Max(Triplet T, ElemType *e)
{ /* Initial condition: triple t already exists. Operation result: return the maximum value of the three elements of T with 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)
{ /* Initial condition: triple t already exists. Operation result: return the minimum value of the three elements of T with e */
	*e = (T[0] <= T[1]) ? (T[0] <= T[2] ? T[0] : T[2]) : (T[1] <= T[2] ? T[1] : T[2]);
	return OK;
}

/* main Check the main function of basic operation */
void main()
{
	Triplet T;
	ElemType m;
	Status i;
	i = InitTriplet(&T, 5, 7, 9);
	//I = inittriplet (& T, 4.0,8.1,9.3); / * when ElemType is double precision, it can replace the previous sentence*/
	printf("After calling the initialization function, i=%d(1:Success) T The three values of are:%d %d %d\n", i, T[0], T[1], T[2]); /* When the type of ElemType changes, change the formatter of printf() accordingly. */
	i = Get(T, 2, &m);
	if (i == OK)
		printf("T The second value of is:%d\n", m);
	i = Put(T, 2, 6);
	if (i == OK)
		printf("take T When the second value of is changed to 6, T The three values of are:%d %d %d\n", T[0], T[1], T[2]);
	i = IsAscending(T); /* This kind of function has no relation with the type of ElemType. When the type of ElemType changes, the argument does not need to change */
	printf("After you call a function that tests ascending, i=%d(0:No 1:yes)\n", i);
	i = IsDescending(T);
	printf("After calling a function that tests descending, i=%d(0:No 1:yes)\n", i);
	if ((i = Max(T, &m)) == OK) /* Assign before compare */
		printf("T The maximum value in is:%d\n", m);
	if ((i = Min(T, &m)) == OK)
		printf("T The minimum value in is:%d\n", m);
	DestroyTriplet(&T); /* Function can also return no return value */
	printf("Destruction T Later, T=%u(NULL)\n", T);
}

Operation result:

 

Published 106 original articles, won praise 62, visited 40000+
Private letter follow

Topics: C