Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Sequence is [5,0,1,4,3,0,0,6,1,0]. Now have to use queue and stack to arrange the sequence in the order [ 5,1,4,3,6,1,0,0,0,0].
Posted
Comments
[no name] 2-Mar-13 13:44pm    
Okay... good for you. Good luck with that.
Zoltán Zörgő 2-Mar-13 16:33pm    
How exactly is your assignment formulated?
Yossi Yaari 4-Mar-13 14:20pm    
sounds like a really forced question :-)

Great! It's very simple to do if you put any thought into it. Just look at the first number and think about what you need to do with it, then look at the next one and do the same. Rinse and repeat until there are no more numbers.

No, we're not going to do your homework for you. So, if you were looking for someone to write your code for you, you've come to wrong place.
 
Share this answer
 
How about this pseudo code (if you insist using both containers)
repeat for each item in the given sequence
   push item to stack if item equals 0
   enqueue item to queue otherwise
end repeat
dequeue all items from the queue
pop all items from the stack

Cheers
Andi
 
Share this answer
 
Hi It seems like bubble sort example but the condition is little different.Try bellow code
C#
int[] a = new int[] { 5, 0, 1, 4, 3, 0, 0, 6, 1, 0 };
int  temp;
            for (int pass = 1; pass <= a.Length - 2; pass++)
            {
                for (int i = 0; i <= a.Length - 2; i++)
                {
                    if (a[i]==0 )
                    {
                        temp = a[i + 1];
                        a[i + 1] = a[i];
                        a[i] = temp;
                    }

                }

            }



string str = "";
for (int i = 0; i < a.Length; i++) //for loop to print sorted elements
{
str += a[i] + ",";
}

in str you can able to find the sorted list of aaray.
 
Share this answer
 
v6
Comments
Zoltán Zörgő 3-Mar-13 3:00am    
And where is the queue and the stack the OP asked for?
Shobhana.n 3-Mar-13 5:07am    
it is in array itself.If you want to see the sorted array then please do one loop again on array.I have updated my answer.
Zoltán Zörgő 3-Mar-13 5:11am    
An array is not LIFO or FIFO by itself. You can however simulate both. But the OP has explicitly specified stack and queue.
Andreas Gieriet 4-Mar-13 16:04pm    
This is not an aswer to the question.
Andi
Dave Kreskowiak 4-Mar-13 16:21pm    
This answer has absolutely nothing to do with the requirements of the question at hand.

A sort is WAY over the top to solve this AND a sort doesn't even solve the problem properly. Look at the both the input and required output again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900