H
HalFas`
Hi, I have this single circular linked list structure:
public class ListItem {
int n;
ListItem next;
public ListItem() {
this.n = 0;
this.next = null;
}
public ListItem(int n, ListItem e) {
this.n = n;
this.next = e;
}
public int getValue() { return this.n; }
public ListItem getNext() { return this.next; }
public void setValue(int n) { this.n = n; }
public void setNext(ListItem nextItem) { this.next =
nextItem; }
}
public class List {
ListItem head;
public List() {
this.head = null;
}
public ListItem getHead() { return this.head; }
public void Insert(int n) {
if (this.head == null) {
this.head = new ListItem(n, null);
this.head.next = head;
} else if (this.head.getNext() == null) {
this.head = new ListItem(n, this.head);
head.setNext(head);
} else {
this.head.next = new ListItem(n, this.head.next);
}
}
public void Remove(int Key) {
ListItem curr = this.head;
do {
if ( curr.next.getValue() == Key ) {
ListItem temp = curr.getNext();
curr.setNext(temp.getNext());
} curr = curr.getNext();
if (curr.getNext() == this.head && curr.getValue()
== Key) {
this.head.setNext(null);
curr.setNext(null);
}
} while ( curr != this.head );
}
}
My question is, how to optimize Remove(int Key) method.
Maybe anyone have some docs, about SINGLE circular linked list's.
Thanks, for any help.
public class ListItem {
int n;
ListItem next;
public ListItem() {
this.n = 0;
this.next = null;
}
public ListItem(int n, ListItem e) {
this.n = n;
this.next = e;
}
public int getValue() { return this.n; }
public ListItem getNext() { return this.next; }
public void setValue(int n) { this.n = n; }
public void setNext(ListItem nextItem) { this.next =
nextItem; }
}
public class List {
ListItem head;
public List() {
this.head = null;
}
public ListItem getHead() { return this.head; }
public void Insert(int n) {
if (this.head == null) {
this.head = new ListItem(n, null);
this.head.next = head;
} else if (this.head.getNext() == null) {
this.head = new ListItem(n, this.head);
head.setNext(head);
} else {
this.head.next = new ListItem(n, this.head.next);
}
}
public void Remove(int Key) {
ListItem curr = this.head;
do {
if ( curr.next.getValue() == Key ) {
ListItem temp = curr.getNext();
curr.setNext(temp.getNext());
} curr = curr.getNext();
if (curr.getNext() == this.head && curr.getValue()
== Key) {
this.head.setNext(null);
curr.setNext(null);
}
} while ( curr != this.head );
}
}
My question is, how to optimize Remove(int Key) method.
Maybe anyone have some docs, about SINGLE circular linked list's.
Thanks, for any help.