如何将显示方法添加到Java链表实现中,该实现只显示列表中的前10项和后10项?

如何将显示方法添加到Java链表实现中,该实现只显示列表中的前10项和后10项?,java,algorithm,data-structures,methods,linked-list,Java,Algorithm,Data Structures,Methods,Linked List,更新:感谢所有回答我的人,我完成了分配给我的任务。这真是一个很棒的论坛,我惊讶地发现这里有这么多乐于助人、思想友好的人:) 我所做的是以以下方式修改print方法: public static void print(ListNode start){ System.out.println("Printing the first 10 elements on the list:"); System.out.print("{");

更新:感谢所有回答我的人,我完成了分配给我的任务。这真是一个很棒的论坛,我惊讶地发现这里有这么多乐于助人、思想友好的人:)

我所做的是以以下方式修改print方法:

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");
       }    
公共静态无效打印(ListNode开始){
System.out.println(“打印列表上的前10个元素:”);
系统输出打印(“{”);
ListNode-previous=开始;
ListNode当前=开始;

对于(inti=0;i好的,我不打算为您编写代码,但我会告诉您如何着手编写

首先,假设您有两个指针(
head1和head2
),它们指向
列表的第一个节点。向前移动
head2
十步,使
head1
保持在同一位置

现在,经过十个步骤,您将
head1
放在0位置,将
head2
放在第9位置。现在一起移动,直到
head2
点击
NULL
。一旦
head2
点击
NULL
,开始单独移动
head1
,并打印每个节点,直到
head1
到达
head2


希望这对你有所帮助好吧,我不打算为你写代码,但我会告诉你怎么做

首先,假设您有两个指针(
head1和head2
),它们指向
列表的第一个节点。向前移动
head2
十步,使
head1
保持在同一位置

现在,经过十个步骤,您将
head1
放在0位置,将
head2
放在第9位置。现在一起移动,直到
head2
点击
NULL
。一旦
head2
点击
NULL
,开始单独移动
head1
,并打印每个节点,直到
head1
到达
head2


希望这对您的第一件和最后一件物品有帮助

最好的方法是将其设置为双重链接列表:

基本上,在你的清单前面,它仍然是一样的。
但现在,您也可以访问列表末尾的列表,并移动到列表的开头。

为此,您需要对第一项和最后一项进行引用

最好的方法是将其设置为双重链接列表:

基本上,在你的清单前面,它仍然是一样的。
但是现在,您也可以访问列表末尾的列表,并移动到列表的开头。

这是一个相当奇怪的链表实现

突出的是

  • 静态成员的数量
  • 没有表示实际列表的类
节点中的静态成员应放置在LinkedList类中。请查看JavaAPI类的成员

其他成员与中发现的类似

目前,最简单的解决方案是按照peraueb在评论中的建议,遵循打印方法;即,按照打印方法进行操作,并存储到第10个到最后一个节点的链接。noMAD的想法在那里可以很好地发挥作用


通常的方法是让LinkedList类处理指向第一个和最后一个节点的引用/链接。节点本身应该包含指向上一个节点和下一个节点的链接/引用。这是Nactives answer的建议。现在,您正在手动处理m中的那些链接/引用ain(…)。

这是一个相当奇怪的链表实现

突出的是

  • 静态成员的数量
  • 没有表示实际列表的类
节点中的静态成员应放置在LinkedList类中。请查看JavaAPI类的成员

其他成员与中发现的类似

目前,最简单的解决方案是按照peraueb在评论中的建议,遵循打印方法;即,按照打印方法进行操作,并存储到第10个到最后一个节点的链接。noMAD的想法在那里可以很好地发挥作用


通常的方法是让LinkedList类处理指向第一个和最后一个节点的引用/链接。节点本身应该包含指向上一个节点和下一个节点的链接/引用。这是Nactives answer的建议。现在,您正在手动处理m中的那些链接/引用艾因(…).

请详细说明一下好吗?显示方法会做什么?在我看来,它与已经实现的打印方法类似。谢谢paraueb8921,我按照你的建议做了,并且成功了!请详细说明一下好吗?显示方法会做什么?在我看来,它与已经实现的打印方法类似。谢谢paraueb8921,我做到了你的建议奏效了!
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);
    }

}