我正在尝试将两个链接列表连接在一起,其中第二个列表将紧接在第一个列表的尾部之后。在我的append方法中,我想获得要连接在一起的两个列表,然后将最后一个列表连接到末尾。我在将当前位置分配到第二个列表的开头时遇到麻烦。关于下一步是什么建议?
public class Link {
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{
dData = d;
}
// -------------------------------------------------------------
public void displayLink() // display this link
{
System.out.print(dData + " ");
}
// -------------------------------------------------------------
} // end class Link
public class FirstLastList {
private Link first; // ref to first link
private Link last; // ref to last link
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no links on list yet
last = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{
return first == null;
}
// -------------------------------------------------------------
public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link
if (isEmpty()) // if empty list,
{
last = newLink; // newLink <-- last
}
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if (isEmpty()) // if empty list,
{
first = newLink; // first --> newLink
} else {
last.next = newLink; // old last --> newLink
}
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{ // (assumes non-empty list)
long temp = first.dData;
if (first.next == null) // if only one item
{
last = null; // null <-- last
}
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList() {
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while (current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
// -------------------------------------------------------------
public void append(FirstLastList list1, FirstLastList list2) {
Link current = first;
while (list1 != null) {
current = current.next;
}
current.next = list2.first;
}
} // end class FirstLastList
public class FirstLastApp {
public static void main(String[] args) { //make a new list
FirstLastList theList = new FirstLastList();
theList.insertFirst(22); // insert at front
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11); // insert at rear
theList.insertLast(33);
theList.insertLast(55);
theList.displayList(); // display the list
theList.deleteFirst(); // delete first two items
theList.deleteFirst();
theList.displayList(); // display again
FirstLastList theList2 = new FirstLastList();
theList.insertFirst(22); // insert at front
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11); // insert at rear
theList.insertLast(33);
theList.insertLast(55);
theList.displayList(); // display the list
theList.deleteFirst(); // delete first two items
theList.deleteFirst();
theList.displayList(); // display again
append(theList, theList2);
theList.displayList(); // display again
} // end main()
} // end class FirstLastApp
答案 0 :(得分:0)
在
while (list1 != null) { current = current.next; }
list1 不变,您将取消对NULL指针的引用
奇怪的是,当操作不是静态的并且不返回结果时,您在参数中获得了两个列表。
对于我来说,如果不是静态操作,该操作必须接收一个列表并将其追加到当前(此)列表的末尾,在参数中迭代列表并使用 insertLast < / p>
您还可以通过值接收参数,最好使用引用不复制任何内容
答案 1 :(得分:0)
在append()方法中:
---
title: "MWE Plotly"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#Needed packages
library(plotly)
library(ggcorrplot)
library(dplyr)
library(purrr)
#For reproducability
set.seed(1)
#Generate some numbers
numbers <- tibble(
a = runif(10),
b = runif(10),
c = runif(10)
)
#Function for making the plot
cor_plotter_ex <- function(data){
cor_plot <- ggcorrplot(corr = data)
return(cor_plot)
}
```
```{r plot_plotly, out.width = "33%", hold = T, plotly = T}
#Some data
tib <- tibble(
Id = letters[1:3],
data = list(numbers)
) %>%
mutate(
cor = map(data, ~cor(.x)),
cor_plot = map(cor, ~cor_plotter_ex(.x))
)
#Plot them with walk does not work
walk(tib$cor_plot,
~ggplotly(.x))
#As it only generates the first plot
#and it is not intractable
#Plot them with lapply and tagList does work
htmltools::tagList(lapply(tib$cor_plot,
function(.x) {ggplotly(.x)}))
#Iff one renders the document with
#rmarkdown::render('mwe_plotly.Rmd', output_file='report.html')
#Plotting one of them works
ggplotly(tib$cor_plot[[1]])
```
```{r regular_plot, out.width = "33%", hold = T}
#Just printing the plots clearly works
walk(tib$cor_plot,
~print(.x))
```
当前节点到达最后一个节点时,其Link current = first;
while(current.next != null) {
current = current.next;
}
current.next = list2.first;
将为null。那就是您加入第二个列表的时候。