帶頭結點的單鏈表實現

2021-10-19 22:04:16 字數 2461 閱讀 4738

/*

帶頭結點的單鏈表實現:

通過對結點的指標操作來實現各個核心功能.

**中要注重核心思想,但是必要的安全檢查還是必須的

後期可以省略,但是要有這個意識

*/#include

#include

#define null null

typedef

int elemtype;

typedef

struct lnode node,

*linklist;

//初始化操作

bool initlist

(linklist *l)

(*l)

->data=null;

(*l)

->next=null;

return true;

}//判空

bool isempty

(linklist l)

else

}//頭插法建表

bool createfromhead

(linklist l)

tem->data=ch;

tem->next=l->next;

l->next=tem;

return true;

}else

}return true;

}//尾插法建表

bool createfromtail

(linklist l)

tem->data=ch;

tem->next=p->next;

p->next=tem;

p=p->next;

//注意尾指標要時刻指向末尾

}else

}return true;

}//遍歷操作

bool printlist

(node* l)

node *tem;

tem=l->next;

elemtype e;

while

(tem!=null)

printf

("\n");

return true;

}//按位查詢

bool getelem

(linklist l,

int index,elemtype *e)

while

(iif(i==index)

else

}//按值查詢

bool localelem

(linklist l,

int*index,elemtype e)

int i=1;

while

(tem->data!=e&&tem!=null)

if(tem->data==e)

else

}//插入操作,在index的位置插入元素.

bool insertlist

(linklist l,

int index,elemtype e)

while

(i1&&tem!=null)

if(tem==null)

p=(node*

)malloc

(sizeof

(node));

if(p==null)

p->data=e;

p->next=tem->next;

tem->next=p;

return true;

}//刪除操作

bool dellist

(linklist l,

int index,elemtype *e)

while

(i1&&tem==null)

if(tem==null)

node *p;

//臨時指標

p=tem->next;

tem->next=p->next;

*e=p->data;

return true;

}//逆序,頭插法

bool reverselist

(linklist l)

return true;

}else

}//main()

intmain()

createfromtail

(l);

printlist

(l);

elemtype e;

//getelem(l,2,&e);

localelem

(l,&e,2)

;printf

("%d\n"

,e);

insertlist

(l,1,99

);printlist

(l);

dellist

(l,1

,&e)

;printf

("%d"

,e);

return0;

}

單鏈表的實現 帶頭結點

linklist.cpp 定義控制台應用程式的入口點。include stdafx.h include using namespace std template struct node template class linklist template linklist linklist templa...

帶頭結點的單鏈表實現

includeusing namespace std typedef char datatype struct node 按序號查詢鍊錶 node getnode node head,int i if i j return null else return p 說明沒找到第 i 個結點 按值查詢 n...

帶頭結點的單鏈表

帶頭結點的單鏈表 1 頭結點 在棧區開闢,指標域指向第乙個首元結點,資料域不儲存資料,可以儲存當前結點的個數 2 普通結點 無論是頭結點還是普通結點都是乙個結構體型別,由指標域和資料域組成 指標域指向下乙個結點,儲存下乙個結點的位址 資料域可以設定成聯合體型別,成員由資料元素和結點個數組成,之所以將...