Print an ordered linked list

后端 未结 2 352
南旧
南旧 2021-01-23 13:27

Just did some editing on it, i tried what you said but it didnt work, so i tried something i am a little more familiar with but it doesnt seem to work correctly. It prints the i

2条回答
  •  孤独总比滥情好
    2021-01-23 13:36

    I would suggest creating an iterator that starts at the first node and goes to the next node until the next node is null and suggest using a like next and not end of list (or has next).

    Then to print you simple continue through the iterator and print out the value. To insert you start at the head item and iterator through and compare the values.

    Added some pseudo code since I am not really a c++ programmer.

    class iterator
    {
        //provide a construction method for this
        listNode current = Head;
        listNode getValue() 
        {
            return current;
        }
    
        void next()
        {
            //probably want to include some checks for validity here
            current = current->next;
        }
    
        boolean hasNext()
        {
            return current->next != null;
        }
    }
    

提交回复
热议问题