merging list problem

P

PRadyut

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
 
P

pete

PRadyut wrote:
// 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 "="
 
R

Richard Bos

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


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

Richard
 
T

Tim Rentsch

PRadyut said:
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;

}

[300+ lines of double-spaced code snipped]

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


Any help
thanks

*Any* help? Don't use double-spaced code.

You're welcome.
 
C

CBFalconer

PRadyut said:
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)

Why double spaced? Is it purely to annoy?

....snip ...
// add.cpp : Defines the entry point for the console application.
^^ ^^^
.... snip ...

C++ is dealt with down the hall and on the right. Here we deal
with C, which is a different language. // comments are evil,
especially in newsgroups.
 
P

PRadyut

i'm really sorry for all the double indentation and all that but google
does this way.
i copy and paste from the notepad and the result is double indentation.


the compiler i use msvc7 or in .net 2003 ide. i tried also in borland
compiler and here i have to use alloc.h for using malloc in the
program.

one question, i have used malloc many a times but i don't know how can
that remain NULL as pete replied.
Also i want to know the use of "new" in c++ as richard replied, how to
use it and all that

the programmiing skills i possess are really bad but i'm using
non-standard compilers too as in msvc7 i'm writing code in c but the
file name is with .cpp extension.
also the programs that i write are for my academic exams and they are
using old programming techniques, more non-standard. Some people at
here are still using the turbo c compiler which came out around 90s

One more question which i'm afraid to ask
how to configure outlook such that i can read and write mails to
comp.lang.c with a hotmail or smtp address

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

pete

PRadyut wrote:
the compiler i use msvc7 or in .net 2003 ide. i tried also in borland
compiler and here i have to use alloc.h for using malloc in the
program.

Try using <stdlib.h> instead of <alloc.h>, and see what happens.
 
P

pete

PRadyut wrote:
one question, i have used malloc many a times but i don't know how can
that remain NULL as pete replied.

It is possible that malloc can run out of memory,
in which case it returns NULL.
 
E

Emmanuel Delahaye

PRadyut wrote on 01/06/05 :
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)

{

Excessive use of space makes the code as unreadble that lack of
space...

while (p != NULL && q != NULL)
{
if (*s == NULL)
{
printf ("test");

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

z = *s;
}


BTW, what is p ? what is q ? You should post a (more compact)
compilable code reduced to the minimum that shows the problem.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

PRadyut wrote on 01/06/05 :
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)

{

Excessive use of space makes the code as unreadble that lack of
space...

while (p != NULL && q != NULL)
{
if (*s == NULL)
{
printf ("test");

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

z = *s;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
O

Old Wolf

PRadyut said:
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));

This line tests to see if *s (ie. NULL) equals the address
returned by malloc. It then discards the result of the
malloc, and the result of the test.

Perhaps you meant to write:

*s = malloc(sizeof **s);

You could have helped to avoid this error by properly
formatting your code (ie. using spaces around operators).

Since *s is still NULL, now z is NULL. So it is not surprising
that z->data fails.
 
E

Emmanuel Delahaye

Emmanuel Delahaye wrote on 02/06/05 :
*s == (struct node *) malloc (sizeof (struct node));

Cute isn'it ?

You want :

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

- drop all these cast
- use the "Simple Way" (Sahaj marg) :

*s = malloc (sizeof *s);

- rename your file in .c (it seems to be a .cpp, you are probably not
using a C compiler)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

Emmanuel Delahaye wrote on 02/06/05 :
*s == (struct node *) malloc (sizeof (struct node));

Cute isn'it ?

You want :

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

- drop all these cast
- use the "Simple Way" (Sahaj marg) :

*s = malloc (sizeof **s);

- rename your file in .c (it seems to be a .cpp, you are probably not
using a C compiler)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
K

Keith Thompson

PRadyut said:
well can please anybody tell me how to configure comp.lang.c within
outlook

No, not really. You'll need to find a Usenet (NNTP) provider; I don't
know of any free ones. Ask your ISP.

Outlook configuration is absolutely off-topic here.

Incidentally, the groups.google.com interface is horrendously bad, but
I don't think it's responsible for double-spacing your code. It may
have to do with the way you cut-and-paste the text. (I think you said
you use Notepad; Wordpad might or might not work better.)

If you want to experiment, you can post test messages to the alt.test
newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,295
Latest member
EmilG1510

Latest Threads

Top