コード例 #1
0
        public object Add(int index, object o)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException("index: " + index);

            if (index > count)
                index = count;

            Node current = this.head;

            if (this.Empty || index == 0)
            {
                this.head = new Node(o, this.head);
            }
            else
            {
                for (int i = 0; i < index - 1; i++)
                    current = current.Next;

                current.Next = new Node(o, current.Next);

            }

            count++;

            return o;
        }
コード例 #2
0
        public object Remove(int index)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException("index: "+index);

            if (this.Empty)
                return null;

            if (index > this.count)
                index = count - 1;

            Node current = this.head;
            object result = null;

            if (index == 0)
            {
                result = current.Data;
                this.head = current.Next;

            }
            else
            {
                for (int i = 0; i < index - 1; i++)
                    current = current.Next;

                result = current.Next.Data;

                current.Next = current.Next.Next;      //skips the reference to "deleted" node

            }
            count++;

            return result;
        }
コード例 #3
0
 public void Clear()
 {
     this.head = null;
     this.count = 0;
 }
コード例 #4
0
 public LinkedList()
 {
     this.head = null;
     this.count = 0;
 }
コード例 #5
0
 public Node(object data, Node next)
 {
     this.data = data;
     this.next = next;
 }