單鏈表的增刪改查

2021-09-26 00:26:22 字數 2785 閱讀 8819

#include

#include

#include

typedef

int data_t;

typedef

struct node /*定義結點*/

node;

typedef

struct list /*定義表結構*/

llt;

/*增刪改查函式宣告*/

intcreate_list

(llt *

*pplist)

;//建立鍊錶

intdestory_list

(llt *

*pplist)

;//銷毀鍊錶

intinsert_elem

(llt *plist,

int index,data_t newdata)

;//插入

intprint_list

(llt *plist)

;//遍歷

intdelete_elem

(llt *plist,

int index)

;//刪除

intreplace_elem

(llt *plist,

int index,data_t rdata)

;//替換

/****************主函式**********************/

intmain()

}destory_list

(&plist)

; plist =

null

;return0;

}/**********************函式************************/

/*建立鍊錶*/

intcreate_list

(llt *

*pplist)

*pplist =

(llt *

)malloc

(sizeof

(llt));

if((*pplist)

==null

)memset

(*pplist,0,

sizeof

(llt));

(*pplist)

->phead =

null;(

*pplist)

->ptial =

null;(

*pplist)

->len =0;

return0;

}/*銷毀鍊錶*/

intdestory_list

(llt *

*pplist)if(

(*pplist)

->phead !=

null)}

free

(*pplist)

;*pplist =

null

;return0;

}/*插入*/

intinsert_elem

(llt *plist,

int index,data_t newdata)

if(index <

0|| index > plist->len)

node *p =

(node *

)malloc

(sizeof

(node));

p->data = newdata;

if(index ==

0&& plist->len ==0)

else

if(index ==

0&& plist->len !=0)

else

if(index >

0&& index < plist->len)

p->pnext = ptmp->pnext;

ptmp->pnext = p;

}else

if(index == plist->len)

plist->len +=1

;return0;

}/*遍歷*/

intprint_list

(llt *plist)

node *ptmp = plist->phead;

while

(ptmp)

puts(""

);return0;

}/*刪除*/

intdelete_elem

(llt *plist,

int index)

if(index > plist->len || index <0)

node *p = plist->phead;

if(index ==0)

free

(p);

}else

if(index >0)

if(index == plist->len-1)

if(index != plist->len-1)

} plist->len -=1

;return0;

}/*replace*/

intreplace_elem

(llt *plist,

int index,data_t rdata)

if(index <

0|| index > plist->len)

int i;

node *p = plist->phead;

for(i =

0;i < index;i++

) p->data = rdata;

return0;

}

單鏈表增刪改查

include include include include using namespace std struct node node int x,node next null 帶參初始化 建立煉表頭結點,新增引用因為要改變指標的位址指向 void createlink node head 新增鍊...

單鏈表增刪改查

單鏈表單鍊錶 linked list 由各個記憶體結構通過乙個 next 指標鏈結在一起組成,每乙個內 存結構都存在後繼記憶體結構 鏈尾除外 記憶體結構由資料域和 next 指標域組成。單鏈表實現圖示 解析 data 資料 next 指標,組成乙個單鏈表的記憶體結構 第乙個記憶體結構稱為 鏈頭,最後...

單鏈表 增刪改查

目錄基本面試題 class heronode class singlelinkedlistpublic void add heronode heronode public void addbyorder heronode heronode if temp.next.no heronode.no el...