コード例 #1
0
        public void AddAt(int index, int value)
        {
            if (index < 0 || index >= size)
            {
                throw new IndexOutOfRangeException();
            }
            Node1 newNode = new Node1(value);

            if (index == 0)
            {
                AddFirst(value);
            }
            if (index == size - 1)
            {
                AddLast(value);
            }
            Node1 currentNode = first;
            Node1 prevNode    = first;

            for (int i = 0; i < index; i++)
            {
                prevNode    = currentNode;
                currentNode = currentNode.Next;
            }

            prevNode.Next = currentNode;
            newNode.Next  = currentNode.Next.Next;
            currentNode   = newNode;
            size++;
        }
コード例 #2
0
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= size)
            {
                throw new IndexOutOfRangeException();
            }

            if (index == size - 1)
            {
                RemoveLast();
                return;
            }

            if (index == 0)
            {
                RemoveFirst();
                return;
            }

            Node1 currentNode = first;
            Node1 temp        = first;

            for (int i = 0; i < index; i++)
            {
                temp        = currentNode;
                currentNode = currentNode.Next;
            }
            temp.Next = currentNode.Next;
            size--;
        }
コード例 #3
0
        public void RemoveAll(int value)
        {
            Node1 currentNode  = first;
            Node1 previousNode = null;

            while (currentNode != null)
            {
                if (currentNode.Value == value)
                {
                    if (previousNode != null)
                    {
                        previousNode.Next = currentNode.Next;
                        if (currentNode.Next == null)
                        {
                            last = previousNode;
                        }
                    }
                    else
                    {
                        first = first.Next;

                        if (first == null)
                        {
                            last = null;
                        }
                    }
                    size--;
                }
                previousNode = currentNode;
                currentNode  = currentNode.Next;
            }
        }
コード例 #4
0
        public int Get(int index)
        {
            Node1 currentNode = first;

            for (int i = 0; i < index; i++)
            {
                currentNode = currentNode.Next;
            }

            return(currentNode.Value);
        }
コード例 #5
0
        public void Reverse()
        {
            Node1 currentNode = first;


            while (currentNode.Next != null)
            {
                Node1 nextNode = currentNode.Next;
                currentNode.Next = nextNode.Next;
                nextNode.Next    = first;
                first            = nextNode;
            }
        }
コード例 #6
0
        public void RemoveFirst()
        {
            if (size != 0)
            {
                first = first.Next;

                size--;

                if (size == 0)
                {
                    last = null;
                }
            }
        }
コード例 #7
0
        public int IndexOf(int value)
        {
            Node1 currentNode = first;

            for (int i = 0; i < size; i++)
            {
                if (currentNode.Value.Equals(value))
                {
                    return(i);
                }
                currentNode = currentNode.Next;
            }
            return(-1);
        }
コード例 #8
0
        public int[] ToArray()
        {
            int[] array       = new int[size];
            int   index       = 0;
            Node1 currentNode = first;

            while (currentNode != null)
            {
                array[index] = currentNode.Value;
                currentNode  = currentNode.Next;
                index++;
            }
            return(array);
        }
コード例 #9
0
        public bool Contains(int value)
        {
            Node1 currentNode = first;

            while (currentNode != null)
            {
                if (currentNode.Value.Equals(value))
                {
                    return(true);
                }
                currentNode = currentNode.Next;
            }
            return(false);
        }
コード例 #10
0
        public void Set(int index, int value)
        {
            if (index < 0 || index >= size)
            {
                throw new IndexOutOfRangeException();
            }

            Node1 currentNode = first;

            for (int i = 0; i < index; i++)
            {
                currentNode = currentNode.Next;
            }
            currentNode.Value = value;
        }
コード例 #11
0
        public void AddLast(int value)
        {
            Node1 newNode1 = new Node1(value);

            if (first == null)
            {
                first = newNode1;
            }
            else
            {
                last.Next = newNode1;
            }

            last = newNode1;
            size++;
        }
コード例 #12
0
        public void AddFirst(int value)
        {
            Node1 newNode1 = new Node1(value);

            if (first == null)
            {
                first = newNode1;
                last  = newNode1;
            }
            else
            {
                newNode1.Next = first;
            }

            first = newNode1;
            size++;
        }
コード例 #13
0
        public void RemoveLast()
        {
            if (size != 0)
            {
                if (size == 1)
                {
                    first = null;
                    last  = null;
                }
                else
                {
                    last = null;
                }

                size--;
            }
        }
コード例 #14
0
        public void AddLast(int[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                Node1 newNode1 = new Node1(values[i]);
                if (first == null)
                {
                    first = newNode1;
                }
                else
                {
                    last.Next = newNode1;
                }

                last = newNode1;
                size++;
            }
        }
コード例 #15
0
        public void AddFirst(int[] values)
        {
            for (int i = values.Length - 1; i >= 0; i--)
            {
                Node1 newNode1 = new Node1(values[i]);
                if (first == null)
                {
                    first = newNode1;
                    last  = newNode1;
                }
                else
                {
                    newNode1.Next = first;
                }

                first = newNode1;
                size++;
            }
        }
コード例 #16
0
 public void Clear()
 {
     size  = 0;
     first = null;
     last  = null;
 }