adding same element in 2 arrays using one function

Hi Everyone
i am doing a depth first search array implementation in c++..and there's this push() function which adds items into array with size 8. together with that i need to show the path of the search .. everything that's pushed into the stack automatically comes in the path. should i write path[++order] in the else statement ..

push(int x)
{
if(top>8)
{
cout<<"stack is full";
}
else
{
stk[++top] = x;
cout<<"added element: "<<x;
}
}


If you had used the code brackets it would probably have looked like this and been easier to understand.
1
2
3
4
5
6
7
8
9
10
11
12
push(int x)
{
	if(top>8)
	{
		cout<<"stack is full"; 
	}
	else
	{
		stk[++top] = x;
		cout<<"added element: "<<x;
	}
}


should i write path[++order] in the else statement

Looking at this source I don't see any variable called "path" or "order", so I don't know what that would do, but I would think that using ++top is a good idea here.

What exactly is your problem?
Maybe a modification like this will help you find if you have an issue and where to look for it (there is probably a pop function as well, that should probably get a similar modification to get a complete view).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
push(int x)
{
	if(top>8)
	{
		cout<<"stack is full"; 
	}
	else
	{
		stk[++top] = x;
		cout<<"added element: "<<x << "\nThe path is now: ";
		for (int i=0;i<top;i++)
		{
			cout << stk[i] << "\t";
		}
		cout << "\n";
	}
}


Kind regards, Nico
Topic archived. No new replies allowed.