Traversing a single linked list in order

情到浓时终转凉″ 提交于 2021-02-08 10:33:33

问题


I have been trying to think a way to traverse a single linked list.

This is so far what I have done:

#include <iostream>

typedef struct node {                                                               
      int data;               // will store information
      node *next;             // the reference to the next node
};  


int printList(node *traverse) {
    if (traverse->next == NULL) {
        return -1;
    }
    traverse=traverse->next;
    printList(traverse);
    cout << traverse->data << endl;
    return 0;
}

int main() {
    node *head = NULL;      
    for (int i = 0; i < 10; i++) {
        node *newEntry = new node;
        newEntry->data = i;
        newEntry->next = head;
        head = newEntry;
    }
    printList(head);
    return 0;
}

I cannot think a way to print the last digit(9) in printList() function. How would I be able to achieve this? My second question is, how can I traverse the same in a while loop rather than a recursive function.

As some of you tried to answer before, I am not looking to traverse this from 9 to 0, This should traverse from 0 to 9, you can see the output from http://codepad.org/ynEdGc9S


回答1:


There are a few things here:


In main() the way you are creating the list is incorrect. Draw what you're doing, you'll realize that your head is the last item in the list, i.e., it will probably have a value of 9. (Print out head's value just before you call printList to verify this).

Let me explain (follow along in your code) with iteration of i = 1:

Current state: head=[0]

  1. A new temporary node is allocated [ ]
  2. Then you assign data to it [1]
  3. Then you set this temporary node's next to your head [1]-->[0] ; head=[0]
  4. Then you set head to this temporary node [1]-->[0] ; head = [1]

So, you can see what's happening here. Head should still be [0] and its next should be [1] not the other way around.

You can explore and think about the correct way of doing this.


printList, this is printing out the recursive stack and not the traversal. Traversal would print them in reverse order because your list is in the reverse order (check the previous section ^ for why).

This is the correct way to print the link in a traversal. This will print the elements of the list in the way they are. When you checked for traverse->next==NULL, traverse held the last element. Since you just ended the recursion by returned -1, the last element was never printed.

int printList(node *traverse) {
   if (traverse == NULL) {  
       return -1;
    }
    cout << traverse->data << endl;
    printList(traverse->next);
    return 0;
}

Iterative

int printList(node *traverse) {
   while(traverse != NULL) {  
    cout << traverse->data << endl;
    traverse = traverse->next;
   }
}

Feel free to post questions, etc.




回答2:


Instead of if (traverse->next == NULL) try if (traverse == NULL)

This way, you print the current node if it's an actual node with data in it. You then recurse. Ultimately, at the end you will recurse into a NULL pointer, which you can easily escape.




回答3:


As an answer to the second part, your code could look like this:

void printList_iter(node* node)
{
    while (node)
    {
        cout << node->data << endl;
        node = node->next;
    }
}

This will loop through the list, printing each element until it gets to a NULL node, which signifies the end of the list. It's a pretty standard iteration algorithm.




回答4:


Why don't you interchange these statements?

traverse=traverse->next;
printList(traverse);
cout << traverse->data << endl;

This should be changed to:

cout << traverse->data << endl;
traverse=traverse->next;
printList(traverse);

This should work. And then, change

if(traverse->next==NULL)

to

if(traverse==NULL)


来源:https://stackoverflow.com/questions/17450085/traversing-a-single-linked-list-in-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!