AHHHGG!
Ben, This is horrible! I think you've not factored the functionality
properly
between the Stack and the "storage" or list class:-
A stack can be implemented by using a list or an array, the essence of the
stack is you only care about the head or the "top", so there's a lot of
unnecessary
cruft in your list struct ( or more accurately a "node" class) dealing with
tails & curs.
Secondly, I'd want the list to be a bit smarter than the class you've
provided, I'd want to
do useful, powerful things like insert a node, get a node remove a node,
tell me if the list is empty.
Thirdly, a stack is a more "specialize d" data structure than the list in
that operations are
solely performed at the head, it would seem that I could easily adapt a list
to perform operations
at the head of the list and I'd get my stack class. Typically, this
specialization is performed by "wrapping" a list class member inside the
stack class and implementing the stack operations by "forwarding " to
particular member list operations:
class Stack
{
public:
// constructor/destructor stuff not show.
public:
// error checking for empty stack omitted for illustration
void push( int e){ _list.insert_at _head(e); } // forwarding operation.
int pop( ) { return _list.remove_at _head( );}
int top( ) { return _list.element_a t_head();
bool isempty(){ return _list.isempty() ;}
private:
list _list;
};
So, the Stack is a simple shell or wrapper over the list class which has all
the brains.
In this model, the destruction of the Stack is trivial, the _list member is
automatically
destroyed, the list class should provide the destruction behavior to loop
over the remaining
nodes left in the list and destroy them.
hope that helps!
dave
"Dave Townsend" <da********@com cast.net> wrote in message
news:7u******** ************@co mcast.com...
Ben, the destructors in both don't seem to be right, I would expect
to see a loop to visit all the list-nodes and delete each of them.
I'm also a bit suspicious of what happens when you have 1 or 2
items in the list, it looks to me that head and tail might be deleted
twice, what about cur too?
You have a pop() operation, but there's no way to get the value of
the top of the stack, such as int top().
"Ben" <cr*********@ya hoo.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com... Hi all,
I implemented a stack in C++ in 2 different ways and I'd like to know
which approach is better than the other.. and if there is any
difference between the two? I'd also like to know if the destructor
i'm using is correct.. I got segmentation fault in my second approach
while I quit the program. I appreciate any help....
My first appoach goes like this:
/* List struct */
typedef struct list {
int data;
list *next;
list *prev;
} list;
/* Stack class */
class dynamic_stack {
list *head;
list *tail;
list *cur;
int NumNodes;
public:
dynamic_stack() {
head = tail = cur = NULL;
NumNodes = 0;
}
~dynamic_stack( ) {
delete head; delete tail; delete cur;
NumNodes = 0;
}
void push(int);
void pop();
void print();
};
My second approach is without the struct:
/* Stack class */
class dynamic_stack {
dynamic_stack *head;
dynamic_stack *tail;
dynamic_stack *cur;
int NumNodes;
int data;
dynamic_stack *next;
dynamic_stack *prev;
public:
dynamic_stack() ;
~dynamic_stack( ) {
delete head; delete tail; delete cur;
NumNodes=data=0 ;
delete next; delete prev;
}
void push(int);
void pop();
void print();
};
Thanks a lot!
Ben