How to copy queue to another queue in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;
                     
public class Program
{
    public static void PrintQueue(Queue<string> q) {
       foreach(string s in q) {
           Console.WriteLine(s);
       }
    }
    public static void Main()
    {
        Queue<string> q = new Queue<string>();
         
        q.Enqueue("c#");
        q.Enqueue("vb.net");
        q.Enqueue("java");
        q.Enqueue("php");
        q.Enqueue("c++");
         
		string[] arr = new string[q.Count * 2];
        q.CopyTo(arr, q.Count);

        Queue<string> q2 = new Queue<string>(arr);
		
		// Remove all the null elements 
		for (int i = 0; i < q.Count; i++)
			q2.Dequeue();
		
		PrintQueue(q2);
    }
}
 
 
/*
run:
 
c#
vb.net
java
php
c++
 
*/

 



answered May 5, 2020 by avibootz
0 votes
using System;
using System.Collections.Generic;
                     
public class Program
{
    public static void PrintQueue(Queue<string> q) {
       foreach(string s in q) {
           Console.WriteLine(s);
       }
    }
    public static void Main()
    {
        Queue<string> q = new Queue<string>();
         
        q.Enqueue("c#");
        q.Enqueue("vb.net");
        q.Enqueue("java");
        q.Enqueue("php");
        q.Enqueue("c++");
 
		Queue<string> q2 = new Queue<string>(q);
		
		PrintQueue(q2);
    }
}
 
 
/*
run:
 
c#
vb.net
java
php
c++
 
*/

 



answered May 5, 2020 by avibootz

Related questions

1 answer 153 views
153 views asked Apr 21, 2020 by avibootz
1 answer 178 views
1 answer 156 views
156 views asked Aug 15, 2022 by avibootz
1 answer 123 views
2 answers 193 views
193 views asked Jan 6, 2017 by avibootz
1 answer 160 views
1 answer 170 views
...