温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

小代码 反转单链表和 反转前K个节点的单链表

发布时间:2020-06-28 16:39:08 来源:网络 阅读:373 作者:wzdouban 栏目:编程语言
 /********************
 
    WZ  ASUST  2016
代码与反思
 
 ********************/
#include<iostream>
#include<assert.h>
#include<vector>//容器--类模板
#include<stdlib.h>//利用随机值
#include<time.h>
using namespace std;
 
#define N 1000 
#define K 100
 typedef struct node
{
    int x;
     
    node *next;
public:
    node():x(1) ,next(NULL){}
    node(int a):x(a), next(NULL){}
}node;
int xx[]={0,7,6,5,4,3,2,1,0};int i=0; 
void  linkcreat(node*head)
{
if(head==NULL)
{
  head=new node;
}
head->x=xx[i++];
while(i<8)
{
node *add=new node(xx[i++]);
add->next=head->next;
head->next=add;
}
}
void show(node *head)
{
node *p=head;
 while(p)
{
cout<<p->x<<" ";
p=p->next;
}
cout<<endl;
} 
 
void V(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;
 
}
void V(node *&head,int k)  
{ 
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;
  
}


int main()
{
	 node *head=new node(1);
      linkcreat(head); 
         show(head);
       V(head,4);
     show(head);  
}
/*********************
博客 作为文件中转站 与记忆的留存
这里也许有错误 大多的程序仅仅实现基本功能
发表的时候 有些是知道错误的
程序员的乐趣在于自己能写一些代码得到反馈
部分错误留下了 在后续的博客引用中可以注明
链表那节  函数名与功能不匹配  反转是错误的 上面已经给出正确解
对于前关于数组匹配  也是错的 项目运用的时候已更改
也就是测试数据很重要

*********************/


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI