使用链表进行奇偶分排 c语言

题目内容:

  使用单向链构建一个有n个节点,输入正整数序列并输出序列,n由键盘输入。然后将偶数节点依次移动到 所有的奇数后面,形成一个前面为奇正数序列,后面为偶整数序列的新序列,最后输出这个序列

#include <stdio.h>
#include <stdlib.h>

typedef struct i
{
    int n;
    struct i *next;
    struct i *past;
}F;
int pd(F *p)
{
    for(;p!=NULL;p=p->next)
    {
        if((p->n)%2==1)
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
   F *ptemp,*head,*pnew,*pstore,*pw,*puse;
   head=(F*)malloc(sizeof(F));
   pnew=(F*)malloc(sizeof(F));
   head->next=pnew;
   head->past=NULL;
   pnew->past=head;
   head->n=1;
   pnew->n=2;
   int num,i;
   scanf("%d",&num);
   ptemp=pnew;
   for(i=2;i<num;i++)
   {
       pnew=(F*)malloc(sizeof(F));
       ptemp->next=pnew;
       pnew->past=ptemp;
       ptemp=ptemp->next;
       ptemp->n=i+1;
   }
   pw=ptemp;
   ptemp->next=NULL;

   for(ptemp=head;ptemp!=NULL;ptemp=ptemp->next)
   {
       printf(" %d",ptemp->n);
   }
   printf("\n");

   for(ptemp=pw;ptemp!=NULL;ptemp=ptemp->past)
   {
       if((ptemp->n)%2==0)
       {
puse=ptemp;
           while(pd(puse)==0)
           {

               int t;
t=ptemp->n;
ptemp->n=ptemp->next->n;
ptemp->next->n=t;
ptemp=ptemp->next;
puse=ptemp;
           }
       }
   }
      for(ptemp=head;ptemp!=NULL;ptemp=ptemp->next)
   {
       printf(" %d",ptemp->n);
   }
   printf("\n");
    return 0;
}

单向的整不会了,整个双向的哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值