單鏈表的基本操作

2021-08-18 21:12:02 字數 2775 閱讀 7735

slist.h

#include #include #include typedef int datatype;

typedef struct listnode

node,*pnode;

//生成乙個新結點

pnode buyslistnode(datatype data);

//列印鍊錶

void printslist(pnode phead);

// 鍊錶的初始化

void slistinit(pnode* phead);

// 尾插

void slistpushback(pnode* phead, datatype data);

// 尾刪

void slistpopback(pnode* phead);

// 頭插

void slistpushfront(pnode* phead, datatype data);

// 頭刪

void slistpopfront(pnode* phead);

// 在鍊錶中查詢值為data的元素,找到後返回值為data的結點

pnode slistfind(pnode phead, datatype data);

// 在pos位置插入值為data的結點

void slistinsert(pnode* phead, pnode pos, datatype data);

// 刪除pos位置額結點

void slisterase(pnode* phead, pnode pos);

// 獲取鍊錶中值data的結點

int slistsize(pnode phead,datatype data);

// 判斷鍊錶是否為空

int slistempty(pnode phead);

// 銷毀聊表

void slistdestroy(pnode* phead);

///void testinitandpushback();

void testpopbackandpushfront();

void testfindandinsert();

void testeraseandsize();

slist.c

#include "slist.h"

void printslist(pnode phead)

while(pcur)

printf("null\n");

}void slistinit(pnode* phead)

pnode buyslistnode(datatype data)

pnewnode->data =data;

pnewnode->pnext =null;

return pnewnode;

}void slistpushback(pnode* phead, datatype data)

//鍊錶中有結點

else

pcur->pnext =buyslistnode(data); }}

void slistpopback(pnode* phead)

//鍊錶中只有乙個結點

else if((*phead)->pnext==null)

//鍊錶中含有多個結點

else

free(pcur);

pre->pnext =null; }}

void slistpushfront(pnode* phead, datatype data)

void slistpopfront(pnode* phead)

else }

pnode slistfind(pnode phead, datatype data)

while(phead)

phead=phead->pnext ;

} return null;

}void slistinsert(pnode* phead, pnode pos, datatype data)

else }

void slisterase(pnode* phead, pnode pos)

del=*phead;

while(del && del!=pos)

if(del->pnext !=null)

else }

int slistsize(pnode phead,datatype data)

while(phead)

phead=phead->pnext ;

} return count;

}int slistempty(pnode phead)

else }

void slistdestroy(pnode* phead)

cur=*phead;

while(cur)

*phead=null;}/

//測試

void testpushbackandpopback()

void testpushfrontandpopfront()

void testfindandinsert()

void testeraseandsize()

test.c

#define _crt_secure_no_warnings 1

#include "slist.h"

int main()

單鏈表基本操作

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

單鏈表基本操作

單鏈表的初始化,建立,插入,查詢,刪除。author wang yong date 2010.8.19 include include typedef int elemtype 定義結點型別 typedef struct node node,linkedlist 單鏈表的初始化 linkedlist...

單鏈表基本操作

include using namespace std define namelenth 20 define ok 0 define error 1 typedef struct flagnode node 生成結點 inline node newnode 銷毀化煉表 void destroylin...