Home > OS >  Trying to write a function to delete numbers larger than x in a linked List
Trying to write a function to delete numbers larger than x in a linked List

Time:11-06

public void deleteElementsGreaterThan(int x) {
  MyListElement f = head;
  for (int j = 0; j < numberOfElements; j  ) {


     if (f.z > x) {
       ?;
     }
     f = f.next;
  }

  }

In place of the question mark I don't know what code is needed to delete the number. Ive got head, tail, next, prev, and z as the int defined.

CodePudding user response:

This is a quick implementation of your problem. In a Nutshell I first test if the current head is greater than the max value. If that's the case I delete it out of the list by deleting it as prev element of its next Cell. Afterwards, I always look at the next Element and delete it out of the list by setting the next element of the current head as the next of the next. This should find all Elements, but i couldn't test it till now

public void deleteElementGT(int x){
    Element currentHead = head; 
    //Delete head if greater:
    while(currentHead.z > x){

    // If head is only Element return
        if(currentHead.next == null){
           currentHead = null;
           return;
        currentHead = currentHead.next;
        currentHead.prev = null;
    }
    // Loop over all Elements
    while(currentHead.next != null){
        // If the next Element in the List is greater x skip it, if next is last just delete 
        while(currentHead.next.z >= x){
            if(currentHead.next.next == null){
                // detatch the current head as the previous of the next and detach the next as next of the head
                currentHEad.next.prev = null;
                currentHead.next = null;
            }
            else {
            currentHead.next = currentHead.next.next;
            currentHead.next.prev = null;
            }
        }
        currentHead = currentHead.next;
    }
}

CodePudding user response:

Here is an implementation of a linked list, which is likely to be similar to yours. I've added some extra methods for clarity, so you can solve your problem faster.

// A complete working Java program
// to demonstrate deletion
// in singly linked list
class LinkedList {
    Node head; // head of list
 
    /* Linked list Node*/
    class Node {
        int data;
        Node next;
        Node(int d)
        {
            data = d;
            next = null;
        }
    }
 
    /* Given a key, deletes the first
       occurrence of key in
     * linked list */
    void deleteNode(int key)
    {
        // Store head node
        Node temp = head, prev = null;
 
        // If head node itself holds the key to be deleted
        if (temp != null && temp.data == key) {
            head = temp.next; // Changed head
            return;
        }
 
        // Search for the key to be deleted, keep track of
        // the previous node as we need to change temp.next
        while (temp != null && temp.data != key) {
            prev = temp;
            temp = temp.next;
        }
 
        // If key was not present in linked list
        if (temp == null)
            return;
 
        // Unlink the node from linked list
        prev.next = temp.next;
    }
 
    /* Inserts a new Node at front of the list. */
    public void push(int new_data)
    {
        Node new_node = new Node(new_data);
        new_node.next = head;
        head = new_node;
    }
 
    /* This function prints contents of linked list starting
       from the given node */
    public void printList()
    {
        Node tnode = head;
        while (tnode != null) {
            System.out.print(tnode.data   " ");
            tnode = tnode.next;
        }
    }
 
    /* Driver program to test above functions. Ideally this
    function should be in a separate user class. It is kept
    here to keep code compact */
    public static void main(String[] args)
    {
        LinkedList llist = new LinkedList();
 
        llist.push(7);
        llist.push(1);
        llist.push(3);
        llist.push(2);
 
        System.out.println("\nCreated Linked list is:");
        llist.printList();
 
        llist.deleteNode(1); // Delete node with data 1
 
        System.out.println(
            "\nLinked List after Deletion of 1:");
        llist.printList();
    }
}

Here's an article, which explains the algorithm and idea behind this approach.

If this answer does not fully solve your problem, please, leave a comment below and I'll respond to it.

  •  Tags:  
  • java
  • Related