One of the more common uses of link list traversals is to print the entire contents of a list. If the user selects the print list option from the menu, we traverse the list and print all of the key fields. In this program, we simply print a sequential number and the data from the node Within a loop, we continue until all of the data have been printed. The code is shown in Algorithm 11.
Algorithm 13: Print Linked List
algorithm printList (val list
This algorithm traverses a linked list and prints the data in node.
Pre list is metadata structure to a valid list
Post All data have been printed in sequence
1 if (list.head null)
1 Print (No data in list.)
2 else
1 print (**** Begin Data Print ****)
2 sno = 0
1 dataptr = list.head
2 loop (dataptr-> link not null)
1 sno = sno + 1
2 print (sno, dataPtr->data)
3 dataptr = dataptr-> link
5 end loop
6 print (**** End Data Print ****)
3 end if
4 retrun
No comments:
Post a Comment