C單鏈表操作

2022-07-04 08:21:11 字數 3411 閱讀 4874

頭指標存放鍊錶元素數量,頭節點開始存放資料,尾節點指向null

list.h

#ifndef _list_h

#define _list_h#include

#include

#define debug 0typedef

struct

nodenode;

node * l_malloc(); //

分配記憶體

node * init(); //

初始化void show(node *list); //

顯示鍊錶元素

int lsearch_pos(node *list,int n); //

左搜尋int rsearch_pos(node *list,int n); //

右搜尋int find_val(node *list,int val); //

值搜尋node * insert(node *list,int pos,int n); //

插入節點

node * del_node(node *list,int n); //

指定位置刪除節點

node * del_val(node *list,int val); //

指定值刪除節點

#endif

list.c

#include "

list.h"/*

************************************

* 分配記憶體,當失敗時重新嘗試1次

************************************

*/node *l_malloc()

while( null == p && --cnt); //

當失敗時重新嘗試

if( 0 >cnt )

returnp;}

/*************************************

* 初始化

************************************

*/node *init()

pcur->next =null;

return

head; }/*

************************************

* 顯示

************************************

*/void show(node *list)

printf(

"end\n\n");

}/*************************************

* 從左開始搜尋

************************************

*/int lsearch_pos(node *list,int

n)

for( int cnt = 0; cnt < n ; cnt++)

return list->val; }/*

************************************

* 從右開始搜尋

************************************

*/int rsearch_pos(node *list,int

n) node *pcur = list->next; //

定義2個指標:當前位置

node *ptag = list->next; //

目標位置

for( int cnt = 0; cnt < n ; cnt++)

//使當前位置與目標位置間隔 n

while( null != pcur )

return ptag->val;}/*

************************************

* 從左開始搜尋指定值

* 有值返回位置,無值返回-1

************************************

*/int find_val(node *list,int

val)

}return -1;}

/*************************************

* 指定位置插入新值

************************************

*/node * insert(node *list,int pos,int

n)

if( null == pcur->next )

else

list->val++;

return

list;}/*

************************************

* 指定位置刪除值

************************************

*/node * del_node(node *list,int

n) node *pcur =null;

node *ptag =list;

for( int cnt = 0; cnt < n ; cnt++)

pcur->next = ptag->next;

free

(ptag);

list->val--;

return

list;}/*

************************************

* 指定值刪除節點

************************************

*/node * del_val(node *list,int

val)

return

del_node(list,n);

}

main.c

#include "

list.h

"int

main()

for( int cnt = 0;cnt <20 ;cnt ++)

list = insert( list, 6 ,99

); printf(

"after insert a value at 6th,the list is:\n");

show(list);

list = insert( list, -6 ,99

); printf(

"after insert a value at -6th,the list is:\n");

show(list);

}

C單鏈表操作

今天面試給 宇龍酷派 鄙視了。我想說,其實鍊錶反轉我會!單鏈表 初始化 建立 顯示 刪除 插入特定位置 刪除特定位置 反轉操作。include include include include typedef struct student node 初始化 node initnode head nex...

C單鏈表操作

函式順序依次為單鏈表的建立 頭插和尾插法 初始化,判空,遍歷,求鍊錶長度,按值查詢,按位查詢,插入,刪除,銷毀操作 並且在主函式中舉例說了鍊錶的建立,遍歷,求長,刪除,插入操作 高階操作 c迴圈鍊錶 include include typedef int datatype 用datatype 替代 ...

C單鏈表的操作

單鏈表的建立 刪除結點 插入新結點 遍歷但鍊錶。很多不足之處,希望多多提意見改正 1 鍊錶 1 單鏈表的建立 首先建立乙個節點,將頭結點 q臨時節點 建立節點p。都指向這個節點。接下來迴圈建立鍊錶的每個節點,為了實現人機互動,最好在建立每個節點的時候都詢問是否建立節點,建立下乙個單獨節點結束之後。然...