#include <iostream>
using namespace std;
 
struct Node
{
     int data;
     Node* next;
};
 
void add2front(int val, Node* &head);
void print(Node* head);
void insertInOrder(int val, Node* &head);
 
int main()
{
          Node *LIST = 0; 
  
          int x;
          cout << "Enter a list of positive numbers, terminated by zero:" << endl;
          cin >> x;
          while (x > 0)
          {
                 add2front(x, LIST);
                 cin >> x;
          }
 
          print(LIST);
 
          cout << "Enter node to be inserted: ";
          cin >> x;
          insertInOrder(x, LIST);
 
          print(LIST);
 
          return 0;
}
 
void add2front(int val, Node* &head)
{
          Node *temp = new Node;
          temp->data = val;
          temp->next = head;
          head = temp;
}
 
void print(Node* head)
{
        Node* p = head;
        while (p != 0)
        {
               cout << p->data << ' ';
               p = p->next;
        }
        cout << endl;
}
 
void insertInOrder(int val, Node* &head)
{
        Node* p = head;
 
        while (p->next->data < val)
        {
               p = p->next;
        }
        add2front(val, p->next);
 
}
 
 

syntax highlighted by Code2HTML, v. 0.9.1