Reputation: 1
I am trying to write the algorithm in this question (from a quiz, not homework):
Write an algorithm of a function to print out the information stored in a circular list. Make sure that your algorithms works for empty lists, lists containing only one node, and lists containing many nodes.
My algorithm prints info <val list metadata>
. This algorithm prints the information in circular list.
if (newptr != null) // check is list empty or not
firstnod = head // if it's not, save the first nod's data because it's circular list
print newptr.data
end if
loop (newptr.data != firstnod)
print newptr.data
count += 1
end loop
Upvotes: 0
Views: 846
Reputation: 34625
Need to update the newptr
in the loop. Else, you will always get the same elements and is an infinite loop.
loop newptr != firstnod
print newptr.data
newptr = newptr.nextnode
endloop
Edit 1:
if list != NULL
print list.data
if list.nextNode != NULL
Node* head, temp
head = list
temp = list.nextNode
while( temp != head )
print temp.data
temp = temp.nextNode
End while
else
print "Next node is not intialized";
else
print "List is empty";
Upvotes: 2