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