问题描述
我正在编写一个双向循环链表并实现各种方法。 其中 2 个包括返回作为参数给出的 2 个列表的并集或交集。 我很确定我的逻辑是正确的,但我不明白为什么我没有得到任何结果。我没有收到错误只是没有响应。
这是我的代码:
public class DCLL {
//ascending order
class Element{
int data; // int type used as example
Element next = null; // reference of the successor
Element prevIoUs = null;
Element(int value) {
this.data = value;
this.next = this;
this.prevIoUs = this;
}
}
private Element head = null;
private Element rear = null;
private int length = 0;
public DCLL() {
this.head = this.rear = null;
this.length = 0;
}
public DCLL(DCLL a) {
this();
//list a is empty
if(a.isEmpty())
return;
Element cur = a.head;
do {
this.insert(cur.data);
cur = cur.next;
}while(cur != a.head);
}
public int getLength() {
return this.length;
}
public boolean isEmpty() {
return this.length == 0;
}
@Override
public String toString() {
String s = " ";
if(this.isEmpty())
return"Empty list";
Element cur = this.head;
s = " | " + cur.data + " | ";
cur = cur.next;
while(cur != this.head) {
s+= cur.data + " | ";
cur = cur.next;
}
return s;
}
public int countEven() {
if(this.isEmpty())
return 0;
Element cur = this.head;
int c = 0;
while(cur != this.rear) {
if(cur.data % 2 == 0)
c++;
cur = cur.next;
}
return c;
}
public int findFirstOccurence(int value) {
if(this.isEmpty())
return -1;
Element cur = this.head;
int c = - 1;
while(cur != this.rear) {
c++;
if(cur.data == value)
return c;
cur = cur.next;
}
return c;
}
public int findLastOccurence(int value) {
if(this.isEmpty())
return - 1;
Element cur = this.rear;
int c = length ;
while(cur != this.head) {
c--;
if(cur.data == value)
return c;
cur = cur.prevIoUs;
}
return c;
}
public boolean isInList(int value) {
if(this.isEmpty())
return false;
Element cur = this.head;
while(cur != this.head) {
if(cur.data == value)
return true;
cur = cur.next;
}
return false;
}
public boolean insertAtHead(int value) {
Element tmp = new Element (value);
if(this.isEmpty()) {
this.head = this.rear = tmp;
return true;
}
this.head.prevIoUs = null;
this.rear.next = null;
tmp.next = this.head;
this.head = tmp;
this.length++;
this.head.prevIoUs = this.rear;
this.rear.next = this.head;
return true;
}
public boolean insert(int value) {
Element tmp = new Element(value);
if(this.isEmpty()) {
this.head = this.rear = tmp;
this.length++;
return true;
}
//inserting before the head
if(this.head.data > tmp.data) {
tmp.next = this.head;
this.head.prevIoUs = tmp;
tmp.prevIoUs = this.rear;
this.rear.next = tmp;
this.head = tmp;
this.length++;
return true;
}
//inserting after the rear
if(this.rear.data < tmp.data) {
tmp.prevIoUs = this.rear;
tmp.next = this.head;
this.rear.next = tmp;
this.head.prevIoUs = tmp;
this.rear = tmp;
this.length++;
return true;
}
// general case insert between head and rear (no repetition)
Element cur = this.head;
while(cur != this.rear && cur.data < tmp.data)
cur = cur.next;
if(cur.data == tmp.data)
return false;
tmp.next = cur;
tmp.prevIoUs = cur.prevIoUs;
cur.prevIoUs.next = tmp;
cur.prevIoUs = tmp;
this.length++;
return true;
}
public boolean delete(int value) {
//list is empty
if(this.isEmpty())
return false;
//testing if value is outside the range [head.data and rear.data]
if(this.head.data > value || this.rear.data < value)
return false;
// list of 1 element
if(this.head == this.rear && this.head.data == value) {
this.head = this.rear = null;
this.length = 0;
return true;
}
//delete the head
if(this.head.data == value) {
this.rear.next = this.head.next;
this.head.next.prevIoUs = this.rear;
this.head = this.head.next;
this.length--;
return true;
}
//delete the rear
if(this.rear.data == value) {
this.rear.prevIoUs.next = this.head;
this.head.prevIoUs = this.rear.prevIoUs;
this.rear = this.rear.prevIoUs;
this.length--;
return true;
}
//general case: delete between head and rear
Element cur = this.head;
while(cur != this.rear && cur.data < value)
cur = cur.next;
if(cur == this.rear || cur.data != value)
return false;
cur.next.prevIoUs = cur.prevIoUs;
cur.prevIoUs.next = cur.next;
this.length--;
return true;
}
public void deleteEven() {
if(this.isEmpty())
return;
this.head.prevIoUs = null;
this.rear.next = null;
if(this.head == this.rear && this.head.data % 2 == 0) {
this.head = this.rear = null;
this.length = 0;
}
while(this.head != null && this.head.data % 2 == 0) {
this.head = this.head.next;
this.length--;
}
if(this.head == null) {
this.rear = null;
return;
}
while(this.rear != null && this.rear.data % 2 == 0) {
this.rear = this.rear.prevIoUs;
this.length--;
}
this.head.prevIoUs = this.rear;
this.rear.next = this.head;
Element cur = this.head;
while(cur != this.rear) {
if(cur.data % 2 == 0) {
cur.next.prevIoUs = cur.prevIoUs;
cur.prevIoUs.next = cur.next;
this.length--;
}
cur = cur.next;
}
}
public void deleteHigher(int value) {
if(this.isEmpty())
return;
this.head.prevIoUs = null;
this.rear.next = null;
if(this.head == this.rear && this.head.data > value) {
this.head = this.rear = null;
this.length = 0;
}
while(this.head != null && this.head.data > value) {
this.head = this.head.next;
this.length--;
}
while(this.rear != null && this.rear.data > value) {
this.rear = this.rear.prevIoUs;
this.length--;
}
this.head.prevIoUs = this.rear;
this.rear.next = this.head;
Element cur = this.head;
while(cur != this.rear) {
if(cur.data > value) {
cur.next.prevIoUs = cur.prevIoUs;
cur.prevIoUs.next = cur.next;
this.length--;
}
cur = cur.next;
}
}
public void deleteLastOccurence(int value) {
if(this.isEmpty())
return;
if(this.head == this.rear && this.head.data == value) {
this.head = this.rear = null;
this.length = 0;
}
if(this.rear.data == value) {
this.head.prevIoUs = this.rear;
this.rear.next = this.head;
this.rear = this.rear.prevIoUs;
this.length--;
}
Element cur = this.rear;
while(cur != this.head) {
if(cur.data == value) {
cur.next.prevIoUs = cur.prevIoUs;
cur.prevIoUs.next = cur.next;
}
cur = cur.prevIoUs;
}
}
public void deleteallOccurrences(int value) {
if(this.isEmpty())
return;
this.head.prevIoUs = null;
this.rear.next = null;
while(this.head != null && this.head.data == value) {
this.head = this.head.next;
this.length--;
}
if(this.head == null) {
this.rear = null;
return;
}
while(this.rear != null && this.rear.data == value) {
this.rear = this.rear.prevIoUs;
this.length--;
}
this.head.prevIoUs = this.rear;
this.rear.next = this.head;
Element cur = this.head;
while(cur != this.rear) {
if(cur.data == value) {
cur.next.prevIoUs = cur.prevIoUs;
cur.prevIoUs.next = cur.next;
}
cur = cur.next;
}
}
public DCLL union(DCLL a,DCLL b) {
Element curA = a.head;
Element curB = b.head;
DCLL c = new DCLL();
do {
if(curA.data > curB.data) {
c.insert(curA.data);
curA = curA.next;
this.length++;
}else {
c.insert(curB.data);
curB = curB.next;
this.length++;
}
}while(curA != a.rear && curB != b.rear);
do {
c.insert(curA.data);
curA = curA.next;
this.length++;
}while(curA != this.rear);
do {
c.insert(curB.data);
curB = curB.next;
this.length++;
}while(curB != this.rear);
return c;
}
public DCLL intersection(DCLL a,DCLL b) {
Element curA = a.head;
DCLL c = new DCLL();
if(a.head == null || b.head == null)
return c;
do {
if(b.isInList(curA.data)) {
c.insert(curA.data);
curA = curA.next;
}
}while(curA != this.rear);
return c;
}
public static void main(String[] args) {
DCLL list = new DCLL();
DCLL list1 = new DCLL();
DCLL list2 = new DCLL();
list.insert(2);
list.insert(5);
list.insert(9);
list.insert(2);
list.insert(58);
list.insert(-8);
list.insert(6);
System.out.println(list);
list1.insert(3);
list1.insert(7);
list1.insert(17);
list1.insert(6);
list1.insert(5);
list1.insert(-8);
list1.insert(-50);
System.out.println(list1);
// list.insertAtHead(3);
// System.out.println(list);
// System.out.println(list.countEven());
//
// System.out.println(list.findFirstOccurence(5));
// list.delete(2);
// System.out.println(list);
// list.deleteEven();
// System.out.println(list);
// list.deleteHigher(5);
// System.out.println(list);
// list.deleteLastOccurence(5);
// System.out.println(list);
// list.deleteallOccurrences(58);
// System.out.println(list);
// System.out.println(list.findLastOccurence(6));
list2.union(list,list1);
System.out.println(list2);
// list2.inter(list,list1);
// System.out.println(list2);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)