如何解决双向链表的各种删除方法不起作用(java)(deleteEven(); deleteMultiple())
我正在练习删除双向链表中元素的新方法。
我实现了一个方法 deleteEven()
删除列表中的所有偶数值,deleteHigher(int value)
删除列表中所有大于参数中的值的值,deleteMultiple(int value)
删除所有值参数中给定值的倍数的列表。
我没有收到错误,只是没有删除所有想要的值。
任何帮助表示赞赏。 (我还是编程新手)
这是我的代码:
public class List {
class Element{
int data;
Element next;
Element previous;
Element(int value){
this.data = value;
this.next = null;
this.previous = null;
}
}
private Element head;
private Element rear;
public List() {
this.head = null;
this.rear = null;
}
public List(List a) {
this();
if(a.head == null)
return;
Element cur = a.head;
Element tmp = new Element(cur.data);
head = rear = tmp;
cur = cur.next;
while(cur != null) {
tmp = new Element(cur.data);
rear.next = tmp;
tmp.previous = rear;
rear = tmp;
cur = cur.next;
}
}
public List List1(List list,int value) {
Element cur = list.head;
List a = new List();
if(list.head == null)
return a;
while(cur != null) {
if(list.head.data < value)
a.insert(list.head.data);
cur = cur.next;
}
return a;
}
public String toString() {
String str;
Element cur = this.head;
if(this.head == null)
str = "The linked list is empty";
else {
str = "The linked list contains ";
while(cur != null) {
str += cur.data + " | ";
cur = cur.next;
}
}
return str;
}
public int getLength() {
Element cur = this.head;
int c = 0;
if(this.head == null)
return 0;
while(cur != null) {
c++;
cur = cur.next;
}
return c;
}
public int countOdd() {
Element cur = this.head;
int c = 0;
if(this.head == null)
return 0;
while(cur != null) {
if(cur.data % 2 != 0)
c++;
cur = cur.next;
}
return c;
}
public boolean insert(int value) {
Element tmp = new Element(value);
Element cur = this.head;
if(this.isInList(value))
return false;
if(this.head == null) {
this.head = this.rear = tmp;
return true;
}
if(this.head.data < value) {
tmp.next = this.head;
this.head.previous = tmp;
this.head = tmp;
return true;
}
if(this.rear.data > value) {
tmp.previous = this.rear;
this.rear.next = tmp;
this.rear = tmp;
return true;
}
while(cur.next != null && cur.next.data > value) {
cur = cur.next;
}
if(cur.data == tmp.data)
return false;
tmp.next = cur.next;
tmp.previous = cur;
cur.next.previous = tmp;
cur.next = tmp;
return true;
}
public boolean isInList(int value) {
Element cur = this.head;
if(this.head == null)
return false;
while(cur != null) {
if(cur.data == value)
return true;
cur = cur.next;
}
return false;
}
public List inter(List a,List b) {
Element curA = a.head;
List c = new List();
if(a.head == null || b.head == null)
return c;
while(curA != null ) {
if(b.isInList(curA.data))
c.insert(curA.data);
curA = curA.next;
}
return c;
}
public List union(List a,List b) {
Element curA = a.head;
Element curB = b.head;
List c = new List();
while(curA != null && curB != null) {
if(curA.data > curB.data) {
c.insert(curA.data);
curA = curA.next;
}else {
c.insert(curB.data);
curB = curB.next;
}
}
while(curA != null) {
c.insert(curA.data);
curA = curA.next;
}
while(curB != null) {
c.insert(curB.data);
curB = curB.next;
}
return c;
}
public int findLastOccurence(int value) {
Element cur = this.head;
int length = this.getLength() - 1;
if(this.head == null)
return -100;
while(cur != null) {
if(cur.data == value)
length--;
cur = cur.previous;
}
return length;
}
public void deleteEven() {
Element cur = this.head;
//empty list
if(this.head == null)
return;
//deleting the head
while(this.head != null && this.head.data % 2 == 0) {
this.head = this.head.next;
//case of if after deleting the head,the list becomes empty
if(this.head == null)
this.rear = null;
else
this.head.previous = null;
return;
}
while(this.rear != null && this.rear.data % 2 == 0) {
this.rear = this.rear.previous;
this.rear.next = null;
}
while(cur != null ) {
if(cur.data % 2 == 0) {
cur.previous.next = cur.next;
cur.next.previous = cur.previous;
}
cur = cur.next;
}
if ((cur == null) )
return;
}
public boolean deleteHigher(int value) {
Element cur = this.head;
if(this.head == null)
return false;
while(this.head != null && this.head.data > value) {
this.head = this.head.next;
}
if(this.head == null) {
this.rear = null;
return true;
}else {
this.head.previous = null;
}
while(this.rear != null && this.rear.data > value)
this.rear = this.rear.previous;
while(cur != null && cur.data < value) {
cur = cur.next;
}
if ((cur == null) || (cur.data != value))
return false;
cur.previous.next = cur.next;
cur.next.previous = cur.previous;
return true;
}
public void deleteMultipleOf(int value) {
Element cur = this.head;
if(this.head == null)
return ;
//case of deleting the head
while(this.head != null && this.head.data % value == 0) {
this.head = this.head.next;
//case of if the head is deleted,the list becomes empty
if(this.head == null)
this.rear = null;
else
this.head.previous = null;
}
//case of deleting the rear
while(this.rear != null && this.rear.data % value == 0) {
this.rear = this.rear.previous;
}
while(cur != null && cur.data % value != 0)
cur = cur.next;
if(cur == null || cur.data % value != 0)
return ;
cur.next.previous = cur.previous;
cur.previous.next = cur.next;
}
public void deleteLAstOccurence(int value) {
Element cur = this.head;
if(this.head == null)
return ;
while(cur != null) {
if(cur.data == value) {
cur.next.previous = cur.previous;
cur.previous.next = cur.next;
}
cur = cur.previous;
}
}
public void deleteAllOccurences(int value) {
Element cur = this.head;
//deleting head
while(cur != null && cur.data == value) {
cur.next = this.head;
cur.next.previous = null;
cur = this.head;
}
while(cur != null) {
while(cur != null && cur.data != value) {
cur = cur.next;
}
if(cur == null)
return;
cur.next.previous = cur.previous;
cur.previous.next = cur.next;
}
}
public static void main(String[] args) {
List l = new List();
List l1 = new List();
// List l2 = new List();
// List l3 = new List();
l.insert(2);
l.insert(4);
l.insert(6);
l.insert(7);
l.insert(3);
l.insert(2);
System.out.println(l);
l1.insert(4);
l1.insert(6);
l1.insert(7);
l1.insert(0);
l1.insert(10);
// System.out.println(l1);
//
// System.out.println(l.isInList(6));
//
// System.out.println(l2.inter(l,l1));
//
// System.out.println(l3.union(l,l1));
// l.deleteEven(); //not everything is being deleted
// System.out.println(l);
// l.deleteHigher(2); //working
// System.out.println(l);
l.deleteMultipleOf(2); // not working
System.out.println(l);
// l.deleteLAstOccurence(3);// not working
// System.out.println(l);
// System.out.println(l.findLastOccurence(-1));
}
}
解决方法
public void deleteEven() {
//empty list
if(this.head == null)
return;
//deleting the head
while(this.head != null && this.head.data % 2 == 0)
this.head = this.head.next;
//case of if after deleting the head,the list becomes empty
if(this.head == null) {
this.rear = null;
}else
this.head.previous = null;
while(this.rear != null && this.rear.data % 2 == 0) {
this.rear = this.rear.previous;
this.rear.next = null;
}
Element cur = this.head;
while(cur.next != null ) {
if(cur.next.data % 2 == 0) {
cur.next = cur.next.next;
cur.next.previous = cur;
}else
cur = cur.next;
}
}
public boolean deleteHigher(int value) {
if(this.head == null)
return false;
while(this.head != null && this.head.data > value) {
this.head = this.head.next;
}
if(this.head == null) {
this.rear = null;
return true;
}else {
this.head.previous = null;
}
while(this.rear != null && this.rear.data > value)
this.rear = this.rear.previous;
Element cur = this.head;
while(cur.next != null) {
if(cur.next.data > value) {
cur.next = cur.next.next;
cur.next.previous = cur;
}else
cur = cur.next;
}
return true;
}
public void deleteMultipleOf(int value) {
if(this.head == null)
return ;
//case of deleting the head
while(this.head != null && this.head.data % value == 0)
this.head = this.head.next;
//case of if the head is deleted,the list becomes empty
if(this.head == null) {
this.rear = null;
}else
this.head.previous = null;
//case of deleting the rear
while(this.rear != null && this.rear.data % value == 0) {
this.rear = this.rear.previous;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。