//创建链表 包含头节点 |
int creatLink<Link **phead> { int res = 0; int num; Link *ptm = <Link *>malloc<sizeof<Link>>; ptm->data = 0; *phead = ptm; printf<"请输入数据,以0结束!!!\n">; scanf<"%d", &num>; while <num != 0> { Link *pNew = <Link *>malloc<sizeof<Link>>; if <pNew == NULL> { res = -1; printf<"pNew==NULL 创建链表失败 error:%d\n",res>; } pNew->data = num; 个体工商年报ptm->next = pNew; ptm = pNew;兰花养殖 printf<"请输入数据,以0结束!!!\n">; scanf<"%d", &num>; } ptm->next = NULL; return res; } |
//打印链表 |
int printLink<Link *pHead> { int res = 0; Link *p = pHead->next; if <p == NULL> { res = -1; printf<"printfLink<> err:%d 链表为空打印失败\n",res>; return res; } 浪漫传说漫画while <p != NULL> { printf<"data=%d\n",p->data>; p = p->next; } return res; } |
//插入链表在data=x的前面插入data=y; |
int inrtLink<Link *pHead, int x, int y> { int res = 0; if <pHead == NULL> { res = -1; printf<"pHead==NULL inrtLink<> err:%d\n",res>; return res; } Link *pNew = <Link *>malloc<sizeof<Link>>; pNew->data = y; Link *pPre = pHead; Link *pCurr = pHead->next; int flag = 0; while <pCurr != NULL> { if <pCurr->data == x> { flag = 1; break; } pPre = pPre->next; pCurr = pCurr->next; } if <flag == 0> { res = -2; printf<"原链表中没有%d\n",x>; return res; } pNew->next = pCurr; pPre->next = pNew; return res; } |
//删除链表中节点 删除data=x的节点 |
int deleLink<Link *pHead, int x> { int res = 0; if <pHead == NULL> { res = -1; printf<"pHead==NULL deleLink<> error:%d\n",res>; return res; } 小牙刷计算机配件Link *pPre = pHead; Link *pCurr = pHead->next; int flag = 0; while <pCurr != NULL> { if <pCurr->data == x> { flag = 1; break; } pPre = pPre->next; pCurr = pCurr->next; } if <flag == 0> { res = -2; printf<"原链表中没有%d\n", x>; return res; } pPre->next = pCurr->next; return res; } |
//反转链表 |
int revertLink<Link *pHead> { int res = 0; if <pHead == NULL||pHead->next==NULL||pHead->next->next==NULL> { res = -1; printf<"pHead==NULL revertLink<> error:%d\n",res>; return res; } Link *pPre = pHead->next; Link *pCurr = pHead->next->next; Link *q = NULL; while <pCurr != NULL> { q = pCurr->next; pCurr->next = pPre; pPre = pCurr; pCurr = q; } pHead->next->next = NULL; pHead->next = pPre; return res; } |
//链表排序 //再创建一个空链表 从原链表中找到最大值的那个节点 然后往空链表里添加 |
int sortLink<Link *pHead,Link **pHead1> { int res = 0; Link *pNewHead = <Link *>malloc<sizeof<Link>>; pNewHead->data = 0; Link *pNewTail = pNewHead; if <pHead == NULL> { res = -1; printf<"pHead==NULL sortLink<> erro:%d\n", res>; return res; } //先从原链表里找出最大值的那个节点 start: { Link *pMax = pHead->next; Link *pPre = pHead; Link *pCurr = pMax->next; while <pCurr != NULL> { if <pCurr->data > pMax->data> { pPre = pMax; pMax = pCurr; } pCurr = pCurr->next; } pPre->next = pMax->next;// 让最大的那个节点脱离原链表 if <pNewHead->next == NULL> { pNewHead->next = pMax; pNewTail = pMax; pMax->next = NULL; } el { pNewTail->next = pMax; pNewTail = pMax; pMax->next = NULL; } if <pHead->next == NULL> //所有的节点都脱离了原链表 { goto sortEnd; } goto start; } sortEnd: *pHead1 = pNewHead; return res; } int destoryLink<Link **pHead> { int res = 0; if <pHead == NULL> { res = -1; printf<"pHead==NULL 链表为空 释放内存失败 erro:%d\n",res>; return res; } 学生简历模板Link *pt = *pHead; while <pt != NULL> { Link *tmp = pt->next; free<pt>; pt =tmp; } *pHead = NULL; return res; } |
//测试案例 |
void main<> { Link *pHead = NULL; int res; res = creatLink<&pHead>; if <res != 0> { printf<"function creatLink<> err:%d\n",res>; goto End; } res = printLink<pHead>; if <res != 0> { printf<"function printLink<> err:%d\n", res>; goto End; } printf<"**************** 在3的前面插入4 **************************\n">; res = inrtLink<pHead,3,4>; if <res != 0> { printf<"function intrLink<> err:%d\n", res>; goto End; } res = printLink<pHead>; if <res != 0> { printf<"function printLink<> err:%d\n", res>; goto End; } printf<"**************** 删除data=4的节点 **************************\n">; res = deleLink<pHead,4>; if <res != 0> { printf<"function deleLink<> err:%d\n", res>; goto End; } res = printLink<pHead>; if <res != 0> { printf<"function printLink<> err:%d\n", res>; goto End; } printf<"**************** 逆转链表 **************************\n">; res = revertLink<pHead>; if <res != 0> { printf<"function revertLink<> err:%d\n", res>; goto End; } res = printLink<pHead>; if <res != 0> { printf<"function printLink<> err:%d\n", res>; goto End; } printf<"**************** 从大到小排序链表 **************************\n">; Link *pHead1 = NULL; res = sortLink<pHead,&pHead1>; if <res != 0> { printf<"function sortLink<> err:%d\n", res>; goto End; } res = printLink<pHead1>; if <res != 0> { printf<"function printLink<> err:%d\n", res>; goto End; } End: if <pHead != NULL> { res = destoryLink<&pHead>; if <res != 0> { printf<"function destoryLink<> is error:%d\n",res>; return; } } system<"pau">; } |
减肥吃什么肉 |
本文发布于:2023-05-12 21:17:38,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/fan/89/889660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |