在线等~急求写个C语言数据结构程序!!!

设有一个由整数组成的无序单链表,编写一个能完成下列功能的算法:(1)找出最大值结点,且打印该值;(2)若该数值是偶数,则将其与直接后继结点的数值交换;(3)若该数值是奇数,则将其直接后继结点删除。
请将算法和主测试函数都写出来!要求有测试数据和测试结果数据。
谢谢!不胜感激!
算法实现了我将另送100分以示感谢!

分数不重要啊,只是通过分数谢谢大家啊!

写了一个,今天百度被黑了,总是打不开。现在传给你看看那哈。
代码都有注释的,相信你能看明白。后边付有测试数据及测试结果。
有问题Hi我哈:)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

/* 链表节点结构体 */
typedef struct _Node {
int data;
struct _Node *next;
} Node;

/* 创建链表 */
void create_list(Node *head)
{
Node *newNode = NULL;
Node *tempNode = NULL;
int input = 0;

if(NULL == head)
{
printf("参数错误,程序结束!");
exit(0);
}

tempNode = head;

printf("请输入需要创建的链表值(输入0结束)\n");

scanf("%d", &input);

while(input != 0)
{
newNode = malloc(sizeof(Node));
if (newNode == NULL)
{
printf("参数错误,程序结束!");
exit(0);
}

newNode->data = input;
newNode->next = NULL;
tempNode->next = newNode;
tempNode = newNode;

scanf("%d", &input);
}
}

/* 找出最大节点 */
void max_node_search(Node *head)
{
int max_value = 0; /* 保存节点中的最大值 */
Node *tempNode = NULL; /* 节点指针,用来遍历链表 */

if(NULL == head)
{
printf("参数错误,程序结束!");
exit(0);
}

tempNode = head->next; /* 当前节点初始化 */
max_value = tempNode->data; /* 保存当前节点的值 */

/* 顺序遍历链表,找到最大值的节点 */
while(tempNode)
{
/* 最大值与当前节点的值作比较 */
if(max_value < tempNode->data)
{
max_value = tempNode->data; /* 保存最大值 */
}

tempNode = tempNode->next; /* 当前节点变更 */
}

printf("最大节点的值: %d\n", max_value); /* 输出最大节点的值 */
}

/* 偶数、奇数查找,如果当前节点值是偶数,则将其与后继节点交换,
如果当前节点为奇数,则删除其后继节点
*/
void list_make_up(Node *head)
{
Node *tempNode = NULL; /* 节点指针,用来遍历链表 */
Node *delNode = NULL; /* 被删除节点的指针 */
int temp = 0;

if(NULL == head)
{
printf("参数错误,程序结束!");
exit(0);
}

tempNode = head->next;

/* 顺序遍历链表 */
while(tempNode)
{
/* 如果已经是最后一个节点,则退出 */
if (tempNode->next == NULL)
break;

if (tempNode->data%2 == 0) /* 偶数判断 */
{
/* 将当前节点与后继节点的值交换 */
temp = tempNode->data;
tempNode->data = tempNode->next->data;
tempNode->next->data = temp;

tempNode = tempNode->next;
continue; /* 继续下一个节点 */
}

if (tempNode->data%2 != 0) /* 奇数判断 */
{
/* 将后继节点删除 */
delNode = tempNode->next;
tempNode->next = delNode->next;
free(delNode); /* 节点被删除,占用内存被释放 */
delNode = NULL;

tempNode = tempNode->next;
continue; /* 继续下一个节点 */
}
}

/* 打印整理后的链表值 */
tempNode = head->next;
printf("整理后的链表值: ");
/* 遍历整个链表,输出节点值 */
while(tempNode)
{
printf("%d ", tempNode->data);
tempNode = tempNode->next;
}

printf("\n");
}

/* 测试函数main */
int main()
{
Node head;
Node *tempNode = NULL;
Node *delNode = NULL;

head.data = 0;
head.next = NULL;

create_list(&head); /* 创建链表 */
max_node_search(&head); /* 查找最大值 */
list_make_up(&head); /* 整理链表 */

/* 释放内存 */
tempNode = head.next;
while(tempNode)
{
delNode = tempNode;
tempNode = delNode->next;
free(delNode);
}

return 0;
}

测试数据

12 4 8 17 76
最大节点值:76
链表:24 8 17 76 12s


12 7 3 8 5
最大节点值:12
链表: 7 3 8 5 12


7 12 34 17 19
最大节点值:34
链表:7 17 19 34
温馨提示:内容为网友见解,仅供参考
第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.诸位难道非常想要这个分数么?
相似回答