diff --git a/Data Structures/Queue/QueueReversal.java b/Data Structures/Queue/QueueReversal.java new file mode 100644 index 00000000..0f8b4afb --- /dev/null +++ b/Data Structures/Queue/QueueReversal.java @@ -0,0 +1,23 @@ +class GfG{ + public Queue rev(Queue q){ + //add code here. + //revHelperRecursive(q); + revHelperIterative(q); + return q; + } + private void revHelperIterative(Queue q){ + Stack s=new Stack(); + while(!q.isEmpty()){ + s.push(q.poll()); + } + while(!s.empty()){ + q.offer(s.pop()); + } + } + private void revHelperRecursive(Queue q){ + if(q.isEmpty())return; + int temp=q.poll(); + revHelperRecursive(q); + q.offer(temp); + } +} \ No newline at end of file diff --git a/Data Structures/Queue/TruckTour/TruckTour.cs b/Data Structures/Queue/TruckTour/TruckTour.cs new file mode 100644 index 00000000..47d1155f --- /dev/null +++ b/Data Structures/Queue/TruckTour/TruckTour.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.IO; +class PetrolPump +{ + public int petrolGiven; + public int distanceToNext; + public PetrolPump(int PetrolGiven,int DistanceToNext) + { + this.petrolGiven = PetrolGiven; + this.distanceToNext = DistanceToNext; + } +} +class Solution { + static void Main(String[] args) { + /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ + int n = int.Parse(Console.ReadLine()); + PetrolPump[] q = new PetrolPump[n]; + for (int i = 0; i < n; i++) + { + int[] info = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse); + q[i]=new PetrolPump(info[0],info[1]); + } + TruckTour(q,n); + } + private static void TruckTour(PetrolPump[] q, int n) + { + int start = 0,end=1; + int curr_petrol= q[start].petrolGiven- q[start].distanceToNext; + while(curr_petrol<0 || start != end) + { + while(curr_petrol<0 && start != end) + { + curr_petrol -= (q[start].petrolGiven - q[start].distanceToNext); + start = (start + 1) % n; + if (start == 0) + { + Console.WriteLine("Not Possible"); + return; + } + } + curr_petrol += q[end].petrolGiven - q[end].distanceToNext; + end = (end + 1) % n; + } + Console.WriteLine(start); + } +} \ No newline at end of file