实现将线性表奇数和偶数分开。

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

typedef struct node
{
  int data;
  struct node *next;
}node;

void create_list(node **head)
{
  int a;
  node *p;
  *head=(node *)malloc(sizeof(node));
  (*head)->next=NULL;
  printf("Input numbers,end by -1:");
  scanf("%d",&a);
  while(a!=-1)
  {
    p=(node *)malloc(sizeof(node));
 p->data=a;
 p->next=(*head)->next;
 (*head)->next=p;
 scanf("%d",&a);
  }
}

void print_list(node *head)
{
  node *p;
  p=head->next;
  while(p!=NULL)
  {
    printf("%d ",p->data);
 p=p->next;
  }
}

void depart(node *head,node **even,node **odd)
{
  node *p,*q;
  *even=(node *)malloc(sizeof(node));
  *odd=(node *)malloc(sizeof(node));
  (*even)->next=NULL;
  (*odd)->next=NULL;
 
  p=head->next;
  while(p!=NULL)
  {
    q=p;
 p=p->next;
 if(q->data%2==0)
 {
   q->next=(*even)->next;
   (*even)->next=q;
 }
 else
 {
   q->next=(*odd)->next;
   (*odd)->next=q;
 }
  }
}

int main()
{
  node *head,*odd,*even;
  create_list(&head);
  print_list(head);
  depart(head,&even,&odd);
  printf("/neven:");
  print_list(even);
  printf("/nodd:");
  print_list(odd);
  printf("/n");
 
  return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值