|
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
|