|
From: <com...@ya...> - 2002-11-18 19:16:12
|
i tried to write a code using linked lists to implement the multiplication and addition of twp polynomials but it seems i'm not very successfull cause my pop funcition doesn't do what it should and i don't know why i get that access violation error ..could any one help??
thanx...
/* definitions*/
typedef struct node{
int coefficient;
int exponent;
struct node *next;
}node;
typedef struct stack{
node *firstptr;
}stack;
/*and the pop function*/
node *pop(stack *s){
node *ptr=new node;
if(nptr==NULL){
cout<<"exhausted memory"<<endl;
exit(1);
}
else{
nptr->coefficient=s->firstptr->coefficient;
nptr->exponent=s->firstptr->exponent;
s->firstptr=s->firstptr->next;//that's the problematic part
}
return nptr;
}
---------------------------------
Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site |
|
From: <or...@vp...> - 2002-11-19 15:24:37
|
Hello j b!
Monday, November 18, 2002, 8:16:12 PM, you wrote:
jb> i tried to write a code using linked lists to implement the
jb> multiplication and addition of twp polynomials but it seems i'm
jb> not very successfull cause my pop funcition doesn't do what it
jb> should and i don't know why i get that access violation error
jb> ..could any one help??
> /* definitions*/
>
> typedef struct node{
> int coefficient;
> int exponent;
> struct node *next;
> }node;
>
> typedef struct stack{
> node *firstptr;
> }stack;
>
> /*and the pop function*/
>
> node *pop(stack *s){
> node *nptr=new node;
> if(nptr==NULL){
> cout<<"exhausted memory"<<endl;
> exit(1);
> }
> else{
> nptr->coefficient=s->firstptr->coefficient;
> nptr->exponent=s->firstptr->exponent;
> s->firstptr=s->firstptr->next;//that's the problematic part
> }
> return nptr;
> }
Well the above code shouldn't give an access violation error, IF the
push (I assume you have a function named like that) works fine. So I
think the problem is with the push function.
And one more thing. I don't know much about C++, but I'm good at C,
and I think you should clean up memory when you detatch a node from
the list.
Something like:
node *pop(stack *s){
node *ntmp; /* Temporary pointer */
...
else{
...
ntmp = s->firstptr; /*!*/
s->firstptr=s->firstptr->next; //that's the problematic part
free(ntmp); /*!*/
}
return nprt;
}
OR:
===
Why not return only a pointer to the node, instead of creating a new
one?
Then the code would be:
node *pop(stack *s){
node *nptr; /* Only a pointer */
nptr = s->firstptr;
s->firstptr=s->firstptr->next;
return nptr;
}
Of course you will need to free the node that you get, from the
outside.
As I stated above, I don't know much about C++, but I've heard, that
there are many built-in functions, and "things" inside C++ for list
creation. (They are created through templates, as I remember). Look in
the book.
--
Best regards,
Oroszi Balázs mailto:or...@vp...
PGP-FP: FB80 8A43 2664 6126 E465 46E2 2546 CFA3 C25B 9F70
finger -l or...@se...
|
|
From: Per W. <pw...@ia...> - 2002-11-19 15:37:18
|
By the way - where does the pop function check if there is something to
pop, i.e. that s->firtptr isn't NULL?
/Per W
On Tue, 19 Nov 2002, Oroszi Bal=E1zs wrote:
> Hello j b!
>=20
> Monday, November 18, 2002, 8:16:12 PM, you wrote:
>=20
> jb> i tried to write a code using linked lists to implement the
> jb> multiplication and addition of twp polynomials but it seems i'm
> jb> not very successfull cause my pop funcition doesn't do what it
> jb> should and i don't know why i get that access violation error
> jb> ..could any one help??
>=20
> > /* definitions*/
> >
> > typedef struct node{
> > int coefficient;
> > int exponent;
> > struct node *next;
> > }node;
> >
> > typedef struct stack{
> > node *firstptr;
> > }stack;
> >
> > /*and the pop function*/
> >=20
> > node *pop(stack *s){
> > node *nptr=3Dnew node;
> > if(nptr=3D=3DNULL){
> > cout<<"exhausted memory"<<endl;
> > exit(1);
> > }
> > else{
> > nptr->coefficient=3Ds->firstptr->coefficient;
> > nptr->exponent=3Ds->firstptr->exponent;
> > s->firstptr=3Ds->firstptr->next;//that's the problematic part
> > }
> > return nptr;
> > }
>=20
> Well the above code shouldn't give an access violation error, IF the
> push (I assume you have a function named like that) works fine. So I
> think the problem is with the push function.
>=20
> And one more thing. I don't know much about C++, but I'm good at C,
> and I think you should clean up memory when you detatch a node from
> the list.
>=20
> Something like:
>=20
> node *pop(stack *s){
> node *ntmp; /* Temporary pointer */
> ...
> else{
> ...
> ntmp =3D s->firstptr; /*!*/
> s->firstptr=3Ds->firstptr->next; //that's the problematic part
> free(ntmp); /*!*/
> }
> return nprt;
> }
>=20
> OR:
> =3D=3D=3D
> Why not return only a pointer to the node, instead of creating a new
> one?
> Then the code would be:
>=20
> node *pop(stack *s){
> node *nptr; /* Only a pointer */
> nptr =3D s->firstptr;
> s->firstptr=3Ds->firstptr->next;
> return nptr;
> }
>=20
> Of course you will need to free the node that you get, from the
> outside.
>=20
> As I stated above, I don't know much about C++, but I've heard, that
> there are many built-in functions, and "things" inside C++ for list
> creation. (They are created through templates, as I remember). Look in
> the book.
>=20
> --=20
> Best regards,
> Oroszi Bal=E1zs mailto:or...@vp...
>=20
> PGP-FP: FB80 8A43 2664 6126 E465 46E2 2546 CFA3 C25B 9F70
> finger -l or...@se... =20
>=20
>=20
>=20
>=20
> -------------------------------------------------------
> This sf.net email is sponsored by: To learn the basics of securing=20
> your web site with SSL, click here to get a FREE TRIAL of a Thawte=20
> Server Certificate: http://www.gothawte.com/rd524.html
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
|
|
From: <qa...@ho...> - 2002-11-19 15:44:38
|
Make sure to test that
s->firstptr !=3D NULL
otherwise the right-hand-side of the statement
s->firstptr=3Ds->firstptr->next;
would be
NULL->next
and that is ofcourse wrong
/J
*********** REPLY SEPARATOR ***********
On 2002-11-19 at 16:23 Oroszi Bal=E1zs wrote:
>Hello j b!
>
>Monday, November 18, 2002, 8:16:12 PM, you wrote:
>
>jb> i tried to write a code using linked lists to implement the
>jb> multiplication and addition of twp polynomials but it seems i'm
>jb> not very successfull cause my pop funcition doesn't do what it
>jb> should and i don't know why i get that access violation error
>jb> ..could any one help??
>
>> /* definitions*/
>>
>> typedef struct node{
>> int coefficient;
>> int exponent;
>> struct node *next;
>> }node;
>>
>> typedef struct stack{
>> node *firstptr;
>> }stack;
>>
>> /*and the pop function*/
>>
>> node *pop(stack *s){
>> node *nptr=3Dnew node;
>> if(nptr=3D=3DNULL){
>> cout<<"exhausted memory"<<endl;
>> exit(1);
>> }
>> else{
>> nptr->coefficient=3Ds->firstptr->coefficient;
>> nptr->exponent=3Ds->firstptr->exponent;
>> s->firstptr=3Ds->firstptr->next;//that's the problematic part
>> }
>> return nptr;
>> }
>
>Well the above code shouldn't give an access violation error, IF the
>push (I assume you have a function named like that) works fine. So I
>think the problem is with the push function.
>
>And one more thing. I don't know much about C++, but I'm good at C,
>and I think you should clean up memory when you detatch a node from
>the list.
>
>Something like:
>
>node *pop(stack *s){
> node *ntmp; /* Temporary pointer */
> ...
> else{
> ...
> ntmp =3D s->firstptr; /*!*/
> s->firstptr=3Ds->firstptr->next; //that's the problematic part
> free(ntmp); /*!*/
> }
> return nprt;
>}
>
>OR:
>=3D=3D=3D
>Why not return only a pointer to the node, instead of creating a new
>one?
>Then the code would be:
>
>node *pop(stack *s){
> node *nptr; /* Only a pointer */
> nptr =3D s->firstptr;
> s->firstptr=3Ds->firstptr->next;
> return nptr;
>}
>
>Of course you will need to free the node that you get, from the
>outside.
>
>As I stated above, I don't know much about C++, but I've heard, that
>there are many built-in functions, and "things" inside C++ for list
>creation. (They are created through templates, as I remember). Look in
>the book.
>
>--
>Best regards,
> Oroszi Bal=E1zs mailto:or...@vp...
>
>PGP-FP: FB80 8A43 2664 6126 E465 46E2 2546 CFA3 C25B 9F70
>finger -l or...@se...
>
>
>
>
>-------------------------------------------------------
>This sf.net email is sponsored by: To learn the basics of securing
>your web site with SSL, click here to get a FREE TRIAL of a Thawte
>Server Certificate: http://www.gothawte.com/rd524.html
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
t
|
|
From: <or...@vp...> - 2002-11-19 20:32:53
|
Hello j b!
Jörgen wrote:
> Make sure to test that
> s->firstptr != NULL
> otherwise the right-hand-side of the statement
> s->firstptr=s->firstptr->next;
> would be
> NULL->next
> and that is ofcourse wrong
Oh, yes, I forgot that, of course you need to check whether it is
NULL, or not, sorry %) But despite that, I still recommend to use the
C++ built-in (standard library) lists. Or if you don't know how to use
them, then try this code for pop:
node *pop(stack *s){
node *nptr; /* Only a pointer */
nptr = s->firstptr;
if (s->firstptr != NULL) s->firstptr = s->firstptr->next;
return nptr;
}
pop() will return NULL, if there are no elements. And you should
delete the node from "outside", when you "popped" it out and done all
calculations with it, so that you won't trash the memory.
--
Best regards,
Oroszi Balázs mailto:or...@vp...
P.S.: Correct the code, if it is wrong, please!
PGP-FP: FB80 8A43 2664 6126 E465 46E2 2546 CFA3 C25B 9F70
finger -l or...@se...
|