Skip to content

Commit

Permalink
added important queue algos
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudherabelly committed Oct 15, 2018
1 parent ce2530e commit 9db78f5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Data Structures/Queue/QueueReversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class GfG{
public Queue<Integer> rev(Queue<Integer> q){
//add code here.
//revHelperRecursive(q);
revHelperIterative(q);
return q;
}
private void revHelperIterative(Queue<Integer> q){
Stack<Integer> s=new Stack<Integer>();
while(!q.isEmpty()){
s.push(q.poll());
}
while(!s.empty()){
q.offer(s.pop());
}
}
private void revHelperRecursive(Queue<Integer> q){
if(q.isEmpty())return;
int temp=q.poll();
revHelperRecursive(q);
q.offer(temp);
}
}
47 changes: 47 additions & 0 deletions Data Structures/Queue/TruckTour/TruckTour.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 9db78f5

Please sign in to comment.