我有一个LinkedList的实现。
package assignment10;
// A linked list is a sequence of nodes with efficient
// element insertion and removal.
// This class contains a subset of the methods of the
// standard java.util.LinkedList class.
import java.util.NoSuchElementException;
public class LinkedList {
//nested class to represent a node
private class Node {
public Object data;
public Node next;
}
//only instance variable that points to the first node.
private Node first;
// Constructs an empty linked list.
public LinkedList() {
first = null;
}
// Returns the first element in the linked list.
public Object getFirst() {
if (first == null) {
NoSuchElementException ex
= new NoSuchElementException();
throw ex;
} else {
return first.data;
}
}
// Removes the first element in the linked list.
public Object removeFirst() {
if (first == null) {
NoSuchElementException ex = new NoSuchElementException();
throw ex;
} else {
Object element = first.data;
first = first.next; //change the reference since it's removed.
return element;
}
}
// Adds an element to the front of the linked list.
public void addFirst(Object element) {
//create a new node
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
//change the first reference to the new node.
first = newNode;
}
// Returns an iterator for iterating through this list.
public ListIterator listIterator() {
return new LinkedListIterator();
}
/**
* *******************************************************
* Add your methods here
* *******************************************************
*/
public String toString() {
String string;
if (size() == 0) {
string = "{ }";
} else {
string = "{ ";
StringBuilder sb = new StringBuilder();
ListIterator iterator = listIterator();
while (iterator.hasNext()) {
sb.append(String.valueOf(iterator.next())).append(" ");
}
sb.append("}");
string = string.concat(sb.toString());
}
return string;
}
public int size() {
// int size=0;
// while(this.listIterator().hasNext()){
// this.listIterator().next();
// size++;
// }
// return size;
int size = 0;
ListIterator iterator = listIterator();
while (iterator.hasNext()) {
iterator.next();
size++;
}
return size;
}
public void addElementAt(Object element, int index) {
ListIterator listIterator = this.listIterator();
int size = size();
if (size == 0) {
this.listIterator().add(element);
} else {
int i= 0;
while(i<index-1){
listIterator.next();
}
listIterator.add(element);
// Node _head = new Node();
// Node node = new Node();
// node.data = element;
// int ix = index - 1;
// node.next = _head;
//
// Node previous = _head;
//
// for (int i = size - 1; i > ix; --i) {
// previous = previous.next;
// }
//
// Node position = previous.next;
//
// previous.next = node;
// node.next = position;
// ++size;
}
}
public void addFewAtEnd(Object element, int howMany) {
ListIterator listIterator = this.listIterator();
int i=0;
while(i<howMany){
listIterator.add(element);
}
}
public void removeLastFew(int howMany) {
ListIterator listIterator = this.listIterator();
int size = size();
int i=0;
if(howMany>0){
if(howMany<size){
while(i<size){
listIterator.next();
}
for(int removed=0;removed<howMany;removed++){
listIterator.remove();
listIterator.next();
}
} else {
while(i<size){
listIterator.next();
}
for(int removed=0;removed<size;removed++){
listIterator.remove();
listIterator.next();
}
}
}
}
public void removeAllOccurrences(Object stringToBeRemoved) {
ListIterator listIterator = this.listIterator();
while(listIterator.hasNext()){
if(String.valueOf(listIterator.next()).equalsIgnoreCase(stringToBeRemoved.toString())){
listIterator.next();
} else {
listIterator.next();
}
}
}
public void reverseLastFew(int howMany) {
ListIterator listIterator = this.listIterator();
int size = size();
int i=0;
if(howMany>0){
if(howMany<size){
LinkedList linkedList = new LinkedList();
while(this.listIterator().hasNext()){
listIterator.next();
}
int count=0;
while(count<howMany){
linkedList.listIterator().add(listIterator.next());
listIterator().remove();
listIterator.next();
}
} else {
LinkedList linkedList = new LinkedList();
int count=0;
while(count<size){
linkedList.listIterator().add(listIterator.next());
listIterator().remove();
listIterator.next();
}
count = 0;
while(linkedList.listIterator().hasNext()){
if(count==0){
this.addFirst(linkedList.first);
} else {
this.addElementAt(linkedList.listIterator().next(), count);
}
}
}
}
}
//nested class to define its iterator
private class LinkedListIterator implements ListIterator {
private Node position; //current position
private Node previous; //it is used for remove() method
// Constructs an iterator that points to the front
// of the linked list.
public LinkedListIterator() {
position = null;
previous = null;
}
// Tests if there is an element after the iterator position.
public boolean hasNext() {
if (position == null) //not traversed yet
{
if (first != null) {
return true;
} else {
return false;
}
} else {
if (position.next != null) {
return true;
} else {
return false;
}
}
}
// Moves the iterator past the next element, and returns
// the traversed element's data.
public Object next() {
if (!hasNext()) {
NoSuchElementException ex = new NoSuchElementException();
throw ex;
} else {
previous = position; // Remember for remove
if (position == null) {
position = first;
} else {
position = position.next;
}
return position.data;
}
}
// Adds an element before the iterator position
// and moves the iterator past the inserted element.
public void add(Object element) {
if (position == null) //never traversed yet
{
addFirst(element);
position = first;
} else {
//making a new node to add
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
//change the link to insert the new node
position.next = newNode;
//move the position forward to the new node
position = newNode;
}
//this means that we cannot call remove() right after add()
previous = position;
}
// Removes the last traversed element. This method may
// only be called after a call to the next() method.
public void remove() {
if (previous == position) //not after next() is called
{
IllegalStateException ex = new IllegalStateException();
throw ex;
} else {
if (position == first) {
removeFirst();
} else {
previous.next = position.next; //removing
}
//stepping back
//this also means that remove() cannot be called twice in a row.
position = previous;
}
}
// Sets the last traversed element to a different value.
public void set(Object element) {
if (position == null) {
NoSuchElementException ex = new NoSuchElementException();
throw ex;
} else {
position.data = element;
}
}
} //end of LinkedListIterator class
} //end of LinkedList class
并且
package assignment10;
// The ListIterator interface allows access of a position in a linked list.
// This interface contains a subset of the methods of the
// standard java.util.ListIterator interface. The methods for
// backward traversal are not included.
public interface ListIterator
{
//Move Moves the iterator past the next element.
Object next();
// Tests if there is an element after the iterator position.
boolean hasNext();
// Adds an element before the iterator position
// and moves the iterator past the inserted element.
void add(Object element);
// Removes the last traversed element. This method may
// only be called after a call to the next() method.
void remove();
// Sets the last traversed element to a different value.
void set(Object element);
}
removeLastFew()方法应该从最后的LinkedList中删除指定数量的项目。我怎样才能做到这一点?目前,该方法无效。
答案 0 :(得分:0)
你有几个选择。
这是丑陋而缓慢但容易的。
创建removeLast()
并根据需要多次调用它。
当然,如果你需要这个用于严肃的制作,它将会太慢(但我怀疑,鉴于问题的复杂性)。如果是作业,我是你的教授,你很可能会失败。
我想你可以自己做。
这可能是效率最高的,也不是很难(但它也不是很优雅)。
对于列表中的每个元素node
,只需检查 n 元素(n是要擦除的元素数),因为node
为空。如果是这种情况,请将node
的引用设置为null(您必须存储以前的节点,是)。
如果您需要使用“删除”功能,只需在找到上述元素后使用简单的循环。
这是我最喜欢的那个,但我一直是递归的忠实粉丝。
创建一个一直到最后的函数,然后开始擦除。
简单示例(当然,您必须使其适应您的代码):
private int removeLast (int n, Node node) {
int remaining = n;
if (node.next != null) {
remaining = removeLast (n, node.next);
}
if (remaining > 0) {
remove(node);
remaining--;
}
return remaining;
}
注意:我不确定这是否会起作用,jsut是从我的头顶做的,并没有测试它,只是一个伪代码,如果你想这样做方式(建议选择2)。
答案 1 :(得分:0)
我在这里看到的可能是错误:
while(i<size) {
listIterator.next();
}
缺少i的增量。并修复这样的条件:
i < (size-howMany)
同样在删除项目时,您必须首先保存&#34; next&#34;中的值。
答案 2 :(得分:0)
一种简单的方法
在LinkedList
中添加一个长度属性,然后当你需要删除n个节点时,转到&#34; length-n th&#34;节点(一些while循环中的一些getNext())并删除其余的!
不要忘记保持长度有效