我想實現R中下面的算法:給定一個列表R:遍歷目錄
Iterate(Cell: top)
While (top != null)
Print top.Value
top = top.Next
End While
End Iterate
基本上,算法應儘快打破,因爲它擊中「空」甚至當列表不過度。
myls<-list('africa','america south','asia','antarctica','australasia',NULL,'europe','america north')
我不得不添加一個for循環使用is.null()函數,但下面的代碼是災難,我需要你的幫助來解決它。
Cell <- function(top) {
#This algorithm examines every cell in the linked list, so if the list contains N cells,
#it has run time O(N).
for (i in 1:length(top)){
while(is.null(top[[i]]) !=TRUE){
print(top)
top = next(top)
}
}
}
您可以使用運行此函數:
Cell(myls)
第二次解決方案中的精彩放置位置。偉大的工作Johan,謝謝你分享這三個。 – Bhushan