Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Oct 8, 2022, 3:23 AM EDT
Programe 1 - Advance
using System; using System.Collections; public class DEMO{ // Function to remove an element from // the queue static Queue q = new Queue(); static void remove_(int t) { // Helper queue to store the elements // temporarily. Queue reff = new Queue(); int s = q.Count; int cnt = 0; // Finding the value to be removed while ((int)q.Count != 0 && (int)q.Peek() != t) { reff.Enqueue(q.Peek()); q.Dequeue(); cnt++; } // If element is not found if (q.Count == 0) { Console.WriteLine("element not found!!"); while (reff.Count != 0) { // Pushing all the elements back into q q.Enqueue(reff.Peek()); reff.Dequeue(); } } // If element is found else { q.Dequeue(); while (reff.Count != 0) { // Pushing all the elements back into q q.Enqueue(reff.Peek()); reff.Dequeue(); } int k = s - cnt - 1; while (k-- >0) { // Pushing elements from front of q to its back int p = (int)q.Peek(); q.Dequeue(); q.Enqueue(p); } } } // Function to print all the elements // of the queue. static void print() { Queue qr = (Queue)q.Clone(); while (qr.Count != 0) { Console.Write(qr.Peek()+ " "); qr.Dequeue(); } Console.WriteLine(); } // Driver Code static public void Main (){ // Pushing into the queue q.Enqueue(10); q.Enqueue(20); q.Enqueue(30); q.Enqueue(40); q.Enqueue(50); q.Enqueue(60); print(); // Removing 39 from the queue remove_(39); print(); // Removing 30 from the queue remove_(30); print(); } } // This code is contributed by Dharanendra L V.Output
10 20 30 40 50 60
element not found!!
10 20 30 40 50 60
10 20 40 50 60
Programe 2 - Simple
using System; using System.Collections.Generic; class DEMO { static void Main() { Queue months = new Queue(); months.Enqueue("January"); months.Enqueue("February"); months.Enqueue("March"); months.Enqueue("April"); months.Enqueue("May"); months.Enqueue("June"); Console.WriteLine("Queue Items Before Clear() : {0}", string.Join(",", months.ToArray())); months.Clear(); Console.WriteLine("Queue Items After Clear() : {0}", string.Join(",", months.ToArray())); } }Output
Queue Items Before Clear() : January,February,March,April,May,June
Queue Items After Clear() :
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 8, 2022, 5:13 AM EDT
Removes and returns the object at the beginning of the Queue. For example,
namespace Queue { classProgram { staticvoid Main(string[] args) { Queue queue1 = newQueue (); queue1.Enqueue("MCA"); queue1.Enqueue("MBA"); queue1.Enqueue("BCA"); queue1.Enqueue("BBA"); Console.WriteLine("The elements in the queue are:"); foreach(string s in queue1) { Console.WriteLine(s); } queue1.Dequeue(); //Removes the first element that enter in the queue here the first element is MCA queue1.Dequeue(); //Removes MBA Console.WriteLine("After removal the elements in the queue are:"); foreach(string s in queue1) { Console.WriteLine(s); } } } }