Hey all. I'm trying to learn stack operations and so far, I believe I've gotten push() and pop() down.

I seem to be stuck on copy, however. I cannot get a check for empty to work correctly.

typedef struct _node
{
   payload       data;
   struct _node *prev;
} node;

int main() {
   node    *rootPtr = NULL;
   node    *copyPtr = NULL;
   payload userInput;    

   // get user input for adding items to the stack, yada yada yada...
   copyPtr = stackCopy(rootPtr,copyPtr);
}

node *stackCopy(node *originalPtr, node *copyPtr)
{
   node    *tempPtr;             
   node    *tempTop;             
   payload  data;                
   node    *copyStackRev = NULL; 
   node    *copyStackOrg = NULL; 

   if (copyPtr != NULL) {
	   while (!copyPtr.empty()) {
		   copyPtr.pop();
	   }
   }
...
}

I'm getting a compile error stating that "left of '.empty' must have class/struct/union" so I know I'm using the .empty check incorrectly but I don't know what's wrong. I've check around the forum as well as on cplusplus.com and cannot find the issue.

Edited 5 Years Ago by SacredFootball: n/a

Oh, I get it... You declared copyPtr in main, and you are using it int the StackCopy function. That function wouldn't know what copyPtr is.

A "node" doesn't have an empty() method (if it did, you'd have to access it as copyPtr->empty() anyway, because copyPtr is a pointer). Did you mean to pass in stacks instead of nodes?

Edited 5 Years Ago by jonsca: n/a

A "node" doesn't have an empty() method (if it did, you'd have to access it as copyPtr->empty() anyway, because copyPtr is a pointer). Did you mean to pass in stacks instead of nodes?

I'm trying to delete and existing copy of copy stack. Checking for null and popping everything if it isn't null.

Right, but you need a stack to do those operations on, not a node. A node has the two things that you have in the struct typedef. I'm not sure where you think the stack is coming from...

This article has been dead for over six months. Start a new discussion instead.