剑指offer--1.删除链表中某个节点 2.从尾到头打印链表
来源:互联网 发布:李荣浩2017新专辑知乎 编辑:程序博客网 时间:2024/06/04 00:53
#include<stdio.h>#include<assert.h>#include<malloc.h>struct ListNode{int data;struct ListNode* next;};
//创建链表void CreatListNode(struct ListNode** phead, int data){struct ListNode* pnew=malloc(sizeof(struct ListNode));pnew->data=data;pnew->next=NULL;if(*phead==NULL) //空链表*phead=pnew; //phead是一个指向指针的指针,每次保存头节点的地址else{struct ListNode* cur=*phead;while(cur->next!=NULL)//找到最后一个节点cur=cur->next;cur->next=pnew;}}
//回收链表void Destory(struct ListNode** phead){assert(phead);struct ListNode* cur=*phead;struct ListNode* del=NULL;while(cur){del=cur;cur=cur->next;free(del);}}
//从头到尾打印链表void Display(struct ListNode** phead){assert(phead);struct ListNode* cur=*phead;while(cur){printf("%d->",cur->data);cur=cur->next;}printf("NULL\n");}//删除某个含某值的节点void RemoveNode(struct ListNode** phead,int data){assert(phead);struct ListNode* cur=*phead;struct ListNode* prev=*phead;if(cur->data==data){*phead=cur->next;prev->next=NULL;free(prev);return;}while(cur){if(cur->data==data){prev->next=cur->next; free(cur);cur->next=NULL;return;}else{prev=cur;cur=cur->next;}}}//递归的思想 从尾到头打印链表
//也可以用C++写调用库里的stackvoid printf_prevtoend(struct ListNode* cur){if(cur==NULL){return;}elseprintf_prevtoend(cur->next);printf("%d->",cur->data);}void PrintList_prevtoend(struct ListNode** phead){assert(phead);struct ListNode* cur=*phead;printf_prevtoend(cur);printf("NULL\n");}int main(){ struct ListNode* phead=NULL; CreatListNode(&phead,1); CreatListNode(&phead,2); CreatListNode(&phead,3); CreatListNode(&phead,4); CreatListNode(&phead,5); Display(&phead); //RemoveNode(&phead,1); PrintList_prevtoend(&phead); //Display(&phead); Destory(&phead);}
1 0
- 剑指offer--1.删除链表中某个节点 2.从尾到头打印链表
- 剑指offer-输入一个链表,从尾到头打印链表每个节点的值
- 剑指offer:5-从尾到头打印链表
- [剑指Offer]7.从尾到头打印链表
- 剑指offer:从尾到头打印链表
- 从尾到头打印链表--《剑指offer》
- 剑指Offer:从尾到头打印链表
- 【剑指offer】从尾到头打印链表
- 【剑指offer】Q5:从尾到头打印链表
- 剑指Offer之从尾到头打印链表
- 剑指offer系列源码-从尾到头打印链表
- 剑指offer 05: 从尾到头打印链表
- 剑指offer--从尾到头打印链表
- 剑指offer-5 从尾到头打印链表
- 剑指Offer之 - 从尾到头打印链表
- 剑指offer 5 -从尾到头打印链表
- 剑指offer: 从尾到头打印链表(链表)
- 《剑指Offer》面试题:从尾到头打印链表
- TCP/IP协议三次握手与四次握手流程解析
- 机器学习中的训练集、验证集和测试集
- 蓝桥杯取球博弈
- 配置对象方法传参
- php常见知识点
- 剑指offer--1.删除链表中某个节点 2.从尾到头打印链表
- 安卓推送
- Intellij license server
- 关于SHA1碰撞——比较两个binary的不同之处
- 欢迎使用CSDN-markdown编辑器
- codeforce 510 C 拓扑排序
- 括号配对问题
- NumPy学习笔记(2)--Array数组和矩阵基本运算
- Android微信智能心跳方案