不帶頭結點的單鏈表的基本操作

2021-08-10 05:05:38 字數 2846 閱讀 1558

1、標頭檔案(nlist.h)

#pragma once

//不帶頭結點的單鏈表

typedef struct nnode

nnode,*nlist;

//鍊錶初始化

void initlist(nlist *pplist);

//頭插

bool insert_head(nlist *pplist,int val);

//尾插

bool insert_tail(nlist *pplist,int val);

//查詢

nnode *search(nlist plist,int key);

//刪除

bool delete(nlist *pplist,int key);

//獲取長度

int getlength(nlist plist);

//判空

bool isempty(nlist plist);

//清空

void clear(nlist *pplist);

//摧毀

void destroy(nlist *pplist);

//列印

void show(nlist plist);#pragma once

//不帶頭結點的單鏈表

typedef struct nnode

nnode,*nlist;

//鍊錶初始化

void initlist(nlist *pplist);

//頭插

bool insert_head(nlist *pplist,int val);

//尾插

bool insert_tail(nlist *pplist,int val);

//查詢

nnode *search(nlist plist,int key);

//刪除

bool delete(nlist *pplist,int key);

//獲取長度

int getlength(nlist plist);

//判空

bool isempty(nlist plist);

//清空

void clear(nlist *pplist);

//摧毀

void destroy(nlist *pplist);

//列印

void show(nlist plist);

2、原始檔(nlist.cpp)

#include#include#include#include"nlist.h"

//鍊錶初始化

void initlist(nlist *pplist)

//頭插

bool insert_head(nlist *pplist,int val)

//尾插

bool insert_tail(nlist *pplist,int val)

nnode *q;

for(q = *pplist;q->next != null;q = q->next);//找尾巴

q->next = p;

return true;

}//查詢

nnode *search(nlist plist,int key)

} return null;

}//刪除

bool delete(nlist *pplist,int key)

nnode *p = *pplist;

if(p->data == key)//刪除第乙個點

for(p = *pplist;p->next != null;p = p->next) }

return false;

}//獲取長度

int getlength(nlist plist)

return count;

}//判空

bool isempty(nlist plist)

//清空

void clear(nlist *pplist)

//摧毀

void destroy(nlist *pplist)

*pplist = null;

}//列印

void show(nlist plist)

printf("\n");

}

3、測試原始檔(test.cpp)

#include#include#include#include"nlist.h"

int main()

//查詢測試用例

/*nnode *s1 = search(list,3);

nnode *s2 = search(list,-1);

nnode *s3 = search(list,15);

printf("%d\n",s1->data);

printf("%d\n",s2->data);

printf("%d\n",s3->data);*/

//刪除測試用例

/*delete(&list,-1);

delete(&list,18);

delete(&list,0);

delete(&list,5);

delete(&list,14);

show(list);*/

//獲取長度測試

printf("%d\n",getlength(list));

show(list);

destroy(&list);

destroy(&list);

return 0;

}

單鏈表的基本操作(不帶頭結點)

node.h ifndef node h define node h 不帶頭結點的單鏈表 typedef struct node node,pnode endif node hlist.h ifndef list h define list h include node.h 頭插 void inse...

不帶頭結點的單鏈表

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

不帶頭結點的單鏈表

不帶頭結點的鍊錶的實現 核心是直接設定頭指標指向第乙個節點 要注意此時的l的位址可能會發生改變.同時還要注意一級指標和二級指標的區別.注意linklist l和linklist l的區別 和帶結點的單鏈表的操作相比較,要考慮到頭指標就是頭結點,在一些會改變頭結點的情況下要仔細考慮.include i...