[英]How to add a display method to a Java Linked List implementation that displays only the first and the last 10 items on the list?
更新:感謝所有回答我所做的工作。 這確實是一個很棒的論壇,我很驚訝在這里找到這么多有用和友好的人:)
我所做的是通過以下方式修改打印方法:
public static void print(ListNode start){
System.out.println("Printing the first 10 elements on the list:");
System.out.print("{");
ListNode previous = start;
ListNode current = start;
for (int i = 0; i<10; i++){
current=current.next;
}
for(ListNode node = start; node != current; node = node.next){
System.out.print(node);
}
System.out.println("}");
System.out.println("Printing the last 10 elements on the list:");
System.out.print("{");
while(current != null){
previous = previous.next;
current = current.next;
}
for(ListNode node = previous; node != current; node = node.next){
System.out.print(node);
}
System.out.println("}");
System.out.println("End of list");
}
更新結束
我正在學習Java中的算法和數據結構,我需要向現有的練習鏈表實現中添加特定的顯示方法,但是我不知道該怎么做。
因此,有一個包含許多項目(例如數千個項目)的鏈接列表, 我需要一種顯示方法,該方法僅顯示列表中的前10個項目和最后10個項目。
你能給我建議一種方法嗎?
我需要處理的鏈表實現如下:
import java.util.*;
public class Main {
public static class ListNode {
//A linked list node. The data field is represented by the field int data
//The next item is referenced by the reverence value next
int data;
ListNode next;
public ListNode(){
this.data = 0; this.next = null;
}
public ListNode(int data){
this();this.data = data;
}
public ListNode(int data, ListNode next){
this.data = data;this.next = next;
}
public String toString() {
return "[" + this.data + "]";
}
//The linked list is referenced by the first node.
//An empty list is referenced by a null reference.
//That's why all methods for the list are public static -
//(null doesn't have methods)
//Returns the beginning of a list with length "length"and
//elements with random values
public static ListNode generate(int length) {
ListNode start = null;
Random rn = new Random();
for(int i = 0; i < length; i++){
start = new ListNode(rn.nextInt(10000), start);
}
return start;
}
//Displays the list to the console from the specified starting point
public static void print(ListNode start){
System.out.print("{");
for(ListNode node = start; node != null; node = node.next){
System.out.print(node);
}
System.out.println("}");
}
//Counts how many elements there are in the list
public static int length(ListNode L){
int k=0;
for(;L!=null;k++,L=L.next);
return k;
}
//Returns a node with key searchd if found in the list
public static ListNode search(ListNode start, int searchd){
for(ListNode node = start; node != null; node = node.next){
if(node.data == searchd){ return node; }
}
return null;
}
//If a node with the specified key is found in the list L
//a new node with the value keyAfter is inserted after it.
public static void insertAfter(ListNode L, int key, int keyAfter){
while(L!=null && L.data!=key)L=L.next;
if(L!=null){
L.next= new ListNode(keyAfter,L.next);
}
}
//Inserts a node with key "keyBefore" before the node
//with the specified key
public static ListNode insertBefore(ListNode L, int key, int keyBefore){
ListNode p = null, r=L;
while(L!=null && L.data!=key){
p=L;L=L.next;
}
if(p!=null){
p.next= new ListNode(keyBefore,p.next);return r;
}
else{
p=new ListNode(keyBefore,L);return p;
}
}
//Inserts a new element with the specified key in a sorted list
//with ascending values so that the list remains sorted
public static ListNode insertOrd(ListNode L, int key){
ListNode p = null, r=L;
while(L!=null && L.data<key){
p=L;L=L.next;
}
if(p!=null){
p.next= new ListNode(key,p.next);return r;
}
else{
p=new ListNode(key,L);return p;
}
}
//Generates a sorted list with a specified lenght
public static ListNode generateOrd(int length) {
ListNode start = null;
Random rn = new Random();
for(int i = 0; i < length; i++){
start = insertOrd(start,rn.nextInt(10000));
}
return start;
}
//Takes two ordered lists and returns a merged and sorted list
//that combines the elements in the original lists
public static ListNode merge(ListNode a, ListNode b){
if(a==null)return b;
if(b==null)return a;
ListNode r = null;
if(a.data<=b.data){
r=a;a=a.next;
}else{
r=b;b=b.next;
}
ListNode last=r;
while(a!=null && b!=null){
if(a.data<=b.data){
last.next=a;a=a.next;
}else{
last.next=b;b=b.next;
}
last=last.next;
}
if(a!=null)last.next=a;else last.next=b;
return r;
}
//Splits a list evenly and returns the beginning of the 2-nd part
public static ListNode split(ListNode L){
int n=length(L)/2;
ListNode t=L;
for(int i=0;i<n-1;i++,t=t.next);
ListNode secPart = t.next;
t.next=null;
return secPart;
}
//Sorts a list in an ascending order
public static ListNode mrgSort(ListNode L){
if(L==null || L.next==null)return L;
ListNode b = split(L);
L=mrgSort(L); b= mrgSort(b);
return merge(L,b);
}
};//End of class ListNode
public static void main(String[] args){
ListNode a = ListNode.generateOrd(10);
ListNode.print(a);
ListNode b = ListNode.generateOrd(10);
ListNode.print(b);
a=ListNode.merge(a,b);
ListNode.print(a);
b=ListNode.split(a);
ListNode.print(a);
ListNode.print(b);
ListNode c = ListNode.generate(20);
ListNode.print(c);
c = ListNode.mrgSort(c);
ListNode.print(c);
}
}
好吧,我不會為您編寫代碼,但會告訴您如何去做。
首先說您有兩個指針( head1 and head2
),它們指向list
的第一個節點。 移動head2
十步向前保持head1
在同一個地方。
現在,經過十步你head1
0和head2
在第9位。 現在一起移動直到head2
達到NULL
為止。 一旦head2
打NULL
,開始移動head1
獨自打印每個節點,直到head1
達到head2
。
希望這可以幫助
這是鏈表的一個相當奇怪的實現。
突出的是
節點中的靜態成員應放在LinkedList類中。 簽出JavaAPIs LinkedList類的成員。
其他成員類似於Collections類中的成員 。
就目前而言,最簡單的解決方案是peraueb在注釋中建議的遵循打印方法。 即執行打印,並將鏈接存儲到第10個節點。 noMAD的想法在這里可以很好地工作。
通常的方法是讓LinkedList類處理對第一個和最后一個Node的引用/鏈接。 節點本身應包含到上 一個節點和下一個節點的鏈接/引用。 這是Nactives答案的建議。 現在,您正在手動處理main(...)中的那些。
為此,您需要參考第一項和最后一項。
最好的方法也是使它成為雙鏈表: http : //en.wikipedia.org/wiki/Doubly_linked_list
基本上,在列表的最前面還是一樣。 但是現在您也可以在列表的末尾訪問列表並移至列表的開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.