2

I was using list_for_each_entry(datastructureptr , &mylinkedlist, head) to traverse a linked list i created. The output I got was last inserted item is printed first. Is it the expected output. Is there any macro/function to print it in the other way?

2 Answers 2

2

list_for_each_entry traverses a list from the first to the last item.

You must ensure that the items are inserted in the correct order and at the correct position. list_add inserts at the start of the list; list_add_tail at the end.

Sign up to request clarification or add additional context in comments.

1 Comment

I gave the second argument of list_add as the list head so got the output like that. Thanks for the info.
1
struct private {
        char data[30];
    struct list_head head;
    }; 
.......
.....
static struct private mylinkedlist;
....
struct private *datastructureptr=&mylinkedlist;
...
   list_for_each_entry(datastructureptr , &mylinkedlist, head)
       printk( " %s->\n",datastructureptr->data); 

prints items in fifo way

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.