title: C. Linked lists. Inserting an element before another
body: <p>Right now I have a program (shown below) that inserts an element (E) after (F). For example if we input <code>n = 4</code> and then insert 4 elements <code>2 3 3 4</code>, followed by our 'F' value which is <code>3</code> and E value which is <code>6</code>, the program will edit the list inserting E (6) after F (3), resulting in a list edition from  <code>2 3 3 4</code> into <code>2 3 6 3 4</code></p>
  
  <p>I am looking for a way to insert a value 'E' BEFORE an F, not after. Could anyone explain  what changes may be required to this code to achieve the same?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  struct sarasas { int duomuo; struct sarasas *next, *prev; };
  typedef struct sarasas Sarasas;
  typedef Sarasas *SarasasPtr;
  
  int main()
  {
      int d,i,n,j,e,f;
      SarasasPtr Sar,x,temp,prev=NULL;
      SarasasPtr head= NULL;
      printf("Insert 'n' \n");
      scanf("%d",&amp;n);
      for (i=0; i&lt;n; i++) {
          printf("Insert an element: \n");
          scanf("%d",&amp;d);
          x=(SarasasPtr) malloc(sizeof( Sarasas ));
          x-&gt;duomuo = d;
          x-&gt;next = NULL;
  
          if(head == NULL) {
            head = x;
          } else {
            temp = head;
            while(temp!=NULL) {
                prev = temp;
                temp = temp-&gt;next;
            }
            prev-&gt;next = x;
          }
      }
  
     printf("Insert an element after which one you want another element to be inserted\n");
     scanf("%d",&amp;e);
     printf("Insert element which you want to insert\n");
     scanf("%d",&amp;f);
  
     temp = head;
     int pakeista = 0;
  
     while(temp!=NULL) {
       //printf("%d",temp-&gt;duomuo);
       if (temp-&gt;duomuo == e) {
         if (changed == 0) {
           x=(SarasasPtr) malloc(sizeof( Sarasas ));
           x-&gt;duomuo=f;
           x-&gt;next=temp-&gt;next;
           temp-&gt;next=x;
           n++;
           changed = 1;
         }
       }
       temp=temp-&gt;next;
     }
     Sar = head;
     for (i=0; i&lt;n; i++) {
        printf("Sk Nr. %i: %d\n",i+1,Sar-&gt;duomuo);
        Sar = Sar-&gt;next;
     }
  }
  </code></pre>