All pastes #2103803 Raw Edit

Enkel lnkad lista

public text v1 · immutable
#2103803 ·published 2012-01-18 13:47 UTC
rendered paste body
public class MyList<T>
{
    private MyListNode<T> first, last;
    private int _length;
    
    public int Length {get {return _length;}}
    
    public void Add(T item) {
        var node = new MyListNode<T> {Value = item};
        ++_length;
        if(first == null) {
            first = node;
            last = node;
            return;
        }
        last.Next = node;
        last = node;
    }
    
    public T Get(int pos) {
        if(pos >= _length) return null;
        var current = first;
        for(int i = 0; i < pos; ++i) {
            current = current.Next;
        }
        return current.Value;
    }
    
    private class MyListNode<T> {
        public T Value {get; set;}
        public MyListNode<T> Next {get; set;}
    }
}