singly linked chain printing c++

后端 未结 6 492
刺人心
刺人心 2021-01-28 18:39

I am trying to pick my chain in the format {1,2,3,4,etc}. You can find the header file below which will have the layout of the nodes. I am just confused on how I should go about

6条回答
  •  情歌与酒
    2021-01-28 18:59

    Here are two recommendations: 1) Sort the list first, then print all nodes; 2) Create another list (indices) to the data and sort those links (don't need data in those nodes).

    Sorting List First

    An often used technique is to order the nodes in the order you want them printed. This should involve changing the link fields.
    Next, start at the head node and print each node in the list (or the data of each node in the list).

    Using an Index list

    Create another linked list without the data fields. The links in this list point to the data fields in the original list. Order the new list in the order you want the nodes printed.
    This technique preserves the order of creation of the first list and allows different ordering schemes.

    Changing Links

    Since you're writing your own Linked List, the changing of the links is left as an exercise as I'm not getting paid to write your code. There are many examples on SO as well as the web for sorting and traversing linked lists.

提交回复
热议问题