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