-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrainComposition.cs
46 lines (39 loc) · 1.02 KB
/
TrainComposition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Linq;
using System.Collections.Generic;
public class TrainComposition
{
private LinkedList<int> train;
public TrainComposition()
{
this.train = new LinkedList<int>();
}
public void AttachWagonFromLeft(int wagonId)
{
this.train.AddFirst(wagonId);
}
public void AttachWagonFromRight(int wagonId)
{
this.train.AddLast(wagonId);
}
public int DetachWagonFromLeft()
{
var node = this.train.First;
this.train.Remove(node);
return node.Value;
}
public int DetachWagonFromRight()
{
var node = this.train.Last;
this.train.Remove(node);
return node.Value;
}
public static void Main(string[] args)
{
TrainComposition train = new TrainComposition();
train.AttachWagonFromLeft(7);
train.AttachWagonFromLeft(13);
Console.WriteLine(train.DetachWagonFromRight()); // 7
Console.WriteLine(train.DetachWagonFromLeft()); // 13
}
}