Sorted insert using Linked List (iterated function)

I have some difficulties to implement one iterative function for sorted insert in linked list. I have previously done that with recursion which is much easier when the insert() function is called, but here I am a bit confused with how to implement the (l->data < data) condition:

1
2
3
4
5
6
7
typedef struct E_Type * List;

struct E_Type
{
  int data;
  struct E_Type* next;
};


and the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
insert(List & l, int data){
  if (l == 0 || l->data > data){
    List new_list = new E_Type;
    new_list->data = data;
    new_list->next = l;
    l = new_list;
  }
  else if (l->data < data){
    List new_list = new E_Type; 
    new_list->data = data;
    new_list->next = l; //i am shooting in the dark with this one
    l = new_list;
  }
}


Topic archived. No new replies allowed.