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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#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;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
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<<nxt->data<<endl;
			nxt = nxt->next;
		}
		return 0;
	}

};

stackLL::stackLL()
{
	top = NULL;
}

stackLL::~stackLL()
{

}
Your push function:
1
2
3
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:
1
2
3
4
5
6
7
8
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:
1
2
3
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.