【问题标题】:Removing all instances of an element from a custom LinkedList从自定义 LinkedList 中删除元素的所有实例
【发布时间】:2022-11-21 05:29:34
【问题描述】:

我试图从列表中删除所有在我的自定义 LinkedList 类中具有相同课程名称的人。我已经设法让我的程序根据人数单独删除人,但无法弄清楚如何一次删除多个人。我在网上浏览过任何解决方案,到目前为止已经尝试了多种解决方案,但都没有成功我自己也尝试过一个,但也没有成功任何帮助或链接到我可以学习的模式将不胜感激。下面是我的 Driver、LinkedList 和 LinearNode 类。我还删除了我认为与此解决方案无关的代码:)。

链表类



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;
        }
      
}

线性节点类

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;
   }
}

司机班


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();
    }
}

【问题讨论】:

    标签: java node.js linked-list element


    【解决方案1】:

    我不明白你到底想要什么,因为你写了你想要的“从列表中删除所有具有相同课程名称的人”,但您的代码绝不会只检查属性,您的代码会在所有地方检查是否相等。

    此示例 clear 函数删除所有等于 param 的元素并返回已删除元素的计数。

    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;
    }
    

    毕竟 .equals(..) 只有 true,如果比较的对象有一个 equals() 方法并检查其内容是否相等,否则两个对象等于 == 运算符,如果它们完全相同(不是有内容)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-25
      • 2013-03-23
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多