output stream acting weird

I am trying to program a stack based linked list. Something very weird is happening with my program. Whenever i want to print with cout<< in specific places, it actually changes the data of the stack. Also, using endl after the cout also changes the data of the program. Help would be greatly appreciated.


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>

using namespace std;

class node
{
public:
	int number;
	node* nextPtr=NULL;
};

class linkedList
{
public:
	int count=0;
	node* topPtr=NULL;
	node* headPtr=NULL;

	void push(node Node)
	{
		if(topPtr==NULL)
		{
			headPtr=&Node;

			topPtr=&Node;

			count++;

		}

		else
		{
			topPtr->nextPtr=&Node;

			topPtr=&Node;

			count++;
		}
	}

	int pop()
	{
		if(count==0)
		{
			cerr<<"Stack is empty";
		}

		else
		{


			if(headPtr==topPtr)
			{
				headPtr=NULL;
				topPtr=NULL;
				count--;
				cout<<headPtr->number<<"\n";
				return headPtr->number;
			}

			else
			{node* tempPtr=headPtr;
				while(tempPtr->nextPtr!=topPtr)
				{
					tempPtr=tempPtr->nextPtr;
				}


				topPtr=tempPtr;
				count--;
				return tempPtr->number;
			}

		}
	}

	bool isEmpty()
	{
		if(count==0)
		{
			return true;
		}

		else
		{
			return false;
		}
	}

	void print()
	{
		node* temp=headPtr;

		if(temp->nextPtr==NULL)
		{
			cout<<"ASS";
		}

		while(temp!=NULL)
		{
			cout<<temp->number<<endl;

			temp=temp->nextPtr;

			if(temp==NULL)
			{
				cout<<"SHIT";
			}

		}
	}
};

int main()
{
	linkedList list;
	node a;
node c;
c.number=8;
	a.number=4;

list.push(a);


	node* b=list.headPtr;

while(b!=NULL)
{
	cout<<b->number<<endl;
	b=b->nextPtr;

}

}
[code]
Last edited on
Why do you thing this has any relation to streams?

On lines 23, 25, 33 and 35 you are saving address of local variable which is destroyed as soon as function ends. It will lead to all kind of problems, one of which you see yourself.
You don't need a topPtr and a headPtr. They are the same thing. Get rid of one of them.

class node is an implementation detail of class linkedList. The user shouldn't have to use it or even know about it. In other words, you want the user to be able to do:
1
2
3
linkedList list;
list.push(4);
list.push(8);

and the linkedList class will take care of the details. This means that the first thing push() does is create a node to hold the item pushed:
1
2
3
4
5
6
void linkedList::push(int val)
{
    node *n = new node;
    n->number = val;
    n->nextPtr = nullptr;
    ...


Even better, let the node class take care of initializing itself:
class node {
public:
node(int v) : number(v), nextPtr(nullptr) {;}
...
};

and this way the beginning of push becomes:
1
2
3
4
void linkedList::push(int val)
{
    node *n = new node(val);
    ...

pop() should return the item that was most recently pushed. This is always the item that topPtr points to, so you don't need lines 61-72 at all.

Personally, I wouldn't use so many blank lines. They just make you scroll through the code more. Also use consistent indenting.
@MiiNiPaa, it doesn't get destroyed once function ends because it gets saved onto the class members.
it doesn't get destroyed
It does.
because it gets saved onto the class members
It does not. Its address is saved. Which becomes invalid after function finishes.
but why would it become invalid if the node object is instantiated in main and not in the function? The address of the node object still points to the node.
The object in question is not instantiated in main. By default, functions receive arguments by value. In other words, they receive a copy of the argument. That copy stops existing when the function finishes.
ooooooooooh, now i see lol. Thank you guys.
Topic archived. No new replies allowed.