I have a new problem. For some reason, my code gets stuck in an infinite loop here on pop which I temp made as a transverse function to ensure my stack is working correctly. Here is what I have.
#include <iostream>
usingnamespace std;
class stackLL
{
private:
class node
{
public:
int data; // for holding data in the node
node *next; // for pointing to the next.
};
node *top;
node *nxt;
public:
stackLL();
~stackLL();
//return true if empty, false if not
bool empty()
{}
//add item to top of stack
void push(int x)
{
node *tmp;
tmp = new node;
if (top == NULL)
{
tmp->data = x;
top = tmp;
tmp->next = NULL;
}
tmp->data = x;
tmp->next = top;
top = tmp;
}
//remove and return top item from stack
int pop()
{
nxt = top;
while (nxt != NULL)
{
cout<<nxt->data<<endl;
nxt = nxt->next;
}
return 0;
}
};
stackLL::stackLL()
{
top = NULL;
}
stackLL::~stackLL()
{
}
I don't know what you did differently from me but it worked compared to mine. I tried to seriously look at what i did compared to you and it seems like you only did the same thing but simplified but at the same time I know you did something because your solution works better than mine.
Posted this after you posted but I don't see how that happened, at least not yet but your code worked none the less.
Now I see how you did it and I see why it happened the way it happened. Not only did you make my code more efficient but I realize that if I only put an else there, I would of avoided this problem. *facepalm on my part*