合并清单问题 [英] merging list problem

查看:65
本文介绍了合并清单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中,它会在代码访问冲突时抛出运行时错误。

在行z-> data = p->数据;


while(p!= NULL&& q!= NULL)


{


if(* s == NULL)


{


printf(" test));


* s ==( struct node *)malloc(sizeof(struct node));


z = * s;


}


else


{


z-> link =(struct node *)malloc(sizeof(struct node));


z = z-> link;


}


if(p->数据< q->数据)


{


z-> data = p-> data;


p = p-> link;


}


else if(q-> data< p->数据)


{


z-> data = q-> data;

q = q-> link;


}


else if(p-> data == q-> ;数据)


{


z-> data = q-> da ta;


p = p-> link;


q = q-> link;


}


}

完整的程序,它是两个链接列表的合并

------- -------------------------------------------------- -

// add.cpp:定义控制台应用程序的入口点。


//


#include< stdio.h>

#include< alloc.h>


struct node


{


int data;


struct node * link;


};


void add(struct node **,int);


void display(struct node *);


void lmerge(struct node *,struct node *,struct node **);


int main()


{


struct node * a,* b,* c;


a = NULL;


add( & a,3);


add(& a,11);


add(& a,6);


add(& a,9);


添加(& a,2);


add(& a,17);


add(& a,4) ;


add(& a,18);


add(& a,5);

>
add(& a,10);


display(a);


b = NULL;


add(& b,1);


add(& b,19);


添加(& b,12);


add(& b,16);


add(& b,15) ;


add(& b,8);


add(& b,7);

add(& b,13);


add(& b,14);


add(& ; b,20);


显示(b);


c = NULL;


lmerge(a,b,& c);


显示(c);


返回0;

}


void add(struct node ** q,int num)


{

struct node * p,* temp;


p = * q;


temp =(struct node *)malloc( sizeof(struct node));


temp-> d ata = num;


// temp-> link = NULL;


if(* q == NULL || p->数据> num)


{


* q = temp;


(* q) - > ; $ =
}
/>
while(p!= NULL)


{


if(p-> link == NULL || p-> link-> data> = num)


{


temp-> link = p-> link ;


p-> link = temp;


返回;


}


p = p-> link;


}


temp-> link = NULL;


p-> link = temp;


}


}


void display(struct node * p)

{


while(p!= NULL)


{


printf("%d \ n",p-> data);


p = p-> link;


}


}


void lmerge (struct node * p,struct node * q,struct node ** s)


{


struct node * z;


z = NULL;


if(p == NULL&& q == NULL)


返回;


while(p!= NULL&& q!= NULL)


{


if(* s == NULL)


{


printf(" test");


* s ==(struct node *)malloc(sizeof(struct node));


z = * s;


}


其他


{


z-> link =(struct node *)malloc(sizeof(struct node));


z = z-> link;


}


if(p-> data< q-> data)


{


printf(test);


z-> data = p-> data;


p = p-> link;


}


else if(q-> data< p - >数据)


{


z-> data = q-> data;


q = q-> link;


}


else if(p-> data == q->数据)


{


z-> data = q-> data;


P =对 - >链路; <无线电通信/>

q = q-> link;


}


}

while(p!= NULL)


{


z-> link =(struct node *)malloc( sizeof(struct node));


z = z-> link;


z-> data = p-> data;


p = p-> link;


}


while(q!= NULL )


{


z-> link =(struct node *)malloc(sizeof(struct node));


z = z-> link;


z-> data = q-> data;


q = q-> link;


}


z-> link = NULL;


}

-------------------------------------- --------------------------

任何帮助

谢谢


Pradyut
http://pradyut.tk http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming

印度

In this code it throws a runtime error on a code access violation .
On the line z->data=p->data;

while (p!=NULL && q!=NULL)

{

if (*s==NULL)

{

printf("test");

*s==(struct node*)malloc(sizeof(struct node));

z=*s;

}

else

{

z->link=(struct node*)malloc(sizeof(struct node));

z=z->link;

}

if(p->data < q->data)

{

z->data=p->data;

p=p->link;

}

else if(q->data < p->data)

{

z->data = q->data;

q=q->link;

}

else if(p->data==q->data)

{

z->data=q->data;

p=p->link;

q=q->link;

}

}
The full program, it is merging of two linked lists
----------------------------------------------------------
// add.cpp : Defines the entry point for the console application.

//

#include <stdio.h>
#include <alloc.h>

struct node

{

int data;

struct node *link;

};

void add(struct node **, int );

void display (struct node *);

void lmerge(struct node *, struct node *, struct node **);

int main()

