From 9db78f57f9171e39ade41db33aca18db5654fc3e Mon Sep 17 00:00:00 2001 From: anirudherabelly Date: Mon, 15 Oct 2018 15:09:06 -0500 Subject: [PATCH] added important queue algos --- Data Structures/Queue/QueueReversal.java | 23 ++++++++++ Data Structures/Queue/TruckTour/TruckTour.cs | 47 ++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 Data Structures/Queue/QueueReversal.java create mode 100644 Data Structures/Queue/TruckTour/TruckTour.cs 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