順序表與鍊錶( )

2021-10-18 13:20:08 字數 4117 閱讀 5880

鍊錶連續的儲存空間儲存任意元素。12

345結構定義:

1、size = 9

2、length = 5

3、data_type = ***

#

include

#include

typedef

struct

vector

vector;

void

init

(vector *vector,

int size)

void

clear

(vector *vector)

intmain()

int

insert

(vector *vector,

int loc,

int value)

if(vector->length >= vector->size)

for(

int i = vector->length; i > loc;

--i )

vector->data[loc]

= value;

//將值賦值給插入的地方

vector->length ++

;return ok;

}

// 請在下面實現擴容函式 expand

void

expand

(vector *vector)

free

(old_data)

;}

int

search

(vector *vector,

int value)

}return-1

;}

int

delete_node

(vector *vector,

int index)

for(

int i = index +

1; i < vector->length; i++

) vector->length --

;//更新length順序表長度;

return ok;

}

void

print

(vector *vector)

printf

("%d"

, vector->data[i]);

}printf

("\n");

}

#

include

#include

typedef

struct

node

node,

*linkedlist;

linkedlist insert

(linkedlist head, node *node,

int index)

if(index ==0)

node *current_node = head;

//定義current_node指向頭結點head

int count =0;

//定義count進行計數

while

(current_node->next !=

null

&& count < index -1)

if(count == index -1)

return head;

}// 請在下面實現輸出函式 output

void

output

(linkedlist head)

node *current_node = head;

while

(current_node !=

null

)printf

("\n");

}// 請在下面實現函式 clear

void

clear

(linkedlist head)

}int

main()

output

(linkedlist)

;//呼叫輸出函式

clear

(linkedlist)

;//釋放記憶體

return0;

}

輸入格式:

第一行輸入是乙個整數 n(1≤n≤100),表示一共要執行 n 次插入操作。接下來輸入 n 行,每行輸入兩個整數 p 和 q(0 <= p, q≤100),其中 p 表示結點插入鍊錶中的位置(從下標為 0 開始),q 表示插入元素的值,兩個整數之間用乙個空格隔開,行末沒有空格。

輸出格式:

輸出一共 n + 1 行。前 n 行對應每次插入操作,一行乙個結果,如果乙個元素成功插入到了鍊錶中,請輸出success,如果插入失敗則輸出failed。第 n + 1行輸出最後的鍊錶,每兩個整數之間用乙個空格隔開,行末不需要多餘的空格。

}

// 請在下面實現刪除結點函式 delete_node

linkedlist delete_node

(linkedlist head,

int index)

node *current_node = head;

int count =0;

if(index ==0)

while

(current_node->next !=

null

&& count < index -1)

if(count == index -

1&& current_node->next !=

null

)return head;

}

// 請在下面實現鍊錶的反轉函式 reverse

linkedlist reverse

(linkedlist head)

node *next_node,

*current_node;

current_node = head->next;

head->next =

null

;while

(current_node !=

null

)return head;

}

順序表與鍊錶

單向鍊錶 雙向鍊錶 迴圈鍊錶 插入刪除過程要注意,初始指標應指向鍊錶尾節點 若當插入到頭前時,找不到前驅節點 當在更新尾節點時需要更新頭指標 鍊錶排序使用氣泡排序最為適合 資料結構 結構定義 結構操作 include include include include define malloc2 my...

順序表與鍊錶

它是n個具有相同特性的資料元素的有限序列。線性表是一種在實際中廣泛使用的資料結構,常見的線性表 順序表 鍊錶 棧 佇列 字串 邏輯上連續,物理上不一定連續 儲存方式 通常是陣列或者鏈式結構 順序表是用一段實體地址連續的儲存單元依次儲存資料元素的線性結構,一般情況下採用陣列儲存。在陣列上完成資料的增刪...

順序表與鍊錶

順序表 1.實現 順序表的實現 typedef struct lnode list struct lnode list ptrl 2.建立空的順序表 建立空順序表 list buildlist 3.查詢元素 查詢元素 intfind elementtype e,list ptrl 4.插入元素 插入...