逆转.cpp

来自「发射系下飞行器九参数计算」· C++ 代码 · 共 59 行

CPP
59
字号
#include"iostream.h"

struct node
{  int d;
   struct node *next;
};

void  main()
{
   int x,n=0;
   struct node *head,*p,*q,*r;
   p=new(struct node);
   head=p;
   head->d=n;
   head->next=NULL;

   cin>>x;                 //建立链表
   while(x!=0)
   {   p=new(struct node) ;
       p->d=x;
	   p->next=NULL;
	   if(head->next==NULL)  head->next=p;
	   else q->next=p;
       q=p;
	   ++n;
	   cin>>x;
	 
   }
   
   q->next=head;
   head->d=n;
   cout<<"Before reversing:";
   p=head->next ;          //显示链表
   while(p!=head)
   {  cout<<"  "<<p->d;
      p=p->next;
   }
   cout<<endl;

   p=head;               //逆转链表
   q=p->next;
   r=q->next;
   while(q!=head )
   {   q->next=p;
       p=q;
	   q=r;
	   r=r->next;
	  
   }
   head->next =p;

   cout<<"After reversing: ";   //显示逆转后的链表
   p=head->next ;
   while(p!=head)
   {   cout<<"  "<<p->d;
      p=p->next;
   }
   cout<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?