{

struct node *a, *b, *c;

a=NULL;

add(&a, 3);

add(&a, 11);

add(&a, 6);

add(&a, 9);

add(&a, 2);

add(&a, 17);

add(&a, 4);

add(&a, 18);

add(&a, 5);

add(&a, 10);

display(a);

b = NULL;

add(&b, 1);

add(&b, 19);

add(&b, 12);

add(&b, 16);

add(&b, 15);

add(&b, 8);

add(&b, 7);

add(&b, 13);

add(&b, 14);

add(&b, 20);

display(b);

c=NULL;

lmerge(a,b, &c);

display(c);

return 0;

}

void add(struct node **q, int num)

{

struct node *p, *temp;

p=*q;

temp=(struct node*)malloc(sizeof(struct node));

temp->data = num;

//temp->link=NULL;

if(*q==NULL || p->data > num)

{

*q=temp;

(*q)->link=p;

}

else

{

while(p!=NULL)

{

if(p->link==NULL || p->link->data >= num )

{

temp->link=p->link;

p->link=temp;

return;

}

p = p->link;

}

temp->link=NULL;

p->link=temp;

}

}

void display (struct node *p)

{

while(p!=NULL)

{

printf("%d\n", p->data);

p=p->link;

}

}

void lmerge(struct node *p, struct node *q, struct node **s)

{

struct node *z;

z=NULL;

if(p==NULL && q==NULL)

return;

while (p!=NULL && q!=NULL)

{

if (*s==NULL)

{

printf("test");

*s==(struct node*)malloc(sizeof(struct node));

z=*s;

}

else

{

z->link=(struct node*)malloc(sizeof(struct node));

z=z->link;

}

if(p->data < q->data)

{

printf("test");

z->data=p->data;

p=p->link;

}

else if(q->data < p->data)

{

z->data = q->data;

q=q->link;

}

else if(p->data==q->data)

{

z->data=q->data;

p=p->link;

q=q->link;

}

}

while(p!=NULL)

{

z->link=(struct node*)malloc(sizeof(struct node));

z=z->link;

z->data=p->data;

p=p->link;

}

while(q!=NULL)

{

z->link=(struct node*)malloc(sizeof(struct node));

z=z->link;

z->data=q->data;

q=q->link;

}

z->link=NULL;

}
----------------------------------------------------------------
Any help
thanks

Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India

推荐答案

PRadyut写道:
PRadyut wrote:
// add.cpp


C ++是另一种语言。

#include< stdio.h>
#include< alloc.h>


malloc.h是标准标题,alloc不是。

int main()


旧式声明。

int main(void)是最新的。

temp =(struct node *)malloc(sizeof(struct node));

temp-> data = num;


如果temp为NULL怎么办?

这些错误发生在你的上一篇文章中。

}




while(p!= NULL)


双倍间距和大缩进

使得阅读比编写它更麻烦。

* s ==(struct node *)malloc(sizeof(struct node));
// add.cpp
C++ is another language.
#include <stdio.h>
#include <alloc.h>
malloc.h is a standard header, alloc isn''t.
int main()
Old style declaration.
int main(void) is current.
temp=(struct node*)malloc(sizeof(struct node));

temp->data = num;
What if temp is NULL?
These same mistakes were in your last post.
}

else

{

while(p!=NULL)
The double spacing and large indentation
make this more trouble to read than it was to write.
*s==(struct node*)malloc(sizeof(struct node));




你拼写错误" ="



You misspelled "="


pete< pf ***** @ mindspring.com>写道:
pete <pf*****@mindspring.com> wrote:
PRadyut写道:
PRadyut wrote:
#include< stdio.h>
#include< alloc.h>
#include <stdio.h>
#include <alloc.h>



malloc.h是一个标准的标题,alloc不是。



malloc.h is a standard header, alloc isn''t.




不,它不是。 < stdlib.h中>是malloc()的标准头。



No, it isn''t. <stdlib.h> is the Standard header for malloc().

temp =(struct node *)malloc(sizeof(struct node));
temp=(struct node*)malloc(sizeof(struct node));




在C中你不需要也不应该使用malloc()。如果你想用
写C ++,那就这样 - 并且使用new。


Richard



In C you don''t need to, and should not, cast malloc(). If you want to
write C++, do so - and use new.

Richard


Richard Bos写道:
Richard Bos wrote:

pete< pf ***** @ mindspring.com>写道:

pete <pf*****@mindspring.com> wrote:
PRadyut写道:
PRadyut wrote:
#include< stdio.h>
#include< alloc.h>
#include <stdio.h>
#include <alloc.h>



malloc.h是一个标准的标题,alloc不是。



malloc.h is a standard header, alloc isn''t.



不,它不是。 < stdlib.h中>是malloc()的标准标题。



No, it isn''t. <stdlib.h> is the Standard header for malloc().




我不知道怎么会错误拼写错误的stdlib.h。



I have no idea how I could have misspelled stdlib.h so badly.


这篇关于合并清单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