单链表进行奇偶整数位置调整

92005:单链表进行奇偶整数位置调整

Time/Memory Limit:1000 MS/32768 K 
Submitted: 107  Accepted: 61

 Problem Description

要求以非空单链表完成以下操作:
输入n(0<n<=50)个整数,每两个数之间以空格分隔,现要求:
1. 先输出其中的奇数,并按输入的相对顺序排列;
2. 然后输出其中的偶数,并按输入的相对顺序排列。
该题必须用单链表完成,否则0分!!!

 Input

第一行为一个整数m,表示下面有m组测试数据,每组包括两行:
每组测试数据的第一行为一个整数n(0<n<=50),表示表长;
每组测试数据的第二行有n个整数,表示表的各元素。

 Output

每组测试数据的输出均占两行:
第一行输出所有奇数,每两个数之间以一个空格分隔;
第二行输出所有偶数,每两个数之间以一个空格分隔。

 Sample Input

2
10
4 7 3 13 11 12 0 47 34 98
8
1 65 3 5 2 1 4 5

 Sample Output

7 3 13 11 47
4 12 0 34 98
1 65 3 5 1 5
2 4

 Hints

注意从测试数据的长度和奇偶性两方面考虑输出的特殊情况。

 Author

txq

 Recommend

zh









#include<iostream>
using namespace std;
struct Node{int data;Node*next;};
int main()
{
int n1,n,n2,t,c,a[100],i;
Node*first,*s,*p,*q,*r;
cin>>t;
while(t--)
{
cin>>n;
first=new Node;first->next=NULL;r=first;
for(i=0;i<n;i++)
{
cin>>a[i];
s=new Node;s->data=a[i];s->next=r->next;r->next=s;r=s;}//建立单链表
n1=0;p=first->next;
while(p)
{
if(p->data%2!=0)
{
n1++;//计算奇数段长度
}
p=p->next;
}
c=0;p=first->next;
while(p)
{
if(p->data%2!=0)
{
c++;//设立标记数字控制输出
if(c<n1){
cout<<p->data<<" ";
}
else if(c==n1){
cout<<p->data;
break;
}
}
p=p->next;
}
cout<<endl;
n2=n-n1;//计算偶数段长度
p=first->next;c=0;
while(p)
{
if(p->data%2==0)
{
c++;
if(c<n2)
{
cout<<p->data<<" ";//输出偶数段
}
else if(c==n2)
{
cout<<p->data;break;
}
}
p=p->next;
}
cout<<endl;
while(first)
{q=first;first=first->next;delete q;}//清理垃圾
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言的代码实现: ```c #include <stdio.h> #include <stdlib.h> //定义链表结点结构体 struct ListNode { int val; struct ListNode *next; }; struct ListNode* createList(int arr[], int n) { struct ListNode *head, *p, *q; head = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next = NULL; p = head; for (int i = 0; i < n; i++) { q = (struct ListNode*)malloc(sizeof(struct ListNode)); q->val = arr[i]; q->next = NULL; p->next = q; p = q; } return head; } void printList(struct ListNode* head) { struct ListNode* p = head->next; while (p) { printf("%d ", p->val); p = p->next; } printf("\n"); } struct ListNode* splitList(struct ListNode* head) { struct ListNode *oddHead, *evenHead, *p, *q; oddHead = (struct ListNode*)malloc(sizeof(struct ListNode)); evenHead = (struct ListNode*)malloc(sizeof(struct ListNode)); oddHead->next = NULL; evenHead->next = NULL; p = oddHead; q = evenHead; struct ListNode* cur = head->next; int count = 1; while (cur) { if (count % 2 == 1) { p->next = cur; p = cur; } else { q->next = cur; q = cur; } cur = cur->next; count++; } p->next = NULL; q->next = NULL; head->next = oddHead->next; return evenHead; } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = sizeof(arr) / sizeof(int); struct ListNode* head = createList(arr, n); printf("Original list: "); printList(head); struct ListNode* evenHead = splitList(head); printf("Odd index list: "); printList(head); printf("Even index list: "); printList(evenHead); return 0; } ``` 这个算法的实现思路是:遍历原链表,将数序号的结点连接到一个新的链表上,将数序号的结点连接到另一个新的链表上。最后,将原链表的表头指向数序号链表的表头,返回数序号链表的表头。 同样的,这个算法需要手动创建两个新的链表,分别保存数序号结点和数序号结点。最后,由于原链表的表头改变了,需要将原链表的表头指向数序号链表的表头。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值