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; } }
public void setNextNode(Node n) { next = n; }
public Node(Node n, string s) { data = s; next = n; }
public Node(string s) { data = s; next = null; }
public Node() { data = null; next = null; }
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; }