zoukankan      html  css  js  c++  java
  • 数据结构与算法实例(数组实现)

    数据结构与算法实例分析—数组

    ★数组是一种最简单的数据结构,它占据一块连续的内存并且顺序存储数据,所以我们需要首先指定数组的大小
    ★数组的空间效率不是很好,会有空闲的区域没有得到充分的应用
    ★时间复杂度为O(1);
    ★数组一旦被定义,它的维度和维界就不会再改变,因此除了结构的初始化和销毁之外,数组就只有存取和修改元素值得操作

    1.数组的存储结构

    2.基本操作

    ⑴.建造空间并进行初始化:struct triple *triple_init(int v1,int v2,int v3);
    ⑵.释放已开辟空间:void triple_free(struct triple *t);
    ⑶.更改空间数据:void triple_set(struct triple *t,int index,int e);
    ⑷.访问空间数据:int triple_get(struct triple *t,int index);
    ⑸.判断数据是否升序:int triple_isascending(struct triple *t);
    ⑹.判断数据是否降序:int triple_isdescending(struct triple *t);
    ⑺.查找数据中最大值:int triple_max(struct triple *t);
    ⑻.查找数据中最小值:int triple_min(struct triple *t);

    3.基本代码

    Triple.h

    #ifndef __TRIPLE_H__
    #define __TRIPLE_H__
    //定义结构体存放存储结构数组的首地址
    struct triple;
    //开辟数组(内存空间),返回指向已开辟空间的指针
    struct triple *triple_init(int v1,int v2,int v3);
    void triple_free(struct triple *t);
    //将元素存储入内存空间
    void triple_set(struct triple *t,int index,int e);
    //将元素从内存空间取出
    int triple_get(struct triple *t,int index);
    //判断数组中的元素是否为升序排列
    int triple_isascending(struct triple *t);
    //判断数组中的元素是否为降序排列
    int triple_deascending(struct triple *t);
    //从存储结构中取出数值比较,返回数组中的最大值
    int triple_max(struct triple *t);
    //从存储结构中取出数值比较,返回数组中的最小值
    int triple_min(struct triple *t);
    
    #endif
    

    Triple.c

    #include <stdlib.h>
    #include <assert.h>
    #include “triple.h”
    //定义结构体作为一级结构,结构体用来存放指向整型的指针变量
    Struct triple{
         Int *data;
    }
    //将v1,v2,v3存放到定义的存储结构中
    Struct triple *triple_init(int v1,int v2,int v3)
    {
        Struct triple *t=NULL;
        //使用malloc函数开辟指定内存大小的空间,返回空间首地址强制类型转换为指向triple结构体类型,最后将变量赋值到t
        t=(struct triple *)malloc(sizeof(struct triple);
        if(t==NULL)  return NULL;
        //断言t不为空
        assert(t!=NULL);
        t->data=NULL;
        //t->data变量中存放长度为3的数组的首地址
        t->data=(int *)malloc(sizeof(int)*3);
        if(t->data==NULL){
        free(t);
        return NULL}
    
        assert(t->data!=NULL);
    
        t->data[0]=v1;
        t->data[1]=v2;
        t->data[2]=v3;
    
        return t;
    }
    
    void triple_free(struct triple *t)
    {
        assert(t->data!=NULL);
        assert(t!=NULL);
        //将内存空间逐级释放
        free(t->data);
        free(t);
    }
    //将传入的整型元素e,插入到数组的index位置
    void triple_set(struct triple *t,int index,int e)
    {
        assert(t!=NULL);
        assert(t->data!=NULL);
        assert(index>=0&&index<3);
    
        t->data[index]=e;
    }
    
    Int triple_get(struct triple *t,int index)
    {
        assert(t!=NULL);
        assert(t->data!=NULL);
        assert(index>=0&&index<3);
    
        return  t->data[index];
    }
    //判断数组中的元素是否按照升序存放,是返回1,否返回0
    int triple_isascending(struct triple *t)
    {
        assert(t!=NULL);
        assert(t->data!=NULL);
    
        if(t->data[0]<=t->data[1]&&t->data[1]<=t->data[2]){
        return 1;
        }
        return 0;
    }
    
    int triple_isdeascending(struct triple *t)
    {
        return 0;
    }
    //通过结构体指针变量t,将数组中的元素依次取出做比较,返回最大值
    int triple_max(struct triple *t)
    {
        int _max;
        assert(t!=NULL);
        assert(t->data!=NULL);
    
        _max=t->data[0];
    
        If(_max<t->data[1]) _max=t->data[1];
        If(_max<t->data[2]) _max=t->data[2];
    
        return max;
    }
    //通过结构体指针变量t,将数组中的元素依次取出做比较,返回最小值
    Int triple_min(struct triple *t)
    {
        Int _min;
    
        assert(t!=NULL);
        assert(t->data!=NULL);
    
        _min=t->data[0];
    
        if(_min>t->data[1]) _min=t->data[1];
        if(_min>t->data[2]) _min=t->data[2];
    
        return _min;
    }
    

    Main.c

    #include <stdio.h>
    #include <stdlib.h>
    
    #include "triple.h"
    
    int main(int argc, char *argv[]) {
        struct triple *t=NULL;
    
        t=triple_init(23,45,87);
    
        if(triple_isascending(t)){
        printf("The array is ascending.
    ");
        }else{
        printf("The array is not ascending.
    ");
        }	
        printf("The max is %d
    ",triple_max(t));
        printf("The min is %d
    ",triple_min(t));
    
        triple_set(t,2,234);
    
        printf("The 1th element is %d
    ",triple_get(t,2));
    
        if(triple_isascending(t)){
        printf("The array is ascending.
    ");
        }else{
        printf("The array is not ascending.
    ");
        }
    
        triple_free(t);
    
        return 0;
    }
    

    四.运行结果

  • 相关阅读:
    scikit-learn算法选择路径图
    mac 添加new file.md
    mac-VBox-Centos6.6安装增强功能
    VirtualBox中安装CentOS使得在ssh和外网都能正常链接
    vboxnetctl: no such file or directory
    mac vim 配色
    埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛 B合约数
    codeforces 965E Trie+multiset
    bzoj 4337 树的同构
    codeforces 963B Destruction of a Tree
  • 原文地址:https://www.cnblogs.com/miaowulj/p/12235638.html
Copyright © 2011-2022 走看看