Java中双重LinkedList中元素的升序

时间:2015-11-26 11:44:04

标签: java linked-list

我一直在努力完成这项任务,但是我已经做得很短,而且绝对无能为力,所以我想知道是否有人可以帮我实施这些方法,以及解释一下?我很有限,这就是为什么我傻了。任何帮助是极大的赞赏!总共有两个类,在单独的文件中,加上已经实现的迭代器。

我必须实现一个SortedLinkedList类,它在双向链表中按键值的升序维护列表元素。

/* LinkedListNode */
public class LinkedListNode {
public int key;               /* Key */
public LinkedListNode prev;   /* Pointer to the previous node */
public LinkedListNode next;   /* Pointer to the next node */
};

/*******************************************************
* Constructor: Initializes the linked list
*******************************************************/
SortedLinkedList();

/*******************************************************
* Removes all of the nodes from this list.
 *******************************************************/
void Clear();

/*******************************************************
* Returns the number of nodes in the list
*******************************************************/
int NoOfNodes(){return noOfNodes;}

/*******************************************************
* Inserts the given key in ascending order in the list
*******************************************************/
public void Add(int key);

/*******************************************************
* Removes the node that contains the key from the list 
* (if the key is found)
* Returns 0 upon successful deletion, -1 on error
*******************************************************/
public void Remove(int key);

/*******************************************************
* Removes a node from the list given a pointer to the node
*******************************************************/
public void Remove(LinkedListNode node);

/*******************************************************
* Searches a key in the list and returns a pointer
* to the list node that contains the key
*******************************************************/
public LinkedListNode Find(int key);

迭代器:

public class LinkedListIterator{
private SortedLinkedList list;        /* Linked List */
private LinkedListNode pCurrentNode;  /* Pointer to the current node */
private int dir;                      /* 0: in ascending order, 1: in descending order */

/*******************************************************
* Constructor: Creates a LinkedListIterator
* dir: 0 means we want the items in ascending order
* dir: 1 means we want the items in descending order
*******************************************************/
public LinkedListIterator(SortedLinkedList _list, int _dir){
list = _list;
dir = _dir;

if (dir == 0) pCurrentNode = list.head;
else          pCurrentNode = list.tail;  
} //end-LinkedListIterator

/*******************************************************
* Returns a pointer to the next node in the list.
* Returns NULL if the end of the list has been reached
*******************************************************/
public LinkedListNode GetNextNode(){
  LinkedListNode ps = pCurrentNode;

  /* Move over to the next student */
  if (ps != null) {
    if (dir == 0) pCurrentNode = ps.next;
    else          pCurrentNode = ps.prev;
  } /* end-if */

  return ps;  
} //end-GetNextNode

/*******************************************************
* Returns TRUE if there are more nodes to return in the list
* Returns FALSE if the end of the list has been reached
*******************************************************/
public boolean HasMoreNodes(){
  return (pCurrentNode == null)?false:true;  
} //end-HasMoreNodes
};

1 个答案:

答案 0 :(得分:0)

  1. 编写一个对列表进行排序的方法
  2. 实现其基本功能中的所有方法,并在退出方法之前调用排序方法
  3. 另外:我不认为列表节点不应具有该功能。 节点只知道它们的价值和邻居