public void swap(Node x, Node y) {
    int temp;

    temp = x.data;
    x.data = y.data;
    y.data = temp;
  }
Beispiel #2
0
  /**
   * Deletes the node specified by the parameter toDelete. parent specifies the parent of the node
   * to be deleted.
   */
  private void deleteNode(Node toDelete, Node parent) {
    if (toDelete.left != null && toDelete.right != null) {
      // Case 3: toDelete has two children.
      // Find a replacement for the item we're deleting -- as well as
      // the replacement's parent.
      // We use the smallest item in toDelete's right subtree as
      // the replacement.
      Node replaceParent = toDelete;
      Node replace = toDelete.right;
      while (replace.left != null) {
        replaceParent = replace;
        replace = replace.left;
      }

      // Replace toDelete's key and data with those of the
      // replacement item.
      toDelete.key = replace.key;
      toDelete.data = replace.data;

      // Recursively delete the replacement item's old node.
      // It has at most one child, so we don't have to
      // worry about infinite recursion.
      deleteNode(replace, replaceParent);
    } else {
      // Cases 1 and 2: toDelete has 0 or 1 child
      Node toDeleteChild;
      if (toDelete.left != null) toDeleteChild = toDelete.left;
      else toDeleteChild = toDelete.right; // null if it has no children

      if (toDelete == root) root = toDeleteChild;
      else if (toDelete.key < parent.key) parent.left = toDeleteChild;
      else parent.right = toDeleteChild;
    }
  }
Beispiel #3
0
 public void deleteNode(Node node) {
   if (node.next != null) {
     node.data = node.next.data;
     node.next = node.next.next;
   } else {
     throw new NoSuchElementException();
   }
 }
	public void deleteElement(int data){
		Node current = head;
		if(head==null)
			System.out.println("Empty list");
		else
		{
			while(current.next!=null){
				if(current.data==data){
					System.out.println("Element found and deleted");
					current.data = current.next.data;
					current.next = current.next.next;
					return;
				}
					
				current = current.next;
			}			
		}
	}
Beispiel #5
0
 public static void swap(Node first, Node second) {
   int tmp = first.data;
   first.data = second.data;
   second.data = tmp;
 }