單鏈表的基本介面

2021-09-25 09:35:38 字數 2491 閱讀 5714

#ifndef _slist_h_

#define _slist_h_

#include#includetypedef int sltdatatype;

typedef struct slistnode slistnode;

void slistinit(slistnode** pphead);

void slistdestory(slistnode** pphead);

slistnode* buyslistnode(sltdatatype x);

void slistpushfront(slistnode** pphead, sltdatatype x);

void slistpopfront(slistnode** pphead);

void slistinsertafter(slistnode* pos, sltdatatype x);

void slisteraseafter(slistnode* pos);

slistnode* slistfind(slistnode* phead, sltdatatype x);

void slistremove(slistnode** pphead, sltdatatype x);

void slistprint(slistnode* phead);

void slistreverse(slistnode **pphead);

void slistreverse2(slistnode **pphead);

#endif /*_slist_h_*/ //_slist_h_

#include "slist.h"

slistnode* buyslistnode(sltdatatype x) //建立新的節點

void slistinit(slistnode** pphead) //初始化

void slistdestory(slistnode** pphead) //銷毀

while ((*pphead)->next)

free(*pphead);

*pphead = null;

}void slistpushfront(slistnode** pphead, sltdatatype x) //頭插

void slistpopfront(slistnode** pphead) //頭刪

slistnode* tmp = (*pphead)->next;

free(*pphead);

*pphead = tmp;

}slistnode* slistfind(slistnode* phead, sltdatatype x) //查詢值為x的元素

} return null;

}void slistinsertafter(slistnode* pos, sltdatatype x) //後插,在第pos個節點後插入值為x的節點

void slisteraseafter(slistnode* pos) //後刪,刪除節點pos後的節點

pos->next = tmp->next;

free(tmp);

}void slistremoveall(slistnode** pphead, sltdatatype x) //刪除全部值為x的節點

for (tmp = *pphead; tmp && tmp->next;)

else

}}void slistremove(slistnode** pphead, sltdatatype x) //刪除第乙個值為x的元素

if ((*pphead)->data == x)

for (tmp = *pphead; tmp->next; tmp = tmp->next) }

}void slistprint(slistnode* phead) //輸出函式

printf("null\n");

}//實現單鏈表的逆序

//方法一:尾刪頭插

void slistreverse(slistnode **pphead)

*pphead = head;

}//方法二:頭尾轉換

void slistreverse2(slistnode **pphead)

*pphead = pre; //迴圈跳出後cur和next都已經指向空了,pre才是新的頭

}

#include "slist.h"

int main()

/*約瑟夫環的單鏈表實現

int _main()

plast->next = phead;

cur = plast;

for (; m > 1; m--)

slisteraseafter(cur);

} printf("%d", cur->data);

free(cur);

system("pause");

return 0;

}*/

單鏈表的基本操作

include include typedef struct lnodelnode,linklist 定義單鏈表 void create linklist linklist l 逆序實現 linklist q l for int i 1 i 5 i 正序實現 初始化單鏈表 void printf l...

單鏈表的基本操作

include iostream.h include stdio.h include string.h struct node node create return head node reverse node head delete head head null return pstr void ...

單鏈表的基本操作

由於做的畢業設計用到了鍊錶,所以將就就鍊錶的一些基本的操作自己寫了下來,以後看看吧,省得每次用到鍊錶都要自己寫了,寫一次就行了 很多不合理的地方,以後留著改吧 include include include typedef struct node link link cre link link in...