2 wearetheworld1234 wearetheworld1234 于 2017.01.15 11:33 提问

高一凡 数据结构 题目不理解,求解

/*
void print(ELemType c) //显示错误的地方!
{
printf("%d",c);

}
*/
//头文件省略

define OK 1;

define ERROR 0;

typedef int Status;

typedef int ElemType;

struct LNode

{

ElemType data;

LNode * next;

};

typedef LNode * LinkList;

void CreateList(LinkList &L,int n)

//逆位序(节点插在表头)输入n个元素的值,建立带表头结点的单链线性表L

{
int i;
LinkList p;

L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
printf("请输入%d个数据\n",n);

for(i = n;i > 0;i--)
{
    p = (LinkList)malloc(sizeof(LNode));
    scanf("%d",&p->data);
    p->next = L->next;
    L->next = p;
}

}

void CreateList1(LinkList &L,int n)
//正位序(结点插在表尾)输入n个元素的值,建立带头结点的单链线性表L;

{
int i;
LinkList p,q;

L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
q = L;

printf("请输入%d个数据\n",n);

for(i = 1;i <= n;i++)
{
    p = (LinkList)malloc(sizeof(LNode));

    scanf("%d",&p->data);

    q->next = p;  //注意不是L->next = p,因为每次插入是插在上个节点的后面
    q = q->next;
}

p->next = NULL;

}

void MergeList(LinkList La,LinkList &Lb,LinkList &Lc)

{
LinkList pa = La->next,pb = Lb->next,pc;
Lc = pc = La;

while(pa && pb)
    if(pa->data <= pb->data)
    {
        pc->next = pa;
        pc = pa;
        pa = pa->next;
    }
    else
    {
        pc->next = pb;
        pc = pb;
        pb = pb->next;

    }

    pc->next = pa ? pa : pb;
    free(Lb);
    Lb = NULL;

}

void ListTraverse(LinkList L,void(* visit)(ElemType))

{
LinkList p = L->next;

while(p)
{
    visit(p->data);
    p = p->next;
}

printf("\n");

}

**void print(ELemType c) //显示错误的地方!

{

printf("%d",c);

}**

void main()
{
int n = 5;
LinkList La,Lb,Lc;
printf("按非递减顺序,");
CreateList(La,n);
printf("La = ");
ListTraverse(La,print);

printf("按非递减顺序,");
CreateList(Lb,print);
printf("Lb = ");
ListTraverse(Lb,print);

MergeList(La,Lb,Lc);
printf("Lc = ");
ListTraverse(Lc,print);

}

/*
error C2065: 'ELemType' : undeclared identifier

//不理解这儿:我前面已经用 typedef int ElemType 定义了ElemType,为什么此处还提示这样的错误信息!

error C2146: syntax error : missing ')' before identifier 'c'

//这个我也不明白了

error C2182: 'print' : illegal use of type 'void'

//这个print是通过调用ListTraverse来实现的,就应该是void型,此处为什么不正确

error C2059: syntax error : ')'
*/

5个回答

YXTS122
YXTS122   2017.01.15 12:51

ELemType和ElemType,两个单词一样吗

YXTS122
YXTS122 CreateList(Lb,print);这个估计是你粗心写错了,应该是CreateList(Lb,n);
11 个月之前 回复
YXTS122
YXTS122 ElemType print(ElemType c){return c;}改为这样
11 个月之前 回复
wearetheworld1234
wearetheworld1234 对对对,这个您说的对,我确实没注意到,但是那个void print函数是怎么回事?
11 个月之前 回复
YXTS122
YXTS122   2017.01.15 13:27

CreateList(Lb,print(n));

wearetheworld1234
wearetheworld1234 谢谢您的回答,但是我不怎么理解,不好意思啊,能再给我说一下吗?
11 个月之前 回复
wearetheworld1234
wearetheworld1234   2017.01.15 11:34

这个不知道怎么理解,求大神指点!感激不尽!

YXTS122
YXTS122   2017.01.15 13:07

ElemType print(ElemType c)
{
return c;
}

YXTS122
YXTS122 (*visit)(p->data);改一下这个
11 个月之前 回复
wearetheworld1234
wearetheworld1234 但是这个print函数是被void ListTraverse(LinkList L,void(* visit)(ElemType))这个引用的,形参要对应,print不应该有返回值的,求指教!
11 个月之前 回复
YXTS122
YXTS122   2017.01.16 12:07

我运行了一下,没问题
#include<stdio.h>
#include<stdlib.h>
#define OK 1

#define ERROR 0

typedef int Status;
typedef int ElemType;
struct LNode
{
ElemType data;
struct LNode * next;
};
typedef struct LNode * LinkList;
void CreateList(LinkList &L,int n)
//逆位序(节点插在表头)输入n个元素的值,建立带表头结点的单链线性表L
{
int i;
LinkList p;
L = (LinkList)malloc(sizeof (struct LNode));
L->next = NULL;
printf("请输入%d个数据\n",n);
for (i = n;i >0;i--)
{
p = (LinkList)malloc(sizeof (struct LNode));
scanf("%d",&p->data);
p->next = L->next;
L->next = p;
}
}
void CreateList1(LinkList &L,int n)
//正位序(结点插在表尾)输入n个元素的值,建立带头结点的单链线性表L;
{
int i;
LinkList p,q;
L = (LinkList)malloc(sizeof (struct LNode));
L->next = NULL;
q = L;
printf("请输入%d个数据\n",n);
for (i = 1;i <= n;i++)
{
p = (LinkList)malloc(sizeof (struct LNode));
scanf("%d",&p->data);
q->next = p;
//注意不是L->next = p,因为每次插入是插在上个节点的后面
q = q->next;
}
p->next = NULL;
}
void MergeList(LinkList La,LinkList &Lb,LinkList &Lc)
{
LinkList pa = La->next,pb = Lb->next,pc;
Lc = pc = La;
while (pa && pb)
if (pa->data <= pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
pc->next = pa ? pa : pb;
free(Lb);
Lb = NULL;
}
void print(ElemType c)
{//显示错误的地方
printf("%d",c);
}
void ListTraverse(LinkList L,void (* visit)(ElemType))
{
LinkList p = L->next;
while (p)
{
(*visit)(p->data);
p = p->next;
}
printf("\n");
}
int main()
{
int n = 5;
LinkList La,Lb,Lc;
printf("按非递减顺序,");
CreateList(La,n);
printf("La = ");
ListTraverse(La,print);
printf("按非递减顺序,");
CreateList(Lb,n);
printf("Lb = ");
ListTraverse(Lb,print);
MergeList(La,Lb,Lc);
printf("Lc = ");
ListTraverse(Lc,print);
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!