資料結構與演算法例項 陣列實現

2021-10-25 14:33:06 字數 2774 閱讀 7708

★陣列是一種最簡單的資料結構,它佔據一塊連續的記憶體並且順序儲存資料,所以我們需要首先指定陣列的大小

★陣列的空間效率不是很好,會有空閒的區域沒有得到充分的應用

★時間複雜度為o(1);

★陣列一旦被定義,它的維度和維界就不會再改變,因此除了結構的初始化和銷毀之外,陣列就只有訪問和修改元素值得操作

⑴.建造空間並進行初始化: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);

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 #include #include 「triple.h」

//定義結構體作為一級結構,結構體用來存放指向整型的指標變數

struct triple

//將v1,v2,v3存放到定義的儲存結構中

struct triple *triple_init(int v1,int v2,int v3)

assert(t->data!=null);

t->data[0]=v1;

t->data[1]=v2;

t->data[2]=v3;

return t;

}void triple_free(struct triple *t)

//將傳入的整型元素e,插入到陣列的index位置

void triple_set(struct triple *t,int index,int e)

int triple_get(struct triple *t,int index)

//判斷陣列中的元素是否按照公升序存放,是返回1,否返回0

int triple_isascending(struct triple *t)

return 0;

}int triple_isdeascending(struct triple *t)

//通過結構體指標變數t,將陣列中的元素依次取出做比較,返回最大值

int triple_max(struct triple *t)

//通過結構體指標變數t,將陣列中的元素依次取出做比較,返回最小值

資料結構與演算法之陣列

陣列的基本概念 陣列是最簡單最常用的資料結構,但是也有一些注意事項 1 陣列的分配方式以及儲存位置 2 初始化 3 不同語言中的陣列高階定義 4 多維陣列 c c 中陣列分配方式 1 int a 10 適用於陣列長度已知或者對陣列長度不敏感的情況,比如定義乙個字串。2 int a int mallo...

資料結構與演算法 陣列

陣列是一種線性表資料結構。它用一組連續的記憶體空間,來儲存一組具有相同型別的資料。其中有幾個重要的概念 非線性表 連續的記憶體空間 儲存相同型別的資料 如圖所示,這是乙個長度為5的int陣列arr,我們假設起始的記憶體位址為1000,那麼第乙個元素的記憶體位址範圍就是 1000 1003,這是因為乙...

資料結構與演算法 陣列

題型1 如何用遞迴實現陣列求和 方法1 題型2 如何用乙個for迴圈列印乙個二維陣列 方法1 array在二維陣列中的行號和列號分別為 i maxy i maxy 題型3 用遞迴和非遞迴的方法實現二分查詢 題型4 如何在排序陣列中,找出給定數字出現的次數 方法1 二分查詢,分別找出左邊界和右邊界,左...