Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   bug in stack (http://www.velocityreviews.com/forums/t744276-bug-in-stack.html)

asit 02-27-2011 07:30 PM

bug in stack
 
what's wrong in the following code ??

#include <iostream>
#include <string>

using namespace std;

struct node
{
int data;
node *prev;
};

node* push(node * st, int data)
{
if(st == NULL)
{
st = new node;
st->prev = NULL;
st->data = data;
}
else
{
node* t = new node;
t->prev = st;
t->data = data;
st = t;
}
return st;
}



Alain Ketterlin 02-27-2011 07:57 PM

Re: bug in stack
 
asit <lipun4u@gmail.com> writes:

> what's wrong in the following code ??


> struct node
> {
> int data;
> node *prev;
> };
>
> node* push(node * st, int data)
> {
> if(st == NULL)
> {
> st = new node;
> st->prev = NULL;
> st->data = data;
> }
> else
> {
> node* t = new node;
> t->prev = st;
> t->data = data;
> st = t;
> }
> return st;
> }


Do you really think both cases are different? Is it really relevant to
distinguish a null incoming stack and a non-null stack?

(By the way, I would advise to not use function arguments as local
variables. You'll save nothing, the compiler is anyway much better at
optimizing than you are. Use as many local variables as you want/need,
give them significant names, and avoid reassigning different values to
the same variable through the course of the function. If you apply this
rule to the code above, you'll see why I ask the questions above.)

Finally, how do you call push()? Your version returns the "new stack",
make sure you use that value.

I guess you haven't learnt constructors yet. If you have, try to think
about how you could use them here. If you have not, forget about them
for now.

-- Alain.

Michael Doubez 02-27-2011 10:08 PM

Re: bug in stack
 
On 27 fév, 21:01, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
> asit <lipu...@gmail.com> wrote in news:e9e79c27-af4b-41ea-91e9-
> 0ca75b41f...@glegroupsg2000goo.googlegroups.com:
>
> > what's wrong in the following code ??

>
> Why do you think there is something wrong with this code?
>
>
>
> > #include <iostream>

>
> not needed
>
> > #include <string>

>
> not needed
>
>
>
> > using namespace std;

>
> not needed
>
>
>
> > struct node
> > {
> > * * * * int data;
> > * * * * node *prev;
> > };

>
> > node* push(node * st, int data)
> > {
> > * * * * if(st == NULL)
> > * * * * {
> > * * * * * * * * st = new node;
> > * * * * * * * * st->prev = NULL;
> > * * * * * * * * st->data = data;
> > * * * * }

>
> duplicate code, the else branch has exactly the same functionality.
>
> > * * * * else
> > * * * * {
> > * * * * * * * * node* t = new node;
> > * * * * * * * * t->prev = st;
> > * * * * * * * * t->data = data;
> > * * * * * * * * st = t;
> > * * * * }
> > * * * * return st;
> > }

>
> Other than lots of unneeded lines and missing specifications about the
> purpose of the struct and the function, I do not see nothing wrong with
> this code.


He is assigning the function parameter value 'st'. It shoukld either
be a pointer-pointer or a pointer-reference.

A coworker of mine thinks all parameter values should be const; I
disagree for readability reasons but it could be a interesting
compiler option.

[snip]

--
Michael

Paul 02-28-2011 12:33 AM

Re: bug in stack
 

"asit" <lipun4u@gmail.com> wrote in message
news:e9e79c27-af4b-41ea-91e9-0ca75b41fd7b@glegroupsg2000goo.googlegroups.com...
> what's wrong in the following code ??
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> struct node
> {
> int data;
> node *prev;
> };
>
> node* push(node * st, int data)
> {
> if(st == NULL)
> {
> st = new node;
> st->prev = NULL;
> st->data = data;
> }
> else
> {
> node* t = new node;
> t->prev = st;
> t->data = data;
> st = t;
> }
> return st;
> }
>
>

If you pass another stack as a node any original stack nodes are
unreachable.
The problem is that the data-node and the stack are both the same object.

HTH


Martijn van Buul 02-28-2011 10:42 AM

Re: bug in stack
 
* Christian Hackl:
> asit ha scritto:
>
>> what's wrong in the following code ??

>
> Most importantly, the fact you don't use a standard C++ container class,
> such as std::stack (if you really want a stack).


Is that so? I think it's a bit of a condescending remark. There are a
lot of reasons why you'd want to implement a stack (or a list, or a
set) yourself - including a didactic one:

You won't appreciate the value of a calculator if you never did any number
crunching by hand.

--
Martijn van Buul - pino@dohd.org

Paul 02-28-2011 05:56 PM

Re: bug in stack
 

"Christian Hackl" <hacki@sbox.tugraz.at> wrote in message
news:ikebd5$pnj$1@news.eternal-september.org...
> asit ha scritto:
>
>> what's wrong in the following code ??

>
> Most importantly, the fact you don't use a standard C++ container class,
> such as std::stack (if you really want a stack).
>
>

I agree with Martin , I find this comment a bit annoying..

How did C++ programmers even manage pre-STL?


Paul 02-28-2011 08:12 PM

Re: bug in stack
 

"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:5NGdneVHUKOGMvbQnZ2dnUVZ8hqdnZ2d@giganews.com ...
> On 28/02/2011 00:33, Paul wrote:
>>
>> "asit" <lipun4u@gmail.com> wrote in message
>> news:e9e79c27-af4b-41ea-91e9-0ca75b41fd7b@glegroupsg2000goo.googlegroups.com...
>>
>>> what's wrong in the following code ??
>>>
>>> #include <iostream>
>>> #include <string>
>>>
>>> using namespace std;
>>>
>>> struct node
>>> {
>>> int data;
>>> node *prev;
>>> };
>>>
>>> node* push(node * st, int data)
>>> {
>>> if(st == NULL)
>>> {
>>> st = new node;
>>> st->prev = NULL;
>>> st->data = data;
>>> }
>>> else
>>> {
>>> node* t = new node;
>>> t->prev = st;
>>> t->data = data;
>>> st = t;
>>> }
>>> return st;
>>> }
>>>
>>>

>> If you pass another stack as a node any original stack nodes are
>> unreachable.
>> The problem is that the data-node and the stack are both the same object.

>
> What utter nonsense. 'push' returns a pointer to the new top of the stack
> *passed to it*; it doesn't affect any other pointers or stacks; you do not
> know what the OP is doing with the return value of 'push'.
>


Utter nonsense is your deparment.

You probably don't even understand so I will explain:
Node* st1, st2;
st2->prev = push(push(push(s1->prev,0),0),0);
st1 = push(push(st2->prev,0),0);




Martijn van Buul 02-28-2011 08:20 PM

Re: bug in stack
 
* Leigh Johnston:
> I need to hammer in a nail. I have a hammer in my toolbox but I prefer
> to bash nails in with this heavy rock instead. The moral of the story:
> don't make work for yourself.


But many parts of STL (and even moreso: boost!) aren't really compareable
with a hammer. They're more like a powerdrill. Useful, convenient to have
around, and there's tons of people who think you need nothing else.

--
Martijn van Buul - pino@dohd.org

Martijn van Buul 02-28-2011 08:22 PM

Re: bug in stack
 
* Paul:

> You probably don't even understand so I will explain:
> Node* st1, st2;
> st2->prev = push(push(push(s1->prev,0),0),0);


Segmentation fault, core dumped.

--
Martijn van Buul - pino@dohd.org

Paul 02-28-2011 08:26 PM

Re: bug in stack
 

"Martijn van Buul" <pino@dohd.org> wrote in message
news:slrnimo0vm.1gnd.pino@mud.stack.nl...
>* Paul:
>
>> You probably don't even understand so I will explain:
>> Node* st1, st2;
>> st2->prev = push(push(push(s1->prev,0),0),0);

>
> Segmentation fault, core dumped.
>

It's ok its only for Leigh.



All times are GMT. The time now is 02:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57