鏈表節點的刪除(刪除鏈表當中最大值,如果有重複值只刪除一個)

#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("%d\n",p->data);

       }

   ElemSN * DelMaxNode(ElemSN*h){

         ElemSN * Pmax,* p,* Qmax,* q;             //p,q指針聯動,Pmax,Qmax也是指針聯動  Pmax表及鏈表中的最大結點,Qmax標記鏈表中最大節點的上一結點

Pmax=h;

         for(q=h,p=h->next;p;q=p,p=p->next){        //兩指針聯動

  if(Pmax->data<p->data){

        Pmax=p;

        Qmax=q;

  }

  }

 if(Pmax!=h)

Qmax->next=Pmax->next;

 else

    h=h->next;

         free(Pmax);

         return h;

   }

   int main(void){

int a[]={3,2,5,8,4,7,9,6,1};

         ElemSN * head;

head=Createlink(a);

head=DelMaxNode(head);

Printlink(head);

   }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章