生活随笔
收集整理的這篇文章主要介紹了
线性表——链表删除
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
王道鏈表題1-4
#include <iostream>
#include <bits/stdc++.h>
using namespace std
;typedef int Elemtype
;
typedef struct Lnode{Elemtype data
;struct Lnode *next
;
}Lnode
,*Linklist
;
int a
[4]={1,2,3,4};
int n
=4;
void buildlist(Lnode
*L
){Lnode
*s
,*r
=L
;r
->data
=a
[0];if(n
==1)r
->next
=NULL;else for(int i
=1;i
<n
;i
++){s
=(Lnode
*)malloc(sizeof(Lnode
));s
->data
=a
[i
];r
->next
=s
;r
=r
->next
;}L
=(Linklist
)malloc(sizeof(Lnode
));L
->next
=NULL;}
Linklist
list_headinsert(Linklist
&L
){Lnode
*s
;int x
;L
=(Linklist
)malloc(sizeof(Lnode
));L
->next
=NULL;scanf("%d",&x
);while(x
!=9999){s
=(Lnode
*)malloc(sizeof(Lnode
));s
->data
=x
;s
->next
=L
->next
;L
->next
=s
;scanf("%d",&x
);}return L
;
}
Linklist
List_TailInsert(Linklist
&L
){int x
;L
=(Linklist
)malloc(sizeof(Linklist
));Lnode
*s
,*r
=L
;scanf("%d",&x
);while(x
!=9999){s
=(Lnode
*)malloc(sizeof(Lnode
));s
->data
=x
;r
->next
=s
;r
=s
;scanf("%d",&x
);}r
->next
=NULL;return L
;
}void disp(Lnode
*L
){Lnode
*s
=L
;while(s
){cout
<<(s
->data
)<<" ";s
=s
->next
;}cout
<<endl
;
}void deletex(Lnode
*&L
,int x
){if(L
==NULL)return;Lnode
*p
;if(L
->data
=x
){p
=L
;L
=L
->next
;free(p
);deletex(L
,x
);}elsedeletex(L
->next
,x
);}
void del_x_1(Linklist
&L
,Elemtype x
){Lnode
*p
=L
->next
,*pre
=L
,*q
;while(p
!=NULL){if(p
->data
==x
){q
=p
;p
=p
->next
;pre
->next
=p
;free(q
);}else{pre
=p
;p
=p
->next
;}}
}
void R_Print(Lnode
*&L
){Lnode
*p
=L
;if(p
!=NULL){R_Print(L
->next
);printf("%d ",p
->data
);}
}
void del_min(Linklist
&L
){Lnode
*pre
=L
,*p
=L
->next
,*q
=L
->next
;while(q
!=NULL){if((q
->data
)<(p
->next
->data
)){p
=pre
;pre
=q
;q
=q
->next
;}else{pre
=q
;q
=q
->next
;}}q
=p
->next
;p
->next
=p
->next
->next
;free(q
);
}int main() {Lnode list
;Lnode
*L
=&list
;List_TailInsert(L
);del_min(L
);disp(L
);return 0;
}
總結
以上是生活随笔為你收集整理的线性表——链表删除的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。