簡體   English   中英

Java中雙重LinkedList中元素的升序

[英]Ascending order of elements in doubly LinkedList in Java

我一直在嘗試完成這項任務,但是我很簡短而且絕對毫無頭緒,所以我想知道是否有人可以幫助我實現這些方法以及解釋? 我非常有限,這就是為什么我很傻。 任何幫助是極大的贊賞! 總共有兩個類,在單獨的文件中,以及已經實現的迭代器。

我必須實現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. 編寫一種對列表進行排序的方法
  2. 實現所有方法的基本功能,並在退出方法之前調用sort方法

另外:我認為列表節點不應該具有該功能。 節點只知道它們的值和鄰居

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM