从自定义LinkedList中删除一个元素的所有实例



我试图从列表中删除所有在我的自定义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;
}

}
<<p>LinearNode类/strong>
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();
}
}

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

这个示例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(..)只有在比较的对象有equals()方法并检查其内容相等时才为真,否则两个对象通过==运算符相等,如果它们完全相同(不是因为有内容)。

最新更新