小代码 反转单链表和 反转前K个节点的单链表
/********************WZASUST2016
代码与反思********************/#include<iostream>#include<assert.h>#include<vector>//容器--类模板#include<stdlib.h>//利用随机值#include<time.h>usingnamespacestd;#defineN1000#defineK100typedefstructnode{intx;node*next;public:node():x(1),next(NULL){}node(inta):x(a),next(NULL){}}node;intxx[]={0,7,6,5,4,3,2,1,0};inti=0;voidlinkcreat(node*head){if(head==NULL){head=newnode;}head->x=xx[i++];while(i<8){node*add=newnode(xx[i++]);add->next=head->next;head->next=add;}}voidshow(node*head){node*p=head;while(p){cout<<p->x<<"";p=p->next;}cout<<endl;}voidV(node*&head){node*newhead=head;node*p=head;node*q=head;node*t=NULL;while(p->next!=NULL){q=p;p=p->next;q->next=t;t=q;}head=p;p->next=q;}voidV(node*&head,intk){node*newhead=head;node*p=head;node*q=head;node*t=NULL;while(k--){q=p;p=p->next;q->next=t;t=q;}cout<<q->next->x<<endl;head=q;newhead->next=p;}intmain(){node*head=newnode(1);linkcreat(head);show(head);V(head,4);show(head);}/*********************博客作为文件中转站与记忆的留存这里也许有错误大多的程序仅仅实现基本功能发表的时候有些是知道错误的程序员的乐趣在于自己能写一些代码得到反馈部分错误留下了在后续的博客引用中可以注明链表那节函数名与功能不匹配反转是错误的上面已经给出正确解对于前关于数组匹配也是错的项目运用的时候已更改也就是测试数据很重要*********************/
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。