Results 1 to 5 of 5

Thread: How to remove an item from LinkedList in java program?

  1. #1
    Join Date
    Nov 2009
    Posts
    47

    How to remove an item from LinkedList in java program?

    Hello to all,
    I am last year B.Sc.I.T. student. In our last exam in java paper one question has asked like How to remove an item from LinkedList in java program? I tried various tricks to solve this program but unable to get solution. Can anyone know how to write this program. Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to remove an item from LinkedList in java program?

    Hey it is very easy process to remove elements from Linked list. You have to just take index number and then use this index to remove that element. I have written following example to do that. Just go through it.


    Code:
    package org.kodejava.example.util;
    
    import java.util.LinkedList;
    
    public class LinkedListRemove {
        public static void main(String[] args) {
            LinkedList<String> names1 = new LinkedList<String>();
            names1.add("komal");
            names1.add("ran");
            names1.add("koral");
            names1.add("limaye");
    
            System.out.println("Original values are:");
            System.out.println("====================");
            for (String name : name1) {
                System.out.println("Name = " + name1);
            }
    
            
            namess.remove(2);
            
            System.out.println("New values are:");
            System.out.println("====================");
            for (String name : names1) {
                System.out.println("Name = " + name1);
            }
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to remove an item from LinkedList in java program?

    It is very e4asy process to remove an item from LinkedList in java program. Just use following code in your java program and then try to run it in your computer. Just pass any value you want to remove from Linked list.


    Code:
       
          public boolean removeSpecific(Item things)
    
          {
      
          if(heads != null)
       
          {
      
          if(previouss == null)
     
          heads = heads.next;
     
          else if(current.data.equals(things))
    
          {
     
          previouss.next = currentss.next;
    
          currents = currents.next;
    
          counts--;
    
          return true;
    
          }
    
          }
    
          return false;
    
          }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to remove an item from LinkedList in java program?

    Just try to understand following program. I have written that program for you. It is very easy process to remove any specific item from linked list. Just insert index number of that particular elements and then it automatically remove from linked list.


    Code:
     
          public void addNode(int coeffs, int powers)
     
          {
    
          if (heads == null)
      
          {
     
          heads = new Node(coeffs, powers, null);
      
          degrees = powers;
      
          }
    
          else
    
          {
      
          Node p = heads;
    
          Node lasts = null;
    
           
    
          while (p != null)
    
          {
    
     
          {
    
          if (lasts == null)
    
          heads = new Node(coeffs, powers, p);
    
          else
     
          lasts.next = new Node(coeffs, powers, p);
      
          degrees = powers;
    
          break;
    
          }
      
          last = p;
    
          p = p.next;
    
          }
     
          if (p == null) 
          {
    
          lasst.next = new Node(coeffs, powers, null);
    
          }
     
          }
     
          }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to remove an item from LinkedList in java program?

    You have to define one class which must have delete() method, which will delete all nodes equal() to Node n and delete() method should return true if at least one node could be removed, and return false otherwise. You have to use Use Java syntax. class. You have to use index number to delete specific element from Linked list.

Similar Threads

  1. Java program to reverse the order of LinkedList elements?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:44 PM
  2. How to sort LinkedList elements using java program?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 08:45 PM
  3. Java program to remove JTree default icons?
    By Baazigar in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 07:35 PM
  4. How to remove a node from JTree in java program?
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 05:41 PM
  5. Replies: 2
    Last Post: 19-01-2007, 12:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,056,728.97446 seconds with 17 queries