# Implement Four way stop

• interface FourWayStop {
/** Adds a car to the intersection. This does not mean the car gets to the stop line. /
/
* Returns the ID of the car to cross the intersection next. Removes the car. */
int remove();
}

Let us simulate four way stop scenario and implement those two methods.

• @Sara can maintain the queue of cars coming to intersection. Whats the use of roadId?

• Hi , here is my solution,
using two list, one is for carid, one is for roadid for each car in car list.
eachtime adding new car, will calculate an index.

``````public class StopSign : FourWayStop {

// 0 : up
// 1: right
// 2: down
// 3: left
private int[] indexs = new int[4];

private List<int> queue = new List<int>();
private List<int> indexQueue = new List<int>();

{
int index = 0;
for(int i = 0;i<4;i++){
index+=indexs[i]*4;
index++;
}
}
queue.Insert(index,cardId);
}

public int remove()
{
if(queue.Count ==0 )
return -1;

int returnVal = queue[0];

indexs[indexQueue[0]]--;
queue.RemoveAt(0);
indexQueue.RemoveAt(0);
return returnVal;
}
``````

}
...

• ``````class FourWayStop {
// carQ record the cars on each road
list<int> carQ[4];
public:
// Adds a car to the intersection. This does not mean the car gets to the stop line.
// if car pull up to the front, this road enters roadQ
}
// this car enters carQ of the road
}
// Returns the ID of the car to cross the intersection next. Removes the car.
int remove() {