單鏈表對元素的基本操作

2021-09-13 09:29:31 字數 1988 閱讀 5309

單鏈表對元素的插入 刪除 和查詢

在這裡插入**片

#include

#include

#include

/************************型別定義************************/

typedef

char datatype;

void

printelem

(datatype x)

typedef

struct node lnode,

*linklist;

/**********************單鏈表的基本操作*********************/

//建立空的單鏈表 (帶頭結點)

linklist create_linklist()

//銷毀單鏈表

void

destroy_linklist

(linklist *h)

(*h)

->next =

null

;printf

("鍊錶已銷毀\n");

}//求表長

intlength_linklist

(linklist h)

return j;

}//按序號查詢

linklist get_linklist

(linklist h,

int i)

return p;}}

//按值查詢

linklist get_linklist

(linklist h,datatype x)

p =get_linklist

(h,fl)

;return p;

}//插入

intinsert_linklist

(linklist h,

int i,datatype x)

else

}//刪除(按序號)

intdelete_linklist

(linklist h,

int i)

else}}

//刪除(按值)

intdelete_linklist

(linklist h,datatype x)

else

else}}

/*******************程式的功能函式**************************/

//列印單鏈表(帶頭結點)

void

print

(linklist h)

}printf

("\n");

}//查詢(按值或按序號查詢,使用者可選擇任意一種方式)

void

search

(linklist h)

else

}else

if(n==2)

if(p==

null

)printf

("該元素不存在");

else}}

//插入

void

insert

(linklist h)

}//刪除 (按值或按序號刪除,使用者可選擇任意一種方式)

void

delete

(linklist h)

else

if(t ==2)

if(ret ==1)

printf

("刪除成功");

else

if(ret ==-1

)printf

("刪除失敗請輸入有效序號");

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

void

printchoice()

intmain()

switch

(n)}

}

刪除線格式

list item

對單鏈表的基本操作

includeusing namespace std typedef int elemtype typedef bool status define ok true define error false typedef struct lnodelnode,linklist 取值 取第i個位置上的資料...

對單鏈表的操作

include include typedef struct lnodelnode,linklist void createlist linklist l,int n p next null void getelem linklist l,int i,int e if p j i return pr...

單鏈表基本操作

include include include include includeusing namespace std typedef struct node node,plinklist plinklist createfromhead node pstnode node malloc sizeof...