Ejemplo n.º 1
0
        public bool AddAt(string newEntry, int position)
        {
            Node tmp = headNode;
            if (position >= 0)
            {
                Node newNode = new Node(newEntry);
                Node tmp2 = new Node();
                if (isEmpty() || position == 0)                         // adding Node at the begining of the List
                {
                    newNode.setNextNode(tmp);
                    headNode = newNode;
                    return true;
                }
                else if (position == Length())                          // adding Node at the end of the List
                {
                    for (int i = 0; i < (position - 1); i++)
                    {
                        tmp = tmp.getNextNode();
                    }
                    tmp.setNextNode(newNode);
                    return true;
                }
                else                                                    // inserting node between first and last node
                {
                    for (int i = -1; i < (position - 1) & tmp != null; i++)
                    {
                        if (i == (position - 2))
                        {
                            tmp2 = tmp;
                        }
                        tmp = tmp.getNextNode();

                    }
                    if (tmp == null)                                    // or if (position > Length())
                    {
                        return false;
                    }
                    newNode.setNextNode(tmp);
                    tmp2.setNextNode(newNode);
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
Ejemplo n.º 2
0
 public void setNextNode(Node n)
 {
     next = n;
 }
Ejemplo n.º 3
0
 public Node(Node n, string s)
 {
     data = s;
     next = n;
 }
Ejemplo n.º 4
0
 public Node(string s)
 {
     data = s;
     next = null;
 }
Ejemplo n.º 5
0
 public Node()
 {
     data = null;
     next = null;
 }
Ejemplo n.º 6
0
        public string RemoveAt(int position)
        {
            string tmpObject = null;
            if (isEmpty() || position < 0)
            {
                return null;
            }

            Node currentNode = headNode;

            if (position == 0)
            {
                tmpObject = currentNode.getData();
                headNode = currentNode.getNextNode();
            }
            else
            {
                for (int i = -1; i < position - 2 && currentNode != null; i++)
                {
                    currentNode = currentNode.getNextNode();
                }
                if (currentNode == null)                                       // or  if (position > Length())
                {
                    return null;
                }
                tmpObject = currentNode.getNextNode().getData();
                currentNode.setNextNode(currentNode.getNextNode().getNextNode());
            }
            return tmpObject;
        }