how to remove a object from linked list in java?

后端 未结 7 903
花落未央
花落未央 2020-12-15 14:08

i have one problem with my code ,i did a sample program to display the emp details from a linked list,now the problem when i trying to delete a particular entry means it doe

7条回答
  •  清歌不尽
    2020-12-15 14:57

    import java.util.Scanner;

    // 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;
        }
    }
    
    /* Drier 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();
    
        Scanner sc = new Scanner(System.in);
        System.out.println("please enter input");
        int aa =sc.nextInt();
        llist.deleteNode(aa); // Delete node at position 4
    
        System.out.println("\nLinked List after Deletion at position :"+aa);
    
    
        llist.printList();
    
        System.out.println("adding element to the LinkedList");
    
        System.out.println("enter element");
        int dd = sc.nextInt();
        llist.push(dd);
    
        llist.printList();
    }
    

    }

提交回复
热议问题