How can I optimize this code to print in this format?

VIshu Kamble

I want to print a linked list like this:

0       1       2       3       4
12      24      36      85      48

My current code is

void LinkedList::printList()
{
    curr = head; //store head in curr to irerate
    int count = 0; //counter to help uer manipulate list
    while (curr != NULL) //traverse whole list
    {
        cout << count++ <<"\t"; //print index
        curr = curr->next; //move to next 
    }
    curr = head; //make current head again
    cout << endl;//go to next line
    while (curr != NULL) //traverse whole list
    {
        cout << curr->data << "\t"; //print data
        curr = curr->next; //move to next 
    }
    cout << endl;
}

I am pretty sure there's another way to do this is a simpler and faster way. I want to reduce the redundancy on this code.

I am showing the counter to help user add or delete numbers.

paddy

It really depends on what you mean by "optimize". Linked lists are inherently suboptimal for traversal because of poor cache locality. Even less optimal is converting integer data to text and writing to a stream.

So I can only conclude that you wish to reduce code redundancy and consider that to be an optimization, even if it's at the expense of execution time. One possible solution is to accept a function that is applied to each element:

void LinkedList::forEach( std::function<void (node*)> fn )
{
    for( node *curr = head; curr != NULL; curr = curr->next )
    {
        fn( curr );
    }
}

And now you can use this to print node data or other stuff:

void LinkedList::printList()
{
    int count = 0;
    forEach( [&count]( node * ) { cout << count++ << "\t"; } );
    cout << endl;
    forEach( []( node *n ) { cout << n->data << "\t"; } );
    cout << endl;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I print a circular structure in a JSON-like format?

From Java

Is there any way I can optimize this R code?

From Dev

How can I optimize this code for creating TIFF files and/or what alternate libraries should I consider to improve performance?

From Dev

Can I Optimize this code with better array manipulation?

From Dev

How could I optimize the following piece of code?

From Dev

Can I use a loop to optimize my code?

From Dev

How do I format code to print with the same width every time?

From Dev

How can I optimize my code for my Spanish Translation Program?

From Dev

How can I optimize This Code Into Lesser Line of Code

From Dev

How can i optimize cycles?

From Dev

How can I optimize my code such that I am able to use a loop to plot histograms in subplots?

From Dev

How can I optimize my EF Code First query?

From Dev

How can I optimize this code with ARM NEON?

From Dev

How can I print this?

From Dev

How can i optimize my Jquery code?

From Dev

How can I optimize the code

From Dev

How can I print well formatted code

From Dev

how can i modifty this code to print the following sequence?

From Dev

How do I optimize this simple matlab code?

From Dev

How can i optimize this regex?

From Dev

How can I print out a matrix in the following format in prolog

From Dev

how i can optimize this java code?

From Dev

How can I optimize the 'IN' query?

From Dev

How can I optimize this android code?

From Dev

How can I format a list to print?

From Dev

How can i calculate the response time in the below code and print it on the console

From Dev

How can I optimize my code to answer the path numbers I can go from A to B?

From Dev

how can I print ascii code value in c++ in this way?

From Dev

How can I optimize my Two Sums code in Python?

Related Related

  1. 1

    How can I print a circular structure in a JSON-like format?

  2. 2

    Is there any way I can optimize this R code?

  3. 3

    How can I optimize this code for creating TIFF files and/or what alternate libraries should I consider to improve performance?

  4. 4

    Can I Optimize this code with better array manipulation?

  5. 5

    How could I optimize the following piece of code?

  6. 6

    Can I use a loop to optimize my code?

  7. 7

    How do I format code to print with the same width every time?

  8. 8

    How can I optimize my code for my Spanish Translation Program?

  9. 9

    How can I optimize This Code Into Lesser Line of Code

  10. 10

    How can i optimize cycles?

  11. 11

    How can I optimize my code such that I am able to use a loop to plot histograms in subplots?

  12. 12

    How can I optimize my EF Code First query?

  13. 13

    How can I optimize this code with ARM NEON?

  14. 14

    How can I print this?

  15. 15

    How can i optimize my Jquery code?

  16. 16

    How can I optimize the code

  17. 17

    How can I print well formatted code

  18. 18

    how can i modifty this code to print the following sequence?

  19. 19

    How do I optimize this simple matlab code?

  20. 20

    How can i optimize this regex?

  21. 21

    How can I print out a matrix in the following format in prolog

  22. 22

    how i can optimize this java code?

  23. 23

    How can I optimize the 'IN' query?

  24. 24

    How can I optimize this android code?

  25. 25

    How can I format a list to print?

  26. 26

    How can i calculate the response time in the below code and print it on the console

  27. 27

    How can I optimize my code to answer the path numbers I can go from A to B?

  28. 28

    how can I print ascii code value in c++ in this way?

  29. 29

    How can I optimize my Two Sums code in Python?

HotTag

Archive