public void delete(string toDelete) { Node currNode = top; bool found = false; if (currNode != null) { //If there are items in the list, look for node to delete if (currNode.Equals(toDelete)) { //If the top node is the node to delete, set top to top.next top = currNode.getNext(); size--; } else { //If the top node is not the node to delete, check through the linked list and delete the node if found while (currNode != null && currNode.getNext() != null && found == false) { if (currNode.getNext().Equals(toDelete)) { currNode.setNext(currNode.getNext().getNext()); size--; found = true; } currNode = currNode.getNext(); } } } //If there are no items in the list, do nothing }
public void deleteFromFront() { if (top != null) { top = top.getNext(); size--; } }
public LinkedList() { top = null; size = 0; }
public void setNext(Node newNext) { next = newNext; }
public Node(string newData) { data = newData; next = null; }
public void insertAtFront(String data) { Node newNode = new Node(data); if (top == null) { //The LinkedList is empty so set top to the new node top = newNode; size++; } else { //The LinkedList contains elements, so set the newNode.next to the top and set top to the newNode newNode.setNext(top); top = newNode; size++; } }