Evan
Evan

Reputation: 2635

Adding a value to a specific location in a list of queues

Queues:

    public class Queue
    {
        public Queue() { }

        public process Front() { return this.q_list.ElementAt(0); }
        public int Size { get { return this.q_list.Count; } }
        public bool IsEmpty { get { return this.q_list.Count <= 0 ? true : false; } }

        public void Enqueue(process proc) { this.q_list.Add(proc); } 
        public void Dequeue() { this.q_list.RemoveAt(0); }

        public List<process> q_list = new List<process>();
    };

Creation of a list:

    List<Queue> rr_list = new List<Queue>(); 

The process struct:

    public class process
    {
        public int Proc_a;
        public int Proc_b;
        public int Proc_Index;
    };

Let's say I want to add a process to the list at a specific location depending on the value of Proc_Index. How can I do that? Let's also assume the list is initially empty.

    process proc = new process{
         Proc_a = 1,
         Proc_b = 2,
         Proc_Index = 4 };

I want to add that to a queue that is in the list located at index 4.

Is this possible?

I've tried:

rr_list[proc.Proc_Index].Enqueue(proc); 

But it says there's an issue with index not being found or something.

The only thing I can thing of is initializing the list by adding empty queues for up to 20 indexes, but I don't know if there's a better way.

Upvotes: 0

Views: 210

Answers (3)

poke
poke

Reputation: 387775

A list is usually supposed to have no holes, so if you were to add an element at index 4 to an empty list, this would make indexes 0 to 3 contain null.

Now, you can do it like that. You could check if the length is bigger than the requested index, and if not, keep adding null values until it is. Then the index would exist, and you could assign something to it:

static void EnsureLength<T> (List<T> list, int index)
{
    while (list.Count <= index)
        list.Add(default(T));
}

Then you could use it like this:

List<int?> list = new List<int?>();

EnsureLength(list, 3);
list[3] = 123;

A possibly better way would be to simply use a Dictionary, especially if you know that you will have holes. So you would just have a Dictionary<int, T>:

Dictionary<int, int?> dict = new Dictionary<int, int?>();
dict[3] = 123;

Upvotes: 1

Smeegs
Smeegs

Reputation: 9224

You may want to use a dictionary instead of a list.

var rr_list = new Dictionary<int, Queue>(); 

Then have an addprocess function as such

function void AddProcess(proccess proc){
     if(rr_list.ContainsKey(proc.Proc_Index){
        rr_list[proc.Proc_Index].Enqueue(proc);
     } else {
        rr_list[proc.Proc_Index] = (new Queue()).Enqueue(proc); 
     }
}

Upvotes: 1

Dark Falcon
Dark Falcon

Reputation: 44181

You should use a System.Collections.Generic.Queue instead of writing your own. Use a System.Collections.Generic.Dictionary if you want key-value lookup.

var rr_list = new Dictionary<int, Queue<process>>();

process proc = new process{
     Proc_a = 1,
     Proc_b = 2,
     Proc_Index = 4 };

rr_list[proc.Proc_Index].Enqueue(proc); 

Upvotes: 1

Related Questions