Stuck in an infinite loop

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include "stackLL.h" //#include "queueLL.h" //#include "priorityQueueLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); stk.pop(); // cout << "Popping: " << stk.pop() << endl; // cout << "Popping: " << stk.pop() << endl; // // stk.push(17); // stk.push(19); // stk.push(23); // // while( ! stk.empty() ) // { // cout << "Popping: " << stk.pop() << endl; // } // // // output order: 11,2,23,19,17,3,7,13,5 // // /////////////////////////////////////// // // //////////Test code for queue /////////// // // ///////////////////////////////////////// // // // // //////////Test code for priority queue///// // // /////////////////////////////////////////// // // stk.~stackLL(); return 0; }``````

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include using namespace 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<data<next; } return 0; } }; stackLL::stackLL() { top = NULL; } stackLL::~stackLL() { }``````
 ``123`` ``````tmp->data = x; top = tmp; tmp->next = NULL;``````

You should probably switch two last lines. It doesn't make sence now. And place lines after that in else statement.

EDIT: Yes it works. And you don't ever need branch here. Here is fixed function:
 ``12345678`` ``````void push(int x) { node *tmp; tmp = new node; tmp->data = x; tmp->next = top; top = tmp; }``````
Last edited on
I think I see what you mean but at the same time I kinda got confused, could you please clarify?
Here is part of your program flow if top is NULL:
 ``123`` ``````top = tmp; tmp->next = top; top = tmp;``````

After that `top->next == top` what had caused an infinet loop whet trying to extract last value in the stack.
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.
Last edited on
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*
Topic archived. No new replies allowed.