Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

How to Read in a list of values store it in a queue and change the order of the inputs?

For example, read in abc123efgh45678 and display a1b2c3d4e5f6g7h8

I know I can use queue to read in the values.

But how do I assign a to go beside 1, eg. a1?

queue <char> q ;

for (int j=0; j < input.size(); j++) {
  q.push (input[j]);
share|improve this question

closed as off topic by Glenn Nelson, Jalayn, thorsten müller, Bart van Ingen Schenau, MainMa Apr 30 '13 at 14:30

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 0 down vote accepted

It appears you have an input stream that can be split into two categories - letters and numbers, and then interleave the two values.

You need multiple list (a linked list would be ideal for this data structure - the rapid append to the end that comes with them is less costly than an array backed data structure - and you don't need random access to the list) - one for each type of data you get.

You read the data in, character by character and decide which list to put the character into - the number or letter one.

Then, iterate over the length of the lists and process (print out or whatever) each one.

The code would look something like:

LinkedList<char> letters;
LinkedList<char> numbers;

for each (character in input) {
  if(character is a letter) {
  } else { // character is a number

while (not(letters.isEmpty()) || not(numbers.isEmpty())) {
  if(not(letters.isEmpty())) {
    process(letters.poll()); // poll returns and removes the first element
  if(not(numbers.isEmpty())) {

This can be done other ways, such as interleaving the values in an array (the odd ones are letters, the even ones are numbers) and maintain multiple indexes into the array. Don't do this - it hides the intent of the code and makes it more difficult to debug later.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.