/// <summary>
		/// Add a new Node to the list.
		/// </summary>
		public void Add(object content)
		{
			size++;

			// This is a more verbose implementation to avoid adding nodes to the head of the list
			var node = new Node()
			{
				NodeContent = content
			};
			if (head == null)
			{
				// This is the first node. Make it the head
				head = node;
			}
			else
			{
				// This is not the head. Make it current's next node.
				current.Next = node;
			}

			// Makes newly added node the current node
			current = node;


			// This implementation is simpler but adds nodes in reverse order. It adds nodes to the head of the list

			//head = new Node()
			//{
			//    Next = head,
			//    NodeContent = content
			//};

		}
Esempio n. 2
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
            {
                //Grab the node right before the insert
                for (int i = 0; i < index - 1; i++)
                    current = current.Next;

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

            }

            count++;

            return o;
        }
Esempio n. 3
0
 public void Clear()
 {
     this.head = null;
     this.count = 0;
 }
Esempio n. 4
0
 public LinkedList()
 {
     this.head = null;
     this.count = 0;
 }
Esempio n. 5
0
        public object Remove(int index)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException("Index: " + index.ToString());

            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;
                head = current.Next;
            }
            else
            {
                for (int i = 0; i < index - 1; i++)
                    current = current.Next;

                result = current.Next.Data;
                current.Next = current.Next.Next;
            }

            count--;
            return result;
        }
Esempio n. 6
0
 public Node(object data, Node next)
 {
     this.data = data;
     this.next = next;
 }
		public List()
		{
			size = 0;
			head = null;
		}
		/// <summary>
		/// Delete a Node in the specified position
		/// </summary>
		/// <param name="Position">Position of node to be deleted</param>
		/// <returns>Successful</returns>
		public bool Delete(int Position)
		{
			if (Position == 1)
			{
				head = null;
				current = null;
				return true;
			}

			if (Position > 1 && Position <= size)
			{
				Node tempNode = head;

				Node lastNode = null;
				int count = 0;

				while (tempNode != null)
				{
					if (count == Position - 1)
					{
						lastNode.Next = tempNode.Next;
						return true;
					}
					count++;

					lastNode = tempNode;
					tempNode = tempNode.Next;
				}
			}

			return false;
		}