順序表操作

2021-08-02 10:37:22 字數 923 閱讀 3209

順序表的簡單實現。包括表的初始化,插入,刪除,歸併。記憶體分配使用c語言的malloc和realloc來實現。

假設在每個合法位置進行插入和刪除操作的概率相等,則這兩種操作每次的平均移動次數為n/2,時間複雜度為o(n)。

**如下:

#include #include #include using namespace std;

#define listincrement 10

//表結構

typedef struct sqlist;

//插入

bool insertlist(sqlist& l,int pos,int e)

int* pp=&l.elem[pos-1];//插入位置

int* q=&l.elem[l.len];

//依次後移

for(;q>=pp;q--)

*pp=e;

++l.len;

return true;

}//刪除

bool deletelist(sqlist& l,int pos,int& e)

--l.len;

return true;

}//初始化,分配記憶體

bool initlist(sqlist& l)

//輸出表

順序表操作

pragma once include include define max size 20 typedef int datatype typedef struct seqlist seqlist seqlist s seqlist.h void seqlistinit seqlist ps 結構體...

靜態順序表順序表的基本操作

一般採用陣列表示順序表,陣列有靜態陣列和動態陣列之分,在此我們採用靜態陣列表示靜態順序表,如圖為線性表的結構 下面實現順序表的基本操作 初始化 銷毀 尾插 頭插 尾刪 頭刪 根據指定元素刪除 指定位置插入和刪除 查詢 靜態順序表的結構定義 define maxsize 100 typedef int...

順序表基本操作

先建個seqlist.h 如下 include include define maxn 100 定義線性表的長度 typedef struct seqlisttype void seqlistinit seqlisttype sl 初始化順序表 int seqlistlength seqlistty...