#include<stdio.h>

#include<stdlib.h>

#define N 9

typedef struct node{

   int  data;

   struct node * next;

}ElemSN;

ElemSN  * Createlink(int a[]){            //逆向创建单向链表

    int i;

    ElemSN * h=NULL, * p;

    for( i=N-1;i>=0;i--){

          p=(ElemSN *)malloc(sizeof(ElemSN));

          p->data =a[i];

          p->next=h;

          h=p;

    }

    return h;

   }

   void Printlink(ElemSN * h){

           ElemSN * p;

           for(p=h;p;p=p->next)

           printf("%2d\n",p->data);

    }

 ElemSN * DelSameNode(ElemSN*h){

         ElemSN * p,* q;

         q=h;

         p=h->next ;        //p指针从头结点的下一个节点开始

         while(p){

               if(p->data!=q->data){  //升序排列,后面的data都大于等于当前节点data                              

                        q=p;                                 //(q指针指的是当前节点,p指针指着与当前节点要比较的结点)

                        p=p->next;                     // 不相等表示当前结点不重复,p指针后移,q指针联动

               }

               else{                           //如果相等,就断链

                    q->next=p->next; 

             free(p);  //先释放p,p再后移,否则剩余的链就挂在了p指针后面以释放后面的链就找不到头指针,属于游离状态

                    p=q->next;

               }

  }

  return h;

   }

  int main(void) {

      int a[]={2,2,3,3,4,4,5,5,6};

      ElemSN * head;

      head=Createlink(a,9);

      head=DelSameNode(head);

      Printlink(head);

  }