第1个回答 2010-01-12
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N_NODE 15 /*单链表中的结点个数*/
typedef struct node{
int data;
struct node *next;
} node;
node *creat(int n) /*创建一个有n个结点的单链表*/
{
int i;
node *s,*p,*h=NULL;
for (i = 0; i<n; i++) {
p=(node *)malloc(sizeof(node));
p->next=NULL;
if (!h) h=p;
else s->next=p;
s=p;
p->data=rand()%100; /*链表结点的数据域用随机数填充,可以在此修改为从键盘输入*/
}
return h;
}
node *maxn(node *h) /*返回最大值结点*/
{
node *ma=h;
h=h->next;
while (h)
{
if (ma->data<h->data) {
ma=h;
}
h=h->next;
}
return ma;
}
node *del(node *h,node *todel) /*删除链表h中的todel结点*/
{
node *p=h;
if (h==todel) {
p=h->next;
free(todel);
}
else p->next=del(h->next,todel);
return p;
}
void prt(node *h) /*输出链表*/
{
while (h)
{
printf("%d ",h->data);
h=h->next;
}
putchar('\n');
}
void fun(node *h) /*完成要求的过程,奇数就删除,否则就调换位置*/
{
node *max=maxn(h);
int t;
printf("原始单链表为:");
prt(h);
printf("最大值:%d\n",max->data);
if (max&&max->data%2) {
h=del(h,max);
}
else if (max&&max->data%2==0) {
t=max->data;
max->data=max->next->data;
max->next->data=t;
}
printf("处理后的结果是:");
prt(h);
}
int main(void)
{
node *ht=NULL;
srand((unsigned int )time(NULL));
ht=creat(N_NODE);
fun(ht);
return 0;
}
//---------------------------------------------------------------------------本回答被提问者采纳
第2个回答 2010-01-12
#include<stdio.h>
#include<stdlib.h>
struct node
{
int x;
node *next;
}*link,*max,*start,t;
node* creatlinks(int n)
{
node *head=NULL,*first=NULL,*ptr=NULL;
head=first=(node*)malloc(sizeof(node));
scanf("%d",&first->x);
for ( int i = 1; i < n; i++)
{
ptr = (node*)malloc(sizeof(node));
scanf("%d",&ptr->x);
first->next = ptr;
first = ptr;
ptr = NULL;
}
first->next = NULL;
return head;
}
int main()
{
int n;
t.x=0;
max=&t;
scanf("%d",&n);
start=link=creatlinks(n);
while(link!=NULL)
{
if(link->x>max->x)
max=link;
link=link->next;
}
printf("max=%d\n",max->x);
if(max->x&1)
{
max->next=max->next->next;
}
else
{
int temp;
temp=max->x;
max->x=max->next->x;
max->next->x=temp;
}
link=start;
while(link!=NULL)
{
printf("%d\n",link->x);
link=link->next;
}
}
//
input:
5
132
45189
51
5163
656
output:
max=45189
132
45189
5163
656
input:
6
4165
15
54154
655
156
85
output:
max=54154
4165
15
655
54154
156
85
第3个回答 2010-01-12
我不是来挣分的,我只想提醒楼上诸位:悬赏分150,追加分100.诸位难道非常想要这个分数么?