#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, * Pkey;//p,q指針關聯 Pkey哨兵,p指針的值與PKey比較,相等就删除,不相等pq關聯
Pkey=h;
while(Pkey){
q=Pkey;
p=Pkey->next;
while(p){
if(Pkey->data!=p->data){ //不等,pq關聯
q=p;
p=p->next;
}
else{
q->next=p->next;
free(p); //先釋放p,p再後移,否則剩餘的鍊就挂在了p指針後面以釋放後面的鍊就找不到頭指針,屬于遊離狀态
p=q->next;
}
}
Pkey=Pkey->next; //Pkey每次後移一位
}
}
int main(void){
int a[]={2,2,3,4,2,3,3,2,5};
ElemSN * head;
head=Createlink(a,9);
head=DelSamenode(head);