初入门数据结构,在编一段代码时遇到一些问题,我一直想不明白那个error错哪儿了,求大神指教(程序要求是:1、设有一个由正整数组成的无序单链表,编写能够完成下列功能的算法:
(1)找出最小值结点,且打印该数值。
(2)若该数值是奇数,则将其与直接后继结点的数值交换。
(3)若该数值是偶数时,则将其直接后继结点删除。
要求:判断奇数或者是偶数需另用函数实现(函数调用)。
存储结构:链表。
)
#include
#include
typedefstructListNode
{
intdata;
structListNode*next;
}LNode;
intmain()
{
LNode*h;
intj;
h=Createlist(10);
j=Locate_min(LNode*h,10);
Judge_and_react(j);
return0;
}
LNode*Creatlist(intn)/*该函数的作用为:创建一个以h为头结点的,除去头结点共有n个结点的链表LNode*/
{
LNode*head;
LNode*p,*pre;
inti;
head=(LNode*)malloc(sizeof(LNode));
head->next=NULL;
pre=head;
for(i=1;inext=NULL;
}
}
intLocate_min(LNode*h)/*该函数的作用为:找出链表LNode各结点数值中的最小值,并返回*/
{
LNode*p;
intmin;
p=h->next;
min=p->data;
while(p)
{
p=p->next;
if(p->datadata;
}
returnmin;
}
voidJudge_and_react(intm)/*该函数判断m的奇偶性并作出相关反应*/
{
if(m%2==0)
Delete();
else
Exchange();
}
voidDelete(LNode*h,intx)/*该函数的作用为:找到链表LNode中结点数值为x的结点并将其后继结点删除*/
{
LNode*p,*pre;
p=h->next;
while(p)
{
if(p->data==x)
{
pre=p->next;
p->next=pre->next;
free(pre);
}
else
p=p->next;
}
printf("Deletedone,thelistis:");
Printflist();
}
voidExchange(LNode*h,intx)/*该函数的作用为:找到链表LNode中结点数值为x的结点并将其与其后继结点交换数据*/
{
LNode*p,*pre;
inty;
p=h->next;
pre=p->next;
while(p)
{
if(p->data=x)
{
y=pre->next;
p->data=y;
pre->data=x;
}
printf("Exchangedone,thelistis:");
Printflist();
}
}
voidPrintflist(LNode*h)/*该函数作用为:依次输出链表LNode的各结点数值*/
{
LNode*p;
p=h->next;
while(p)
{
printf("%d\t",p->data);
p=p->next;
}
}
分 -->
|