0
    struct NODE{
        int data;
        NODE *next;
    };
    int main()
    {
            NODE * n; 
            NODE * pHead =NULL;
            NODE * pTail =NULL;

            cout<<"Enter Data: [Y]|[N] ";
            cin>>option;

            while(option!='n')
            {
                n = new NODE; //creating a new node
                cout<<"\nEnter Some Data: "; //asking for users to enter data
                cin>>data;

                n->data = data; //storing data

                if (pHead ==NULL) //checking if head is -1 / NULL
                {
                    pHead = n;  //set head to the first node created 
                }else{
                    pTail->next = n;
                }
                pTail = n;
                n->next = NULL; //set next to -1 / NULL 


                cout<<"\nEnter More Data: [Y]|[N] "; //ask if the user wants to continue or no
                cin>>option;
            }

            //Front Order using For Loop
            cout<<"\nFront Order Using For Loop: "; 
             for (n=pHead; n!=NULL; n=n->next)
             {
                cout<<n->data<<" "; //Print Data
             }
       return 0 ;
    }

input 1 2 3 4

output Front Order Using For Loop: 1 2 3

my question is how can i use the for loop to print the reverse order i am able to do the recursive one but unable to use a for loop to print the reverse order

  • This is a singly linked list. So, you need to go through the list and save the data and print in reverse order later. You are printing the data when you are going through the list. You can either use doubly linked list or you need extra memory to save the reverse list. – Wasi Ahmad Oct 31 '16 at 02:21
  • "without Functions" - how about you take the body of the function and copy it in the main and rename the parameters to your local variables? – Adrian Colomitchi Oct 31 '16 at 02:40
  • @jake123 -- Seriously, if the OP stated what you stated, I would have downvoted due to laziness. As the other comment stated, what's so hard about copying and pasting the code inside of main, and replacing the parameters with local variables? – PaulMcKenzie Oct 31 '16 at 02:49

0 Answers0