示例#1
0
 /// <summary>
 /// The indexer property
 /// </summary>
 /// <param name="index">The desired position</param>
 /// <returns></returns>
 public T this[int index]
 {
     get
     {
         CustomLinkedNode <T> desiredNode = headNode;
         if (index < 0 || index > count)
         {
             throw new IndexOutOfRangeException("This is an invalid index.");
         }
         else
         {
             for (int i = 0; i < index; i++)
             {
                 desiredNode = desiredNode.Next;
             }
             return(desiredNode.Data);
         }
     }
     set
     {
         CustomLinkedNode <T> desiredNode = headNode;
         if (index < 0 || index > count)
         {
             throw new IndexOutOfRangeException("This is an invalid index.");
         }
         else
         {
             for (int i = 0; i < index; i++)
             {
                 desiredNode = desiredNode.Next;
             }
             desiredNode.Data = value;
         }
     }
 }
示例#2
0
        /// <summary>
        /// Prints the list in reverse order
        /// </summary>
        public void PrintReversed()
        {
            CustomLinkedNode <T> currentNode = tailNode;

            for (int i = count - 1; i >= 0; i++)
            {
                Console.WriteLine(currentNode.Data);
                currentNode = currentNode.Previous;
            }
        }
示例#3
0
        /// <summary>
        /// This inserts an item at a desire position
        /// </summary>
        /// <param name="item">The item beind inserted</param>
        /// <param name="index">The desired position</param>
        public void Insert(T item, int index)
        {
            //This will be the node BEFORE the inserted node
            CustomLinkedNode <T> currentnode = headNode;

            //This checks for an invalid index
            if (index < 0 || index > count + 1)
            {
                throw new IndexOutOfRangeException("This is an invalid index.");
            }

            //This handles an empty list
            else if (count == 0)
            {
                CustomLinkedNode <T> newNode = new CustomLinkedNode <T>(item);
                headNode = newNode;
                tailNode = newNode;
            }

            //This handles a new head
            else if (index == 0)
            {
                CustomLinkedNode <T> newNode = new CustomLinkedNode <T>(item, headNode);
                headNode.Previous = newNode;
                headNode          = newNode;
            }

            //This handles a new tail
            else if (index == count - 1)
            {
                CustomLinkedNode <T> newNode = new CustomLinkedNode <T>(tailNode, item);
                tailNode.Next = newNode;
                tailNode      = newNode;
            }

            //This handles an index just after the end of the list
            else if (index == count)
            {
                Add(item);
            }

            //This handles indecies in the middle of the list
            else
            {
                for (int i = 0; i < index; i++)
                {
                    currentnode = currentnode.Next;
                }
                CustomLinkedNode <T> newNode = new CustomLinkedNode <T>(currentnode, item, currentnode.Next);
                currentnode.Next      = newNode;
                newNode.Next.Previous = newNode;
            }

            count++;
        }
示例#4
0
        /// <summary>
        /// Adds a new item to the list
        /// </summary>
        /// <param name="item">The item being added</param>
        public void Add(T item)
        {
            CustomLinkedNode <T> newNode = new CustomLinkedNode <T>(item);

            if (count > 0)
            {
                tailNode.Next    = newNode;
                newNode.Previous = tailNode;
            }
            else
            {
                headNode = newNode;
            }
            tailNode = newNode;
            count++;
        }
示例#5
0
 /// <summary>
 /// This creates a linked node that only links to the next node (Only used for head nodes)
 /// </summary>
 /// <param name="data">The data being held by the node</param>
 /// <param name="next">Links to the next node</param>
 public CustomLinkedNode(T data, CustomLinkedNode <T> next)
 {
     this.previous = null;
     this.data     = data;
     this.next     = next;
 }
示例#6
0
 /// <summary>
 /// This creates a linked node with two links
 /// </summary>
 /// <param name="previous">Links to the node before it</param>
 /// <param name="data">The data being held</param>
 /// <param name="next">Links to the node next to it</param>
 public CustomLinkedNode(CustomLinkedNode <T> previous, T data, CustomLinkedNode <T> next)
 {
     this.previous = previous;
     this.data     = data;
     this.next     = next;
 }
示例#7
0
 /// <summary>
 /// Creates a linked node that only links to the previous node (Only used for tail nodes)
 /// </summary>
 /// <param name="data">The data of the node</param>
 public CustomLinkedNode(CustomLinkedNode <T> previous, T data)
 {
     this.previous = previous;
     this.data     = data;
     this.next     = null;
 }
示例#8
0
 /// <summary>
 /// Creates a node with just the data
 /// </summary>
 /// <param name="data">The data in the node</param>
 public CustomLinkedNode(T data)
 {
     this.previous = null;
     this.data     = data;
     this.next     = null;
 }
示例#9
0
 /// <summary>
 /// The default constructor
 /// </summary>
 public CustomLinkedList()
 {
     headNode = null;
     count    = 0;
     tailNode = null;
 }
示例#10
0
 /// <summary>
 /// Clears the list
 /// </summary>
 public void Clear()
 {
     count    = 0;
     tailNode = null;
     headNode = null;
 }
示例#11
0
        /// <summary>
        /// This removes an item from the list and returns the removed item
        /// </summary>
        /// <param name="index">Index of the item being removed</param>
        /// <returns>The removed index</returns>
        public T Remove(int index)
        {
            //This is used to index the list
            CustomLinkedNode <T> currentNode = headNode;
            //This is holds the removed node so it can be returned after it is removed
            CustomLinkedNode <T> returnedNode = null;


            //Checks for a valid index
            if (index < 0 || index >= count)
            {
                throw new IndexOutOfRangeException("Sorry but the index " + index + " is out of range");
            }
            //Checks to see if the list has only 1 item
            else if (count == 1)
            {
                returnedNode = headNode;
                headNode     = null;
                tailNode     = null;
            }
            //Checks for the head
            else if (index == 0)
            {
                returnedNode      = headNode;
                headNode          = headNode.Next;
                headNode.Previous = null;
            }
            //Checks for the tail
            else if (index == count - 1)
            {
                returnedNode  = tailNode;
                tailNode      = tailNode.Previous;
                tailNode.Next = null;
            }
            //Index is in the middle of the list
            else
            {
                //This handles an index in the first half of the list
                if (index < count / 2)
                {
                    for (int i = 0; i < index; i++)
                    {
                        currentNode = currentNode.Next;
                    }
                    returnedNode = currentNode;
                    currentNode.Next.Previous = currentNode.Previous;
                    currentNode.Previous.Next = currentNode.Next;
                }
                //This handles an index in the second half of the list
                else
                {
                    currentNode = tailNode;
                    for (int i = count; i > index + 1; i--)
                    {
                        currentNode = currentNode.Previous;
                    }
                    returnedNode = currentNode;
                    currentNode.Next.Previous = currentNode.Previous;
                    currentNode.Previous.Next = currentNode.Next;
                }
            }

            count--;
            return(returnedNode.Data);
        }