不帶頭結點的單鏈表

2021-10-19 22:54:21 字數 1933 閱讀 3532

/*

不帶頭結點的鍊錶的實現:

核心是直接設定頭指標指向第乙個節點

要注意此時的l的位址可能會發生改變.

同時還要注意一級指標和二級指標的區別.

注意linklist l和linklist *l的區別

和帶結點的單鏈表的操作相比較,

要考慮到頭指標就是頭結點,在一些會改變頭結點的情況下要仔細考慮.

*/#include

#include

#define null null

typedef

int elemtype;

typedef

struct lnodenode,

*linklist;

//初始化

bool initlist

(linklist *l)

//判空

bool isempty

(linklist l)

//遍歷

bool printlist

(linklist l)

printf

("\n");

return true;

}//頭插法建表

bool createfromhead

(linklist *l)

tem->data=ch;

tem->next=

(*l);(

*l)=tem;

}else

}return true;

}//尾插法建表

bool createfromtail

(linklist *l)

tem->data=ch;if(

(*l)

==null)

p->next=tem;

tem->next=null;

p=tem;

}else

}return true;

}//按位查詢

bool getelem

(linklist l,

int index,elemtype *e)

if(i==index)

return false;

}//按值查詢

bool localelem

(linklist l,

int*index,elemtype e)

if(l==null)

*index=i;

return true;

}//插入資料

bool insertlist

(linklist *l,

int index,elemtype e)

int i=1;

while

(i1&&p!=null)

if(p==null)

tem=

(node*

)malloc

(sizeof

(node));

if(tem==null)

tem->data=e;

if(p==

*l)else

return true;

}//刪除資料

bool dellist

(linklist *l,

int index,elemtype *e)

while

(p!=null&&iif(p==null)

node *tem;

if(p==

(*l)

)else

free

(tem)

;return true;

}//逆序

bool reverselist

(linklist l)

else}}

else

return true;

}//main()

intmain()

不帶頭結點的單鏈表

slist.h pragma once typedef int sldatatype typedef struct slistnode slistnode 不帶頭節點的單鏈表 鍊錶初始化 void slistinit slistnode phead 建立新結點 slistnode slistnewn...

不帶頭結點的單鏈表

單鏈表也可以不設頭結點,如圖212 所示。顯 然,基於這種結構的基本操作和帶有頭結點的線性鏈 表基本操作是不同的。bo2 8.cpp 是不帶頭結點的線 性鍊錶的基本操作。bo2 8.cpp 不帶頭結點的單鏈表 儲存結構由c2 2.h定義 的部分基本操作 9個 define destroylist c...

單鏈表的建立(帶頭結點以及不帶頭結點)

include stdio.h include stdlib.h typedef struct list list list headcreatlist 頭插法建立鍊錶,不帶頭結點 return head list tailcreatlist 尾插法建立鍊錶,不帶頭結點 r next s 將l指向的...