降序数排列及变形

//生成动态链表,先将整数的各位数按降序数排列,再将各位数的元素按最大、最小、次最大、次最小等
//间隔排序,最后计算出输入整数的各位数之和
#include <iostream>
using namespace std;

class NUM{
public:
 int n;
 NUM *next;
};
NUM *creat(){
 NUM *head=NULL,*tail=NULL,*temp=NULL;
 int a;
 cout<<"请输入任意一个整数:";
 cin>>a; 
 head=new NUM;
 head->n=a%10;
 a=a/10;
 head->next=NULL;
 tail=head;
 while(a){
  temp=new NUM;
  temp->n=a%10;
  a=a/10;
  temp->next=NULL;
  tail->next=temp;
  tail=temp;
 }
 return head;
}
void show(NUM *head){
 cout<<endl<<"输入的数字如下:"<<endl;
 NUM *p;
 p = head;
 while (p)
 {
  cout<<p->n<<"\t";
  p=p->next;
 }
 cout<<endl;
}
int Sum(NUM *head){
 int total=0;
 cout<<endl<<"各位数数字之和为:";
 NUM *p;
 p=head;
 while (p)
 {
  total+=p->n;
  p=p->next;
 }
 return total;
}
NUM* Paixu(NUM *head)//这里的*&head是关键.如果不用这种形式,这应该采用NUM *Paixu(NUM *head)形式,从大到小
{
 if(head==NULL)return NULL;
 NUM *rest,*p,*pGuard;
 rest=head->next;//旧链表:从第二个结点起
 head->next=NULL;//新链表:单个结点(原首结点)
 while (rest!=NULL)//循环直至旧链表为空
 {
  p=rest;
  rest=rest->next;//从旧链表中卸下当前首结点
  if (p->n >= head->n)//插入到新链表的适当位置
  {
   p->next=head;
   head=p;
  }
  else
  {
   pGuard=head;
   while (pGuard->next!=NULL && pGuard->next->n > p->n)
    pGuard=pGuard->next;
   p->next=pGuard->next;
   pGuard->next=p;
  }
 }
 return head;
}
NUM* Paixu1(NUM *head)//从小到大
{
 if(head==NULL)return NULL;
 NUM *rest,*p,*pGuard;
 rest=head->next;//旧链表:从第二个结点起
 head->next=NULL;//新链表:单个结点(原首结点)
 while (rest!=NULL)//循环直至旧链表为空
 {
  p=rest;
  rest=rest->next;//从旧链表中卸下当前首结点
  if (p->n <= head->n)//插入到新链表的适当位置
  {
   p->next=head;
   head=p;
  }
  else
  {
   pGuard=head;
   while (pGuard->next!=NULL && pGuard->next->n < p->n)
    pGuard=pGuard->next;
   p->next=pGuard->next;
   pGuard->next=p;
  }
 }
 return head;
}
NUM* Insert(NUM *head){
 if (head==NULL)return NULL;
 int count=0,count1,count2;
 NUM *p,*head1,*head2;
 p=head;
 head1=head;
 head2=head;
 while(p){count++;p=p->next;}
 //cout<<"count:"<<count<<endl;
 count1=(count+1)/2;
 count2=count1-1;
 //cout<<"count1:"<<count1<<endl;
 //cout<<"count2:"<<count2<<endl;
 while (count1>0)//分段的后半段
 {
  head1=head1->next;
  count1--;
 }
 head1=Paixu1(head1);//将后半段从小到大排列
 //show(head1);
 //cout<<head1->n<<endl;
 while (count2>0)//分段的前半段
 {
  head2=head2->next;
  count2--;
 }
 head2->next=NULL;
 //show(head);//这里的head相当于指向前半段链表
 //cout<<head2->n<<endl;
 NUM *temp,*temp1,*head3;
 head3=head;
 temp=head;
 temp1=head1;
 while (temp1!=NULL)
 {
  temp=temp->next;
  temp1=temp1->next;
  head1->next=head3->next;
  head3->next=head1;
  head1=temp1;
  head3=temp;
 }/**/
 //show(head);
 return head;
}
int main(){
 NUM *head=NULL;
 head=creat();
 show(head);
 head=Paixu(head);
 show(head);
 int sum;
 sum=Sum(head);
 cout<<sum<<endl;
 head=Insert(head);
 show(head);
 return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值