链表逆序排序
来源:互联网 发布:印象主题女装淘宝店 编辑:程序博客网 时间:2024/05/16 18:41
void reverse(){ if(head->next==NULL||head==NULL||head->next->next==NULL) return; node* N1=head->next; node* pre=head; node* cur=head->next; while(cur) { node* saved=cur->next; cur->next=pre; pre=cur; cur=saved; } head->next=pre; if(N1) N1->next=NULL;}
或者:
#include <iostream>#include<list>using namespace std;template <class T>struct Node{ T data; Node<T>* next;};template <class T>//尾插建立循环单链表Node<T>* creat_back( Node<T> * first,int len){ Node<T>* r=first; for( int i=0; i<len; i++) { int data; cin>>data; Node<T>* pnew=new Node<T>; pnew->data=data; pnew->next=r->next; r->next=pnew; r=pnew; } r->next=NULL; return first;}template <class T>//输出链表循环单链表void show(Node<T>* first){ Node<T>* p=first->next; while(p!=NULL) { cout<<p->data<<' '; p=p->next; } cout<<endl<<endl;}template <class T>//单链表逆置Node<T>* Reverse(Node<T>* first){ Node<T>* cur=first->next; Node<T>* pre=NULL; while(cur) { Node<T>* r=cur->next; cur->next=pre; pre=cur; cur=r; } first->next=pre; return first;}template<class T>//利用头插法进行逆置Node<T>* Reverse_front(Node<T>* first){ Node<T>* p=first->next; first->next=NULL; while(p) { Node<T>* r=p->next; p->next=first->next; first->next=p; p=r; } return first;}int main(){ Node<int >* first=new Node<int>; first=creat_back(first,5); show(first); first=Reverse_front(first); show(first); first=Reverse(first); show(first); return 0;}
阅读全文
0 0
- 链表逆序排序
- 链表的排序和逆序
- 链表的逆序,排序等
- 链表的实现以及合并,排序,逆序,等
- iOS 算法面试题(冒泡排序、选择排序、链表逆序)~demo
- 链表的逆序
- 链表逆序
- 链表逆序
- 链表逆序小结
- 逆序链表
- 链表逆序 :华硕
- 链表逆序
- 链表逆序
- 链表逆序
- 链表逆序
- 实现链表逆序
- 链表逆序
- 链表逆序
- Valid signing identity not found解决办法(原有IDP私钥丢失)及Certificate、App ID、Devices、Provisioning Profiles之间区别
- POJ
- 创建版本库
- LeetCode#13-Roman to Integer
- A. Greed
- 链表逆序排序
- ubuntu for ROS安装PCL1.8
- en en
- eeee
- Eclipse中使用cmd,解决乱码。
- ext 实现一个鼠标滑过弹出 子菜单面板
- 线程池ExecutorService 中并发数的(引入信号量Semaphore)控制执行
- 项目
- VC++6.0多项目联调断点设置方法