溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

小代碼 反轉(zhuǎn)單鏈表和 反轉(zhuǎn)前K個(gè)節(jié)點(diǎn)的單鏈表

發(fā)布時(shí)間:2020-06-28 16:39:08 來源:網(wǎng)絡(luò) 閱讀:380 作者:wzdouban 欄目:編程語言
 /********************
 
    WZ  ASUST  2016
代碼與反思
 
 ********************/
#include<iostream>
#include<assert.h>
#include<vector>//容器--類模板
#include<stdlib.h>//利用隨機(jī)值
#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);  
}
/*********************
博客 作為文件中轉(zhuǎn)站 與記憶的留存
這里也許有錯(cuò)誤 大多的程序僅僅實(shí)現(xiàn)基本功能
發(fā)表的時(shí)候 有些是知道錯(cuò)誤的
程序員的樂趣在于自己能寫一些代碼得到反饋
部分錯(cuò)誤留下了 在后續(xù)的博客引用中可以注明
鏈表那節(jié)  函數(shù)名與功能不匹配  反轉(zhuǎn)是錯(cuò)誤的 上面已經(jīng)給出正確解
對(duì)于前關(guān)于數(shù)組匹配  也是錯(cuò)的 項(xiàng)目運(yùn)用的時(shí)候已更改
也就是測(cè)試數(shù)據(jù)很重要

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


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI