Main.c
1#include"Ch_1.h"2 3 intMain ()4 {5 Triplet p;6 Status i;7 elemtype m;8i = Initlist (&p,1,2,3);9printf"initlist:%d,%d,%d\n", p[0],p[1],p[2]);Ten Onei = Get (p,2, &m); A if(i = =OK) -printf"%d\n", m); - thePut (P,2,3); - if(i = =OK) -printf"%d,%d,%d\n", p[0], p[1], p[2]); - +i =isascending (p); -printf"yes:1 no:0%d\n", i); + Ai =isdesending (p); atprintf"yes:1 no:0%d\n", i); - -printf"Max:%d\n", Max (P, &m)); - -Destroytriplet (&p); - return 0; in}
Ch_1.h
1#include <stdio.h>2#include <stdlib.h>3 #defineOK 14 #defineERROR 05 #defineOVERFLOW-26typedefintElemtype;7typedefintStatus;8typedef ELEMTYPE *Triplet;9 TenStatus initlist (Triplet *t, Elemtype v1, Elemtype v2, Elemtype v3)//Constructing ternary arrays One { A*t = (elemtype*)malloc(3*sizeof(Elemtype)); - if(!*T) - exit (OVERFLOW); the(*t) [0] =v1; -(*t) [1] =v2; -(*t) [2] =v3; - returnOK; + } - +Status Get (Triplet T,intI, Elemtype *e)//return the corresponding element value with e A { at if(I <1|| i >3) - returnERROR; -*e = T[i-1]; - returnOK; - } - inStatus Put (Triplet T,intI, Elemtype e)//change the corresponding element value - { to if(I <1|| i >3) + returnERROR; -T[i-1] =e; the } * $Status isascending (Triplet T)//AscendingPanax Notoginseng { - return((t[0] <= t[1]) && (t[1] <= t[2])); the } + AStatus isdesending (Triplet T)//Descending the { + return((t[0] >= t[1]) && (t[1] >= t[2])); - } $ $Status Max (Triplet T, Elemtype *e) - { - if(t[0] >= t[1]) the { - if(t[0] >= t[2])Wuyi*e = t[0]; the Else -*e = t[2]; Wu } - About Else $ { - if(t[1] >= t[2]) -*e = t[1]; - Else A*e = t[2]; + } the } - $Status Destroytriplet (Triplet *T) the { the Free(*T); the*t =NULL; the returnOK; -}
Operation Result:
initlist:1,2,3
2
1,3,3
Yes:1 no:0 1
Yes:1 no:0 0
Max:3
Data structure (C language) Chapter 1th