zoukankan      html  css  js  c++  java
  • 三元组数据结构

    欢迎访问我的新博客:http://www.milkcu.com/blog/

    原文地址:http://www.milkcu.com/blog/archives/1368823560.html

    摘自《数据结构:C语言版》(严蔚敏,吴伟民编著)第12页例1-7。

    抽象数据类型Tiplet的表现与实现。

    C语言(本书未直接采用类和对象等设施,而是从C语言中精选了一个核心子集,并增添C++语言的引用调用参数传递方式等,构成一个类C描述语言)源代码如下:

    # include <stdio.h>
    # include <stdlib.h>
    # define TRUE 1
    # define FALSE 0
    # define OK 1
    # define ERROR 0
    # define INFREASIBLE -1
    # define OVERFLOW -2
    typedef int Status;
    typedef int ElemType;
    
    typedef ElemType * Triplet;
    
    Status InitTriplet(Triplet & T, ElemType v1, ElemType v2, ElemType v3);
    Status DestroyTiplet(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);
    Status Show(Triplet T);
    
    Status InitTriplet(Triplet & T, ElemType v1, ElemType v2, ElemType v3)
    {
    	T = (ElemType *)malloc(3 * sizeof(ElemType));
    	if(!T) {
    		exit(OVERFLOW);
    	}
    	T[0] = v1;
    	T[1] = v2;
    	T[2] = v3;
    	return OK;
    }
    Status DestroyTiplet(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;
    }
    Status Show(Triplet T)
    {
    	int i;
    	for(i = 0; i < 3; i++) {
    		printf("%d  ", T[i]);
    	}
    	putchar('\n');
    	return OK;
    }
    
    int main(void)
    {
    	Triplet T;
    	InitTriplet(T, 9, 3, 0);
    	Show(T);
    	ElemType e;
    	Get(T, 2, e);
    	printf("%d\n", e);
    	Put(T, 1, 5);
    	Show(T);
    	printf("%d  %d\n", IsAscending(T), IsDescending(T));
    	ElemType e1, e2;
    	Max(T, e1);
    	Min(T, e2);
    	printf("%d  %d\n", e1, e2);
    }

    参考资料

    • 《数据结构:C语言版》(严蔚敏,吴伟民编著)

    (全文完)

  • 相关阅读:
    426 根据gop 讲解x264整个过程
    426 x264全局 完整有些东西还没理解
    SAD SATD转过来的
    帧内预测1
    4.1总结
    426 大话dct
    426 pixel赋值问题 mbcmp函数 宏定义
    426 pts dts
    每天进步一点点IAR for ARM_V6.30.1
    每天进步一点点NIOS II按键中断程序
  • 原文地址:https://www.cnblogs.com/milkcu/p/3808895.html
Copyright © 2011-2022 走看看