Skip to content
Advertisement

Removing all instances of an element from a custom LinkedList

Im trying to remove all people from the list who have the same course name in my custom LinkedList class. I have managed to get my programme to delete people individually based on number however can not figure out how to remove multiple at once. I have browsed online for any solutions and have tried multiple so far but none to any success I also attempted one myself but also no success any help or links to were I could learn mode would be greatly appreciated. Below is my Driver, LinkedList, and LinearNode class. I have also removed code I beleive is not relevant to this solution :).

Linked List Class


public class LinkedList<T> implements LinkedListADT<T> {
    
     private int count;  // the current number of elements in the list
     private LinearNode<T> list; //pointer to the first element 
     private LinearNode<T> last; //pointer to the last element 
     
     //-----------------------------------------------------------------
     //  Creates an empty list.
     //-----------------------------------------------------------------
        public LinkedList()
        {
           this.count = 0;
           this.last = null;
           this.list = null;
        }
       public void add (T element)
       {      
           LinearNode<T> node = new LinearNode<T> (element); 
       
           if (size() == 0) {  
                this.last = node; // This is the last and the 
                this.list = node; // first node
                this.count++;
           }//end if
           else
             { 
                  last.setNext(node); // add node to the end of the list
                  last = node; // now make this the new last node.
                  this.count++;   
              } //end else
       }
       
       public T remove()
       {
           LinearNode<T> current = list;
           LinearNode<T> temp = list;
           T result = null;
          
            if (current == null) {
                System.out.println("There are no such employees in the list");
            }//end if
            else {
            
                result = this.list.getElement();
                temp = list;
                this.list = this.list.getNext();
                temp.setNext(null); //dereference the original first element
                count--;
            }//end else
            return result;

       }
       
       public T remove(T element) 
       {
           LinearNode<T> current = list;
           LinearNode<T> previous = list;
           LinearNode<T> temp;
           T result = null;
           
            if (current == null) {
                
                System.out.println("There are no such employees in the list");
            }//end if
            else {
                
                for (current = this.list; current != null && !current.getElement().equals(element); current = current.getNext())
                   {
                    previous = current;
                
                   }
                if(current == null) {
                    System.out.println("No such employee on the list");
                }
                else if (current == list)
                {
                    remove();
                }
                else if(current == last) {
                    previous.setNext(null); 
                    this.last = previous.getNext();
                    
                    count--;
                }
                else
                {
                    previous.setNext(current.getNext());
                    count--;
                }
                
            }
            
            return result;
       }
**
  My attempted Solution**
       
       public T clear(T element) {
           T result = null;
            while (this.list != null && this.list.getElement() == element) {
                this.list = this.list.getNext();
             count--;
            }

            if (this.list == null) {
                return result;
            }

            LinearNode<T> current = this.list;
            while (current.getNext() != null) {
                if (current.getNext().getElement() == element) {
                    current.setNext(current.getNext());
                    count--;
                } else {
                    current = current.getNext();
                }
                
            }
            return result;
        }
      
}

LinearNode Class

public class LinearNode<T>
{
   private LinearNode<T> next;
   private T element;

   //---------------------------------------------------------
   //  Creates an empty node.
   //---------------------------------------------------------
   public LinearNode()
   {
      this.next = null;
      this.element = null;
   }

   //---------------------------------------------------------
   //  Creates a node storing the specified element.
   //---------------------------------------------------------
   public LinearNode (T elem)
   {
      this.next = null;
      this.element = elem;
   }

   //---------------------------------------------------------
   //  Returns the node that follows this one.
   //---------------------------------------------------------
   public LinearNode<T> getNext()
   {
      return this.next;
   }

   //---------------------------------------------------------
   //  Sets the node that follows this one.
   //---------------------------------------------------------
   public void setNext (LinearNode<T> node)
   {
      this.next = node;
   }

   //---------------------------------------------------------
   //  Returns the element stored in this node.
   //---------------------------------------------------------
   public T getElement()
   {
      return this.element;
   }

   //---------------------------------------------------------
   //  Sets the element stored in this node.
   //---------------------------------------------------------
   public void setElement (T elem)
   {
      this.element = elem;
   }
}

Driver Class

public class TrainingCourses {

    LinkedList<employee>list;
    int Size =10;
     int numberofEmployees=0;
    
    
    
    public TrainingCourses() {
     list = new LinkedList<employee>();
    
    

     inputEmployee();
     displayEmployee();
     deleteCourses();
     displayEmployee();

         
          
    }
    

    
    public void inputEmployee() {
        employee a;
        a = null;
        String number,name,courseName = null;
        int years;
        
        
        
        Scanner scan = new Scanner(System.in);
        
        
    
         for (int count = 1; count<=numberofEmployees; count++){
        System.out.println("Input employee number");
        number = scan.nextLine();
        System.out.println("Input employee name");
        name = scan.nextLine();
        System.out.println("Input years at organisation");
        years = scan.nextInt(); scan.nextLine();
        if(years >=5) {
        System.out.println("Input course name");
        courseName = scan.nextLine();
        }else {
            System.out.println("Can not join training course employee must be with organisation 5 or more years");
        
        }
        
        a = new employee(number,name,years,courseName);
        
        
        list.add(a);
    
            }
    
    }
    public void displayEmployee() {
         System.out.println("nDisplaying all employees....");

            System.out.println(list.toString());
        
    }
    
    

        
    public void deleteCourses(){
         {
             Scanner scan = new Scanner(System.in);
                employee b = null;
                String number,name,courseName;
                int years;

                System.out.println("Enter employee number you wish to remove");
                number = scan.nextLine();
                System.out.println("Input employee name");
                name = scan.nextLine();
                System.out.println("Input years at organisation");
                years = scan.nextInt(); scan.nextLine();
                System.out.println("Input course name");
                courseName = scan.nextLine();
                
                b = new employee(number,name,years,courseName);
                  
                list.clear(b);
               
        }
            
        
    }
    public static void main(String[]args) {
        new TrainingCourses();
    }
}

Advertisement

Answer

I don’t understand exactly what you want, because of you wrote you want “to remove all people from the list who have the same course name”, but your code never checks only property, your code checks equality everywhere.

This example clear function removes all elements equals to param and returns count of removed elements.

public long clear(T element) {
    long result = 0L;
    
    LinearNode<T> current = this.list;
    LinearNode<T> previous = null;
    while (current != null) {
        if (current.getElement().equals(element)) {
            if (previous != null) {
                if (current.getNext() != null) {
                    previous.setNext(current.getNext());
                } else {
                    this.last = previous;
                }
            } else if (current.getNext() != null) {
                this.list = current.getNext();
            } else {
                this.list = this.last = null;
            }
            this.count--;
            result++;
        } else {
            previous = current;
        }
        current = current.getNext();
    }

    return result;
}

And after all .equals(..) only true, if the compared Objects has an equals() method and checks its content equality, otherwise two Objects equals by == operator, if they are exactly the same (not by there’s contents).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement