Advertising | Internet Advertising | Credit Cards | Loans | Zietuwel.nl
stacks ( C++) [Archive] - PCMech Forums

PDA

View Full Version : stacks ( C++)


GSXdan
12-07-2003, 08:50 PM
assuming:


class Node {
public:
Node( char = ' ') { data = value;, next = 0}
char data;
Node * next;
};

class stack {
public:
stack();
void push( char let );
char pop();
bool empty();
private:
Node * top;
};


would this pop function work(no need to compile it or anything, i just need to write it out)?:


char stack:: pop() {
char temp;

temp = (top->next)->data;
top-> = (top->next)->next; // not sure about this part

return temp;

}



seems a little too simple, but i guess there isnt too much involved.

TIA ^ dan

AerynSedai
12-10-2003, 01:53 PM
OOP, we meet again. it has been a long time.

unless the bottom node on the stack is a dummy node (i use them in doubly linked lists, but i have never heard of them in stacks), you should access data directly from top, not top->next.

also, what if the stack is empty?
char stack:: pop()
{
if( !top ) // NULL pointer
return ' ';

char temp = top->data;
top = top->next;
return temp;
}also might want to make Node a subclass contained within stack, unless you want to use Node in other classes. also, you have to worry about freeing memory.

there might be problems with this. i am a C programmer. been awhile since i have touched C++.

AS

GSXdan
12-10-2003, 03:45 PM
Thanks a lot and welcome to the forums! It will be nice to have more programmers(especially c/c++) on the board!

^ dan