資料結構之順序表

2021-10-12 19:22:41 字數 3245 閱讀 9112

//#pragma once

#ifndef sqlist_h_

#define sqlist_h_

#pragma region header

#include #include #include #pragma endregion

#pragma region some rudimentary definition

using elemtype = int; //typedef int elemtype

constexpr int list_init_size = 100; //初始分配大小

constexpr int listincrement = 10; //分配增量

constexpr int ok = 0;

constexpr int error = -1;

constexpr int true = 1;

constexpr int false = 0;

#pragma endregion

#pragma region the struct of sqlist

struct sqlist ;

#pragma endregion

#pragma region function of sqlist

inline void existlist(sqlist l)

};int initlist_sq(sqlist& l);

int destorylist_sq(sqlist& l);

int clearlist_sq(sqlist& l);

int listempty_sq(sqlist l);

int listlength_sq(sqlist l);

int getelem_sq(elemtype& e, sqlist l, int i);

int locateelem_sq(sqlist l, elemtype e, int (*compare)(elemtype, elemtype));

int priorelem_sq(elemtype& pre_e, sqlist l, elemtype cur_e);

int nextelem_sq(elemtype& next_e, sqlist l, elemtype cur_e);

int listinsert_sq(sqlist& l, int i, elemtype e);

int listdelete_sq(sqlist& l, int i, elemtype& e);

int listprint_sq(sqlist l);

#pragma endregion

#endif // !sqlist_h_

#include "sqlist.h"

#pragma region function of sqlist

/*初始化線性表*/

int initlist_sq(sqlist& l)

/*摧毀線性表*/

int destorylist_sq(sqlist& l)

/*清空線性表*/

int clearlist_sq(sqlist& l)

/*判斷線性表是否為空*/

int listempty_sq(sqlist l)

else

}/*線性表長度*/

int listlength_sq(sqlist l)

/*線性表第i個位置的值*/

int getelem_sq(elemtype& e, sqlist l, int i)

e = l.elem[i - 1];

return ok;

}/*線性表中第乙個與e滿足compare()關係的位置*/

int locateelem_sq(sqlist l, elemtype e, int (*compare)(elemtype, elemtype))

} return false;

}#pragma region temp function

int compare_equal(elemtype a, elemtype b)

#pragma endregion

/*前驅*/

int priorelem_sq(elemtype& pre_e, sqlist l, elemtype cur_e)

return ok;

}/*後驅*/

int nextelem_sq(elemtype& next_e, sqlist l, elemtype cur_e)

else if (temp == l.listlength)

else

}/*在第i位置之前插入e*/

int listinsert_sq(sqlist& l, int i, elemtype e)

if (l.listlength >= l.listsize)

elemtype* q = &l.elem[i - 1];

for (elemtype* p = &l.elem[l.listlength - 1]; p >= q; --p)

*q = e;

l.listlength++;

return ok;

}/*刪除第i個元素*/

int listdelete_sq(sqlist& l, int i, elemtype& e)

elemtype* p = &l.elem[i - 1];

e = *p;

elemtype* q = &l.elem[l.listlength - 1];

for (++p; p<= q; p++)

l.listlength--;

return ok;

}/*遍歷表*/

int listprint_sq(sqlist l)

return ok;

}#pragma endregion

#include "sqlist.h"

#pragma region temp function

/*尋找第乙個大於b的位置*/

學習筆記,歡迎指正。

資料結構之順序表

首先是標頭檔案seqlist.h ifndef seqlist h define seqlist h include includeusing namespace std define elemtype int define seqlist default size 10 typedef struc...

資料結構之順序表

順序表的思想容易了解,但是 不容易掌握,我這裡根據老師所提供的 進行一下簡單的總結 這個 包含順序表的查詢,插入,刪除,建表,輸出資料 includeusing namespace std define ok 1 define error 0 define overflow 2 typedef in...

資料結構之順序表

順序表就是按照順序儲存方式儲存的線性表,該線性表的結點按照邏輯次序一次存放在計算機的一組連續的儲存單元中如下圖 由於順序表是一次存放的,只要知道了該順序表的首位址以及每個資料元素所占用的儲存長度,那麼我們就很容易計算出任何乙個資料元素 也就是資料繫結點 的位置。1 結點資料型別 public cla...