码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构与算法实例(数组实现)

时间:2020-01-27 12:23:30      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:print   改变   数组   需要   ==   转换   算法   har   更改   

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

★数组是一种最简单的数据结构,它占据一块连续的内存并且顺序存储数据,所以我们需要首先指定数组的大小
★数组的空间效率不是很好,会有空闲的区域没有得到充分的应用
★时间复杂度为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.\n");
    }else{
    printf("The array is not ascending.\n");
    }   
    printf("The max is %d\n",triple_max(t));
    printf("The min is %d\n",triple_min(t));

    triple_set(t,2,234);

    printf("The 1th element is %d\n",triple_get(t,2));

    if(triple_isascending(t)){
    printf("The array is ascending.\n");
    }else{
    printf("The array is not ascending.\n");
    }

    triple_free(t);

    return 0;
}

四.运行结果

技术图片

数据结构与算法实例(数组实现)

标签:print   改变   数组   需要   ==   转换   算法   har   更改   

原文地址:https://www.cnblogs.com/miaowulj/p/12235638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!