如何在不使用节点的情况下,利用递归交替合并链表?

huangapple go评论74阅读模式
英文:

How do I merge linked list alternately without using nodes? and by using recursion?

问题

以下是翻译好的内容:

没有错误,但仍无法工作。我无法弄清楚问题出在哪里。我们还没有在课堂上讨论过节点的概念,所以我很难理解它。

输出应为:

Skinny Love- Birdy

Happier- Ed Sheeran

Someday- Chelsea Cutler

public class HandsOnAct1 {

  static LinkedList songs = new LinkedList();
  static LinkedList artists = new LinkedList();
  static LinkedList playlist = new LinkedList();
  
  public static void Merge(){
      int element= songs.size();
      int number=0;
      
      System.out.println(element);
      
  if (number !=element){
  playlist.add(number, songs);
  playlist.add(number,artists);
  number++;
  
  }
      System.out.println(playlist);
  }
  

    public static void main(String[] args) {
 
        songs.add("Skinny Love");
        songs.add("Happier");
        songs.add("Sometimes");
        
        artists.add("Birdy");
        artists.add("Ed Sheeran");
        artists.add("Chelsea Cutler");
        
        Merge();    
    }
    
}
英文:

There are no errors but it still won't work. I can't figure out what's wrong. We still haven't discussed the concept of nodes in class so I have trouble understanding it.

It's output should be:

Skinny Love- Birdy

Happier- Ed Sheeran

Someday- Chelsea Cutler

public class HandsOnAct1 {

  static LinkedList songs = new LinkedList();
  static LinkedList artists = new LinkedList();
  static LinkedList playlist = new LinkedList();
  
  public static void Merge(){
      int element= songs.size();
      int number=0;
      
      System.out.println(element);
      
  if (number !=element){
  playlist.add(number, songs);
  playlist.add(number,artists);
  number++;
  
  }
      System.out.println(playlist);
  }
  

    public static void main(String[] args) {
 
        songs.add("Skinny Love");
        songs.add("Happier");
        songs.add("Sometimes");
        
        artists.add("Birdy");
        artists.add("Ed Sheeran");
        artists.add("Chelsea Cutler");
        
        Merge();    
    }
    
}

答案1

得分: 0

这是一个使用递归的示例,正如我理解的那样,这是一个先决条件。

static LinkedList<String> songs = new LinkedList<>();
static LinkedList<String> artists = new LinkedList<>();
static LinkedList<String> playlist = new LinkedList<>();

songs.add("Skinny Love");
songs.add("Happier");
songs.add("Sometimes");

artists.add("Birdy");
artists.add("Ed Sheeran");
artists.add("Chelsea Cutler");

int index = 0;
static void merge() {

    // 如果需要的话,可以进行检查
    if (index >= artists.size()){
        return;
    }

    playlist.add(songs.get(index) + " - " + artists.get(index));
    System.out.println(playlist.get(index));
    index++;
    merge();
}

merge();

你自己的代码的主要问题在于,你需要从内部再次调用merge函数,并且要么像我所做的那样将索引移到函数外部,要么将其作为参数传入,比如public static void merge(int index)。然后你可以像这样从内部调用它:merge(index++)

这可能是一个观点问题。(如果你愿意,你可以阅读关于纯函数/非纯函数和命令式/函数式编程的内容,这是其中的区别。)

英文:

Here is an example using recursion as i understand it was a pre-requisite?

static LinkedList&lt;String&gt; songs = new LinkedList&lt;&gt;();
static LinkedList&lt;String&gt; artists = new LinkedList&lt;&gt;();
static LinkedList&lt;String&gt; playlist = new LinkedList&lt;&gt;();

songs.add(&quot;Skinny Love&quot;);
songs.add(&quot;Happier&quot;);
songs.add(&quot;Sometimes&quot;);

artists.add(&quot;Birdy&quot;);
artists.add(&quot;Ed Sheeran&quot;);
artists.add(&quot;Chelsea Cutler&quot;);

int index = 0;
static void merge() {

    // if you want/need checks
    if ( index &gt;= artists.size()){
        return;
    }

    playlist.add(songs.get(index) + &quot; - &quot; + artists.get(index));
    System.out.println(playlist.get(index));
    index++;
    merge();
}

merge();

the main issue with your own code is that you need to call merge again from inside it and either move the index outside the function as i have done or take it in as a parameter like public static void merge(int index). you will then call it from inside like merge(index++).

That's a matter of opinion i guess. (if you want you can read on pure/impure functions and imperative/functional programming which is the difference)

huangapple
  • 本文由 发表于 2020年10月12日 06:58:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64309822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定