# Thread: Need help with stacks

1. ## Need help with stacks

hi guys, im trying to complete a function where you have stack S and stack T. Both integer type and both have 4 elements in each stack which the elements are in ascending order (Top to bottom (eg..1 2 3 4 7 8)). Now i have to combine both stacks and make one union stack which combines both stacks S and T and puts that stack in ascending order.

my program only outputs 5 elements and not 8 (combining of both S and T).

Code:
```stack <int> unionS(stack<int>& S, stack<int>& T)
{
stack <int> unionStack;
stack <int> a;
stack <int> b;

S.push(2);//top
S.push(4);
S.push(8);
S.push(9);//bottom

T.push(1);//top
T.push(3);
T.push(5);
T.push(7);//bottom

for(int j = S.size(); j>0; j--)
{
a.push(S.top());
S.pop();
}

for(int g = T.size(); g>0; g--)
{
b.push(T.top());
T.pop();
}

cout<<a.size()<<" ELEMENTS IN STACK a";
cout<<endl;
cout<<b.size()<<" ELEMENTS IN STACK b";
cout<<endl;

for(int x = 0; x<=(a.size())+(b.size()); x++)
{

if(a.top()<=b.top())
{
unionStack.push(a.top());
a.pop();
}
else
{
unionStack.push(b.top());
b.pop();
}

}

cout<<unionStack.size();
cout<<endl;

}```
any1 know why i can only get 5 and not all 8 into the unionStack that unites both S and T and puts all the numbers in ascending order? thank u!

2. its ok guys, i got it working

Code:
```stack <int> unionS(stack<int>& S, stack<int>& T)
{
stack <int> unionStack;
stack <int> unionStackFinal;
stack <int> unionStackFinal2;
stack <int> a;
stack <int> b;

S.push(1);//top
S.push(6);
S.push(8);
S.push(9);//bottom

T.push(4);//top
T.push(5);
T.push(6);
T.push(7);//bottom

for(int j = S.size(); j>0; j--)
{
a.push(S.top());
S.pop();
}

for(int g = T.size(); g>0; g--)
{
b.push(T.top());
T.pop();
}

cout<<a.size()<<" ELEMENTS IN STACK a";
cout<<endl;
cout<<b.size()<<" ELEMENTS IN STACK b";
cout<<endl;

for(int x = 0; x<=(a.size() + b.size()); x++)
{

if(a.top()<=b.top())
{
unionStack.push(a.top());
a.pop();
}

if(b.top()<=a.top())
{
unionStack.push(b.top());
b.pop();
}

if(a.size()==0)
{
for(int z = b.size(); z>0; z--)
{
unionStack.push(b.top());
b.pop();
}
}

if(b.size()==0)
{
for(int f = a.size(); f>0; f--)
{
unionStack.push(a.top());
a.pop();
}
}

}
cout<<endl;
cout<<unionStack.size();
cout<<endl;

for(int h = unionStack.size(); h>=1; h--)
{
unionStackFinal.push(unionStack.top());
unionStack.pop();
}
cout<<endl;
for(int b = unionStackFinal.size(); b>=1; b--)
{
cout<<unionStackFinal.top();
unionStackFinal.pop();
}

cout<<endl;

}```

3. There are still a number of problems with that code.
Your 'x' for loop is not right. Each iteration the size of a or b decreases and x also increases.
You'd be better off using an infinite loop "for(;; )" and "break"ing out of the loop when either stack becomes empty. I also suggest putting the test for an empty stack first so that combining two stacks where one was empty to begin with, also works.

Also, each iteration, one OR two items are added to the unionStack. You should instead make these use an if-else instead:
Code:
```     if(a.top()<=b.top())
...
if(b.top()<=a.top())
...```
becomes:
Code:
```     if(a.top()<=b.top())
...
else
...```
Lastly, you shouldn't use ".size()==0". To specifically test for an empty container you should use ".empty()". Cleaner, and potentially more efficient when all you care about is whether it is zero or not, rather than the exact value.

4. Originally Posted by iMalc
There are still a number of problems with that code.
Your 'x' for loop is not right. Each iteration the size of a or b decreases and x also increases.
You'd be better off using an infinite loop "for(;; )" and "break"ing out of the loop when either stack becomes empty. I also suggest putting the test for an empty stack first so that combining two stacks where one was empty to begin with, also works.

Also, each iteration, one OR two items are added to the unionStack. You should instead make these use an if-else instead:
Code:
```     if(a.top()<=b.top())
...
if(b.top()<=a.top())
...```
becomes:
Code:
```     if(a.top()<=b.top())
...
else
...```
Lastly, you shouldn't use ".size()==0". To specifically test for an empty container you should use ".empty()". Cleaner, and potentially more efficient when all you care about is whether it is zero or not, rather than the exact value.
thanks for this. i will have a look at it!