Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 保持LinkedList始终处于排序状态_Java_Singly Linked List - Fatal编程技术网

Java 保持LinkedList始终处于排序状态

Java 保持LinkedList始终处于排序状态,java,singly-linked-list,Java,Singly Linked List,为了练习,我得到了如下所示的代码,我的任务是修改它,以便链表总是被排序的。我现在的主要问题是,我不知道从哪里开始,也不知道要修改哪些方法。欢迎任何提示 import java.util.Iterator; import java.util.NoSuchElementException; / * This is a copy of the LinkedList class, which has been modified to only * accept elements that can

为了练习,我得到了如下所示的代码,我的任务是修改它,以便链表总是被排序的。我现在的主要问题是,我不知道从哪里开始,也不知道要修改哪些方法。欢迎任何提示

import java.util.Iterator;
import java.util.NoSuchElementException;

/
 * This is a copy of the LinkedList class, which has been modified to only
 * accept elements that can be compared.
 * 
 * Note that the elements accepted by this list must implement the Comparable
 * interface. This means that the elements can be compared as follows:
 *   
 * x.compareTo(y) == 0 means x == y
 * x.compareTo(y) < 0 means x < y
 * x.compareTo(y) > 0 means x > y
 * 
 * Your task is to modify this class so that the elements always are sorted.
 */
public class SortedLinkedList<T extends Comparable<T>> implements Iterable<T> {

/* Easy operations for a linked list

add(x): Searching for the place where the element x is to be added must
        take place in the calling routine. This must set previous to
        point to the node AFTER which the new element is to be inserted.
        curr is set to point to the new element.

remove(): The curr element is removed from the list. Searching for this 
          element must take place in the calling routine. This must set 
          curr to point to the element to be removed. After removal curr
          points to the element following the removed one. 

isEmpty(): checks for an empty list

endOfList(): checks whether curr has reached and passed the end of the list 

retrievecurr(): return the info part of the curr element.

reset(): resets the list so that curr points to the first element

succ(): an iterator, moves curr one step forward

Note that when a class implements the interface Iterable<T> then
it can be the target of the "foreach" statement. See IterationExample.java
 */

private Node start, curr, prev;

public SortedLinkedList() {

    curr = null;     // the curr position in the list
    start = null;    // the first element
    prev = null;     // the node before curr
}

public void add(T x) {

    if (start == null) {            // if start == null, insert a first element into an empty list
        Node newNode = new Node();  // create the new element, info and link are set to null.
        newNode.info = x;           // and assign the data given as parameter. The link is left as null            
        start = newNode;            // start is updated to point to the new element
        curr = start;               // curr is updated to point to the new first (and only) element

    } else if (prev == null) { // a new first element is inserterd into a non-empty list

        Node newNode = new Node();  // a new node is created ...
        newNode.info = x;           // and assigned the data given as parameter
        newNode.link = start;       // and linked before the old first element 
        start = newNode;            // start is updated to point to the new first element
        curr = newNode;             // curr is updated to point to the new first element

    } else { // a new element is inserted last (if prev.link == null) or between prev and curr

        Node newNode = new Node();  // create a new node 
        newNode.info = x;           // assign it the data given as parameter 
        newNode.link = prev.link;   // link it before curr ...
        prev.link = newNode;        // ... and after previous
        curr = newNode;             // update curr to point to newNode      
    }

} // add

public void remove() {              // removes the current node

    if (isEmpty() || endOfList()) { // no node to be removed 
        return;
    } 
    else {                          // curr points to the element to be removed 
        if (prev == null) {         // remove a first element: start is updated!
            start = start.link;     // "jump over" the first element (curr/start)
            curr = start;           // update curr to point to the new start
        } 
        else {                      // the element to be removed is in the middle of the list/last
            prev.link = curr.link;  // "jump over it!"
            curr = curr.link;       // curr is updated to the element after the removed one
        } 
    }
} // remove

public boolean isEmpty() { // checks if the list is empty
    return start == null;
}

public T retrieveCurr() { // return the curr element's data
    return curr.info;
}

public void reset() { // resets a list so that curr points to the first element

    curr = start;     // curr starts from the beginning of the list 
    prev = null;      // there is no previous element for curr
}

public boolean endOfList() { // has curr reached and passed the end of the list?

    return curr == null;
}

public void succ() { // moves curr to the next element

    curr = curr.link;       // curr is set to the next node
    if (prev == null) {     // previous must follow; if it was null to begin with ...
        prev = start;       // ... it must now point to start
    }
    else {
        prev = prev.link;   // otherwise it is just moved one step forward
    }
}

public Iterator<T> iterator(){ //Needed to implement the iterable interface
    return new ListIterator();
}

private class Node {

    T info;
    Node link;

    Node() {
        info = null;
        link = null;
    }
}

/*
 * An example iterator class to walk through the list from
 * the start to the end. Note the similarity with the methods
 * above. They implement similar functionality, but here it
 * is hidden behind a standard interface.
 * 
 * Note the class is private and the implementation of the iterator
 * is therefore not visible outside. 
 * 
 * See the java api documentation for more information on 
 * how iterators should behave.
 */
private class ListIterator implements Iterator<T>{

    private Node curr;

    public ListIterator(){
        curr=start;
    }

    public boolean hasNext(){
        return curr!=null;
    }

