Data Structure triples

Source: Internet
Author: User

Review the data structure and find out what the data structure is today. I forgot what I learned at the beginning. Let's start again this time.

1. Abstract Data Type

/* ADT Triplett {Data Object: D = {E1, E2, E3 | E1, E2, E3 ε elemset (a set defining relational operations)} Data Relationship: R = {<E1, E2,> <E2, E3>} basic operation: inittriplet (T, V1, V2, V3) operation result: the Triple T, element E1, e2 and E3 are assigned the values of V1, V2, and V3 respectively. Result of the destroytriplet (t) operation: The productkey, devicename, and devicesecret are destroyed. Get (T, I, e) initial condition: the Triple T already exists, 0 <I <4 Operation Result: use e to return the value of the I element of T. Put (T, I, e) initial condition: the Triple T already exists 0 <I <4. Operation Result: the I-th element value of T is changed to E. If the element exists, the position of the element in the table is returned. If the element does not exist,-1 is returned. max (T, E) initial condition: the Triple T already exists. Operation Result: use e to return the maximum value of the three elements of T. Min (T, E) initial condition: the Triple T already exists. Operation Result: use e to return the minimum values of the three elements of T. Print (t) initial condition: the Triple T already exists. Operation Result: print each element value of the Triple T */

2. Source Code

# Include <stdio. h> # include <malloc. h> # include <stdlib. h> # define overflow-2 # define OK 1 # define error 0 typedef int status; typedef int elemtype; typedef elemtype * triplet; int inittriplet (triplet * t, int V1, int V2, int V3) {// construct a Triple T and assign values to V1, V2, V3 (* t) = (int *) malloc (3 * sizeof (INT) in sequence )); if (! T) Exit (overflow); (* t) [0] = V1; (* t) [1] = V2; (* t) [2] = V3; return OK;} int destorytriplet (triplet * t) {// destroy the triple free (* t); * t = NULL; Return OK;} int get (triplet T, int I, int * E) {// remove a component of the triplet. Use E to return the value of the I element of T if (I <1 | I> 3) return error; * E = T [I-1]; Return OK;} int put (triplet * t, int I, int e) {if (I <1 | I> 3) return overflow; (* t) [I-1] = E; Return OK;} int max (triplet T, int * E) {* E = (T [0]> = T [1])? (T [0]> = T [2])? T [0]: T [2]) :( (T [1]> = T [2])? T [1]: T [2]); Return OK;} int min (triplet T, int * E) {* E = (T [0] <= T [1])? (T [0] <= T [2])? T [0]: T [2]) :( (T [1] <= T [2])? T [1]: T [2]); Return OK;} void print (triplet t) {int I; elemtype ELEM, * P = & ELEM; for (I = 1; I <= 3; I ++) {Get (t, I, P); printf ("t [% d] = % d \ n", I, ELEM) ;}} void main () {triplet T, * t = & T; int E1, E2, E3, E, * P = & E; int I; printf ("\ n please input the triple element E1 E2 E3: \ n"); scanf ("% d", & E1, & E2, & E3 ); printf ("the output triple is \ n"); inittriplet (T, E1, E2, E3); print (t ); printf ("\ n please input the order of the productkey, devicename, and devicesecret \ n"); scanf ("% d", & I ); if (I> = 1 & I <= 3) {Get (t, I, P); printf (" The % d element in the \ n triple is: % d \ n ", I, * P);} else {printf (" incorrect input \ n ");} printf ("\ n enter the order I:"); scanf ("% d", & I ); if (I> = 1 & I <= 3) {printf ("\ n, enter the new element:"); scanf ("% d ", & E); Put (T, I, e); printf ("\ n"); print (t );} else {printf ("incorrect input! \ N ");} printf (" % d \ n ", max (T, P); printf (" \ n maximum value: % d \ n ", e ); min (T, P); printf ("\ n minimum value: % d \ n", e); destorytriplet (t );}

3. Running result

4. Summary

The C language should be solid, and the pointer and memory dynamic allocation should be reviewed and strengthened.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.