我有一个算法比较两个链表L1和L2和相同的元素,它将它们放在第三个列表L3中,同时将它们从L2中删除。我不确定,但如果这是算法应该做的全部内容,并且我不理解某些概念,例如“p1prec”。我想合理地知道算法的设计,更大的画面。当我试图通过逐一检查指示来获得它的感觉时,我觉得我正试图记住它。同样在方法设计相似的算法提出了一些建议会相当welcome.The算法如下:查看两个链表是否相等的算法
Equal(L1,L2)
head[L3] = NIL //L3 is empty
if head[L2] = NIL //if there are no elements in L2 return NIL
return L3
p1 = head[L1] //p1 points the head of L1
p1prec = nil //p1prec is supposed to be precedent I guess.
while p1 =/= nil
p2 = head[L2] //p2 points head of L2
p2prec = nil //p2prec is precedent of p2; first cycle is nil
while p2 =/= nil and key[p1] =/= key[p2]
p2prec = p2 // pass p2 and p2prec through L2 until
p2 = next[p2] // key[p1] = key[p2]
if p2 =/= nil // if two elements are found equal
next[p1prec] = next[p1] // im not sure what this does
insert(L3,p1) // insert the element in L3
p1 = next[p1prec] //neither this,
next[p2prec] = next[p2] //connects the p2prec with nextp2 because p2
free(p2) //is going to be deleted
p1prec = p1 //moves through L1, if no element of p2 is found
p1 = next[p1] //equal with first element of p1
你看如果L1 [i] == L2 [i]或者它可以是L1 [i] == L2 [j]?其中i!= j –
它可以是L1 [i] == L2 [j]?我在哪里!= j –
这是一个问题还是你同意?:) –