    public T next(){
        if(curr==null){
            throw new NoSuchElementException();
        }
        T value=curr.info;
        curr=curr.link;
        return value;
    }
    public void remove(){
        throw new UnsupportedOperationException();

    }
}
}
import java.util.Iterator;
导入java.util.NoSuchElementException;
/
*这是LinkedList类的副本,该类已修改为
*接受可以比较的元素。
* 
*请注意,此列表接受的元素必须实现可比较的
*接口。这意味着可以按如下方式对元素进行比较:
*   
*(y)==0表示x==y
*(y)<0表示x0表示x>y
* 
*您的任务是修改此类,以便始终对元素进行排序。
*/
公共类SortedLinkedList实现了Iterable{
/*链接列表的简单操作
添加(x):必须搜索要添加元素x的位置
在调用例程中发生。必须将其设置为“上一个”
指向要插入新元素的节点。
curr设置为指向新元素。
remove():curr元素已从列表中删除。正在搜索此项
元素必须在调用例程中发生。必须设置
curr指向要删除的元素。删除后curr
指向移除的元素后面的元素。
isEmpty():检查空列表
endOfList():检查curr是否已到达并通过列表的末尾
retrievecurr():返回curr元素的info部分。
reset():重置列表,使curr指向第一个元素
such():迭代器,将curr向前移动一步
请注意,当类实现接口Iterable时
它可以是“foreach”语句的目标
*/
专用节点开始、当前、上一个;
公共分类链接列表(){
curr=null;//列表中的curr位置
start=null;//第一个元素
prev=null;//curr之前的节点
}
公共无效添加(T x){
如果(start==null){//if start==null,在空列表中插入第一个元素
Node newNode=new Node();//创建新元素时,info和link设置为null。
newNode.info=x;//并将给定的数据指定为参数。链接保留为null
start=newNode;//start更新为指向新元素
curr=start;//curr更新为指向新的第一个(也是唯一的)元素
}else如果(prev==null){//将新的第一个元素插入到非空列表中
Node newNode=new Node();//将创建一个新节点。。。
newNode.info=x;//并将给定的数据指定为参数
newNode.link=start;//并在旧的第一个元素之前链接
start=newNode;//start更新为指向新的第一个元素
curr=newNode;//curr更新为指向新的第一个元素
}else{//最后插入新元素(如果prev.link==null)或在prev和curr之间插入新元素
Node newNode=new Node();//创建一个新节点
newNode.info=x;//将给定的数据作为参数分配给它
newNode.link=prev.link;//在当前之前链接它。。。
prev.link=newNode;//…和在上一个
curr=newNode;//更新curr以指向newNode
}
}//添加
public void remove(){//删除当前节点
如果(isEmpty()| | endOfList()){//没有要删除的节点
返回;
} 
else{//curr指向要删除的元素
如果(prev==null){//删除第一个元素:开始更新!
start=start.link;//“跳过”第一个元素(curr/start)
curr=start;//更新curr以指向新的开始
} 
否则{///要移除的元素位于列表/最后的中间。
prev.link=curr.link;/“跳过它!”
curr=curr.link;//curr更新为删除的元素之后的元素
} 
}
}//删除
public boolean isEmpty(){//检查列表是否为空
返回start==null;
}
public T retrieveCurr(){//返回curr元素的数据
返回当前信息;
}
public void reset(){//重置列表,使curr指向第一个元素
curr=start;//curr从列表的开头开始
prev=null;//curr没有上一个元素
}
public boolean endOfList(){//curr是否已到达并通过列表末尾?
返回curr==null;
}
public void such(){//将curr移动到下一个元素
curr=curr.link;//curr设置为下一个节点
if(prev==null){//previous必须跟在后面;如果它以null开头。。。
prev=start;//…它现在必须指向start
}
否则{
prev=prev.link;//否则只向前移动一步
}
}
实现iterable接口需要公共迭代器Iterator(){//
返回新的ListIterator();
}
私有类节点{
T信息;
节点链接;
节点(){
info=null;
link=null;
}
}
/*
*遍历列表的示例迭代器类
*从开始到结束。注意与方法的相似性
*它们实现了类似的功能,但在这里
*隐藏在标准接口后面。
* 
*注意,类是私有的,迭代器的实现是私有的
*因此在外面看不见。
* 
*请参阅java api文档以了解有关的更多信息
*迭代器应该如何工作。
*/
私有类ListIterator实现了迭代器{
专用节点电流;
公共列表迭代器(){
curr=开始;
}
公共布尔hasNext(){
返回货币!=null;
}
public void add(T x) {
    Node newNode = new Node();
    newNode.info = x;

    // case: start is null; just assign start to the new node and return
    if (start == null) {
        start = newNode;
        curr = start;
        // prev is null, hence not formally assigned here
        return;
    }

    // case: new node to be inserted comes before the current start;
    //       in this case, point the new node to start, update pointers, and return
    if (x.compareTo(start.info) < 0) {
        newNode.link = start;
        start = newNode;
        curr = start;
        // again we leave prev undefined, as it is null
        return;
    }

    // otherwise walk down the list until reaching either the end of the list
    // or the first position whose element is greater than the node to be
    // inserted; then insert the node and update the pointers
    prev = start;
    curr = start;
    while (curr != null && x.compareTo(curr.info) >= 0) {
        prev = curr;
        curr = curr.link;
    }

    // splice in the new node and update the curr pointer (prev already correct)
    newNode.link = prev.link;
    prev.link = newNode;
    curr = newNode;
}