0

I have a question about roundrobin algorithm with queue and I am new with data structures. The algorithm should make this:

  • we get 3 numbers like (12 5 8) and one quantum number for example q:10
  • Then algorithm should make this :

    step1: 12 5 18
    step2: 5 18 2
    step3: 18 2
    step4: 2 8
    step5: 8
    step6: array is empty
    

As you see when quantum number is equal or bigger than array's number, we will delete first number in array. when quantum number is smaller than our number ,then quantum-number (12-10=2) will be added to the end of the array.

Can someone help me ?

3
  • What have you tried so far? Commented Nov 3, 2018 at 17:35
  • Hi Please refer this How To Ask Good question , Good question asked get up voted easily, voted question get more attraction to get good answer. Commented Nov 3, 2018 at 17:36
  • god bless your example which is clearer than your problem statement Commented Nov 3, 2018 at 18:00

1 Answer 1

1

Welcome to StackOverflow!

You want to:

  • store the list in an ArrayList (or something similar)
  • get the first item
  • check whether or not it is larger than quantum
  • If so, add it to the back of the list (after subtracting quantum of course).

Just continue until the ArrayList is empty.

Here is the code:

ArrayList<Integer> values = new ArrayList<Integer>();
values.add(12);
values.add(5);
values.add(18);
int quantum = 10;
int index = 0;
while (values.size() > 0) {
    System.out.println(values.toString());
    int value = values.remove(0);
    if (value > quantum) {
        values.add(value - quantum);
    }
}
System.out.println("Array is empty");

This will give you the desired result:

[12, 5, 18]
[5, 18, 2]
[18, 2]
[2, 8]
[8]
Array is empty
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.