title: I have a problem with swap code in Doubly linked list
body: <p>I made swap code in C
  but I didn't get perfect grade in school compiler.
  I think there is a problem with following code.
  I made doubly linked list with struct and pointer.
  Node is defined that:</p>
  <pre><code>struct node* newnode(char x)
  {
      struct node* new = (struct node*)malloc(sizeof(struct node));
      new-&gt;next = NULL;
      new-&gt;prev = NULL;
      new-&gt;c = x;
  
      return new;
  }
  </code></pre>
  <p>And the following code is swap code that I made each function factor mean that &quot;struct node*p&quot; is full list from head to tail, &quot;int a and b&quot; is rank of list
  and &quot;count1(p)&quot; is function that count node in full list and return number of node (exclude head and tail).</p>
  <pre><code>void swap(struct node* p, int a, int b)
  {
      int x, y; 
      struct node* x1, * x2, emp = { 0 };
      int i = 0;
      x1 = p;
      x2 = p;
  
      if (a &gt;= b) {
          x = b;
          y = a;
      }
      else {
          x = a;
          y = b;
      }
  if (b &lt;= 0 || a &lt;= 0 || a &gt; N || a &gt; count1(p) || b &gt; N || b &gt; count1(p)) {
          printf(&quot;invalid position\n&quot;);
          return;
      }while (i &lt; x &amp;&amp; x1 != tail) {
          x1 = x1-&gt;next;
          i++;
      }
      i = 0;
      while (i &lt; y &amp;&amp; p != tail) {
          x2 = x2-&gt;next;
          i++;
      }
      if (x1-&gt;next != x2)
      {
          x1-&gt;prev-&gt;next = x2;
          x1-&gt;next-&gt;prev = x2;
          x2-&gt;prev-&gt;next = x1;
          x2-&gt;next-&gt;prev = x1;
          emp.next = x1-&gt;next;
          emp.prev = x1-&gt;prev;
          x1-&gt;next = x2-&gt;next;
          x1-&gt;prev = x2-&gt;prev;
          x2-&gt;next = emp.next;
          x2-&gt;prev = emp.prev;
      }
      if (x1-&gt;next == x2)
      {
          emp.prev = x1-&gt;prev;
          emp.next = x1-&gt;next;
          x1-&gt;prev-&gt;next = x2;
          x2-&gt;next-&gt;prev = x1;
          x1-&gt;prev = x2;
          x1-&gt;next = x2-&gt;next;
          x2-&gt;prev = emp.prev;
          x2-&gt;next = x1;
  
  
      }
  
  }
  </code></pre>
  <p>The following code is my full code:</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  int N;
  struct node {
      struct node* next;
      struct node* prev;
      char c;
  };
  struct node* head, * tail;
  
  void reset() {
  
      head = (struct node*)malloc(sizeof(struct node));
      tail = (struct node*)malloc(sizeof(struct node));
      head-&gt;prev = NULL;
      head-&gt;next = tail;
      head-&gt;c = NULL;
      tail-&gt;prev = head;
      tail-&gt;next = NULL;
      tail-&gt;c = NULL;
  }
  struct node* newnode(char x)
  {
      struct node* new = (struct node*)malloc(sizeof(struct node));
      new-&gt;next = NULL;
      new-&gt;prev = NULL;
      new-&gt;c = x;
  
      return new;
  }
  void prcount(struct node* p)
  {
  
      printf(&quot;%d\n&quot;, count1(p));
  }
  int count1(struct node* p)
  {
      int sum = 0;
  
      while (p != tail) {
          p = p-&gt;next;
  
          sum++;
  
      }
      return sum + -1;
  }
  void add1(struct node* p, int n, char x)
  {
      struct node* new1 = newnode(x);
      int i = 1;
      if (n &lt;= 0 || n &gt; N || n &gt; count1(p) + 1) {
          printf(&quot;invalid position\n&quot;);
          return;
      }
      while (i &lt; n &amp;&amp; p != tail) {
          p = p-&gt;next;
  
          i++;
  
      }
      p-&gt;next-&gt;prev = new1;
      new1-&gt;prev = p;
      new1-&gt;next = p-&gt;next;
      p-&gt;next = new1;
  
  }
  void delete1(struct node* p, int n)
  {
      int i = 0;
      if (n &lt;= 0 || n &gt; N || n &gt; count1(p)) {
          printf(&quot;invalid position\n&quot;);
          return;
      }
      while (i &lt; n &amp;&amp; p != tail)
      {
          p = p-&gt;next;
          i++;
      }
      p-&gt;prev-&gt;next = p-&gt;next;
      p-&gt;next-&gt;prev = p-&gt;prev;
      free(p);
  
  
  }
  void get1(struct node* p, int n) {
      int i = 0;
      if (n &lt;= 0 || n &gt; N || n &gt; count1(p)) {
          printf(&quot;invalid position\n&quot;);
          return;
      }while (i &lt; n &amp;&amp; p != tail) {
          p = p-&gt;next;
          i++;
      }
      printf(&quot;%c\n&quot;, p-&gt;c);
  }
  void print1(struct node* p)
  {
      int i = 0;
      if (count1(p) == 0) {
          printf(&quot;invalid position\n&quot;);
          return;
      }
      while (i &lt; N &amp;&amp; p-&gt;next != tail)
      {
          p = p-&gt;next;
          printf(&quot;%c&quot;, p-&gt;c);
          i++;
      }
      printf(&quot;\n&quot;);
  
  }void removeall(struct node* p)
  {
  
      struct node* emp;
      p = p-&gt;next;
  
  
      while (p != tail)
  
      {
          emp = p-&gt;next;
          free(p);
          p = emp;
      }
      head-&gt;prev = NULL;
      head-&gt;next = tail;
      head-&gt;c = NULL;
      tail-&gt;prev = head;
      tail-&gt;next = NULL;
      tail-&gt;c = NULL;
  }
  void swap(struct node* p, int a, int b)
  {
      int x, y;
      struct node* x1, * x2, emp = { 0 };
      int i = 0;
      x1 = p;
      x2 = p;
  
      if (a &gt;= b) {
          x = b;
          y = a;
      }
      else {
          x = a;
          y = b;
      }
  
      if (b &lt;= 0 || a &lt;= 0 || a &gt; N || a &gt; count1(p) || b &gt; N || b &gt; count1(p)) {
          printf(&quot;invalid position\n&quot;);
          return;
      }while (i &lt; x &amp;&amp; x1 != tail) {
          x1 = x1-&gt;next;
          i++;
      }
      i = 0;
      while (i &lt; y &amp;&amp; x2 != tail) {
          x2 = x2-&gt;next;
          i++;
      }
      if (x1-&gt;next != x2)
      {
          x1-&gt;prev-&gt;next = x2;
          x1-&gt;next-&gt;prev = x2;
          x2-&gt;prev-&gt;next = x1;
          x2-&gt;next-&gt;prev = x1;
          emp.next = x1-&gt;next;
          emp.prev = x1-&gt;prev;
          x1-&gt;next = x2-&gt;next;
          x1-&gt;prev = x2-&gt;prev;
          x2-&gt;next = emp.next;
          x2-&gt;prev = emp.prev;
      }
      if (x1-&gt;next == x2)
      {
          emp.prev = x1-&gt;prev;
          emp.next = x1-&gt;next;
          x1-&gt;prev-&gt;next = x2;
          x2-&gt;next-&gt;prev = x1;
          x1-&gt;prev = x2;
          x1-&gt;next = x2-&gt;next;
          x2-&gt;prev = emp.prev;
          x2-&gt;next = x1;
  
  
      }
  
  }
  int main()
  {
      int i;
      int r, n;
      char x, y;
  
      reset();
  
      scanf(&quot;%d&quot;, &amp;N);
      for (i = 0; i &lt; N; i++)
      {
          scanf(&quot; %c&quot;, &amp;x);
          if (x == 'A') {//A mean add(head,int a, char x) -&gt; add 'x' on a rank in list
              scanf(&quot;%d&quot;, &amp;r);
  
              scanf(&quot; %c&quot;, &amp;y);
  
              add1(head, r, y);
          }
          else if (x == 'D') {// D mean delete(struct node *head, int a) -&gt; delete element on int a rank
              scanf(&quot;%d&quot;, &amp;r);
  
              delete1(head, r);
          }
          else if (x == 'G') {//G mean Get(head,int a)-&gt; print element on int a rank
              scanf(&quot;%d&quot;, &amp;r);
              get1(head, r);
  
          }
          else if (x == 'P') {//P mean print(head) -&gt; print all element in list
              print1(head);
          }
          else if (x == 'R') {//R mean Removeall(head) -&gt; remove all node (exclude head and tail0
              removeall(head);
          }
          else if (x == 'C')//C Mean prcount(head) -&gt; count and print number of node in list (exclude head and tail)
          {
              prcount(head);
          }
          else if (x == 'S') {//S mean Swap(head, int a, int b) -&gt; swap element of int a rank with int b rank 
              scanf(&quot;%d&quot;, &amp;r);
              scanf(&quot;%d&quot;, &amp;n);
              swap(head, r, n);
          }
      }
      removeall(head);
  }
  </code></pre>
  <p>It's  my full code.
  I want to know is there any problem Or more effective way to make swap code?</p>