溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

鏈表結(jié)點(diǎn)的刪除(有重復(fù))

發(fā)布時(shí)間:2020-06-30 04:32:39 來源:網(wǎng)絡(luò) 閱讀:351 作者:閆寶通 欄目:編程語言
#include<stdio.h>
#include<stdlib.h>
#define N 9
typedef struct node{
   int  data;
   struct node * next;
}ElemSN;
ElemSN  * Createlink(int a[],int n){            //逆向創(chuàng)建單向鏈表
    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)
	  pintf("%2d\n",p->data);
   }
 ElemSN * DelSamenode(ElemSN*h,int key){  
	 ElemSN * p,* q;
	 p=h;
	 while(p){              //p不為空
	  if(p->data!=key) {            //未找到key
	    q=p;            //兩指針聯(lián)動(dòng)
		p=p->next;
	  }                            
      else{                         //key找到p指針指著key
    	if(p!=h){                  //判斷是否為頭結(jié)點(diǎn) 不是頭結(jié)點(diǎn)
    	  q->next=p->next;
    	    free(p);
    	    p=q->next;
    	}
	    else{                        //是頭結(jié)點(diǎn)
		   h=h->next;
		  free(p);
		  p=h;
		}
	  }
	}
	return h;
  }
int main(void){
	 int a[]={3,2,9,8,9,7,9,6,1};
	 int key;
     ElemSN * head;
	 head=Createlink(a,9);
	 printf("key=");
	 scanf("%2d",&key);
     head=DelSamenode(head,key);
	 Printlink(head);
 }


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI