新聞中心
C語言單鏈表的刪除指定元素操作。
你的DeleteElem我有些看不懂
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比塔河網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式塔河網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋塔河地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
根據(jù)你想要的功能,給改了一下--
#include
#include
typedef
int
ElemType;
typedef
void
Status;
typedef
struct
Node
{
ElemType
data;
struct
Node
*next;
}LNode,
*LinkList;
Status
CreatList(int
,LinkList
);
Status
Traverse(LinkList
);
Status
FreeList(LinkList
);
ElemType
DelElem(LinkList
,ElemType
);
int
main()
{
int
Length;
ElemType
e;
LinkList
L;
L=(LinkList)malloc(sizeof(LNode));
L-next=NULL;
printf("Input
the
Length
of
List:\n");
scanf("%d",Length);
CreatList(Length,L);
printf("Travers
the
List
:\n");
Traverse(L);
while(1)
{
printf("請輸入要?jiǎng)h除的元素:\n");
scanf("%d",e);
if(DelElem(L,e))//在L中將元素e刪除
{printf("刪除%d后:\n",e);break;}
printf("未找到該元素,刪除失敗\n");
}
Traverse(L);
FreeList(L);
printf("List
release
Success!\n");
//system("PAUSE");return
0已經(jīng)使
程序暫停
了
return
0;
}
Status
CreatList(int
Length,LinkList
L)
{
int
i;
LinkList
Body=NULL,p=L;
for
(i=0;i
next=Body;
printf("Input
the
%dth
num:\n",i+1);
scanf("%d",(Body-data));
p=Body;
}
Body-next=NULL;
}
Status
FreeList(LinkList
L)
{
LinkList
temp=NULL,p=L;
while
(p)
{
temp=p-next;
free(p);
p=temp;
}
}
Status
Traverse(LinkList
L)
{
LinkList
p=L;
p=p-next;
while(p)//while(p!=NULL)
{
printf("%d
",p-data);
p=p-next;
}
printf("\n");
}
ElemType
DelElem(LinkList
L,ElemType
e)
{
LinkList
p,q;
q=p=L;
p=p-next;//p指向
頭結(jié)點(diǎn)
后第一個(gè)元素
while(p)
{
if(p-data==e)
{
q-next=p-next;
free(p);
return
1;
}
else{
q=p;//q始終指向p上一個(gè)結(jié)點(diǎn)
p=p-next;
}
}
//沒有執(zhí)行上個(gè)return,說明沒找到
return
0;
}
對單鏈表l進(jìn)行刪除數(shù)據(jù)元素x的c語言算法
假設(shè)有鏈表1
頭結(jié)點(diǎn)指針為h,節(jié)點(diǎn)類型為
struct
node{
int
date;
struct
node*
next;
}創(chuàng)建鏈表過程就不寫了,親。
下面是刪除元素x的C語言算法(將它寫成函數(shù)形式):
void
Delete_X(struct
node*
h,int
x)
{
struct
node
*p,*q;
for(p=h,q=NULL;p;q=p,p=p-next){
if(p-date==x){
if(!q){
//如果刪除的元素是頭結(jié)點(diǎn)
h=q-next;
p-next=NULL;
}
//endif
else{
q-next=p-next;
p-next=NULL;
}
//endelse
}
//endif
}
//endfor
}
//end
C語言鏈表 刪除
struct?LNode?*?delete(LNode?*?head)
{
LNode?*?node?=?head;
LNode?**?parent?=?head;
double?min,?max;
printf("請輸入min和max:?");
scanf("%lf?%lf",?min,?max);
while?(node)
{
//?大于min和小于max,則刪除節(jié)點(diǎn)
if?(node-data??min??node-data??max)
{
*parent?=?node-next;
//?如果你的LNode是malloc出來的,需要free(node);
node?=?*parent;
}
else
{
node?=?node-next;
parent?=?node-next;
}
}
return?head;
//?這個(gè)邏輯不需要給你太多注釋,你只需要拿一支筆,手動(dòng)畫一個(gè)3個(gè)節(jié)點(diǎn)的鏈表,????
//?然后從函數(shù)執(zhí)行開始,一步一步去推怎么執(zhí)行的就懂了。
//?至于你的程序的錯(cuò)誤,沒幫你看
}
c語言單項(xiàng)鏈表的刪除
最后面倒數(shù)第三行。 p-data==x 當(dāng)節(jié)點(diǎn)不存在的時(shí)候p為NULL所以這時(shí)候取data錯(cuò)誤,
另外該代碼還存在內(nèi)存泄露的問題(沒有改)。
正確的該法:
struct?node
{
int?data;
struct?node?*next;
};
typedef?struct?node?SLIST;
#includestdio.h
#includemalloc.h
void?main()
{
void?delete_snode(SLIST?*head,int?x);
void?insert_slist(SLIST?*head?,int?x,int?y);
void?print_slist(SLIST?*head);
SLIST?*creat_slist();
SLIST?*h;
int?x,y;
int?m;
scanf("%d%d",x,y);
scanf("%d",m);
h=creat_slist();
print_slist(h);
insert_slist(h,x,y);
print_slist(h);
delete_snode(h,m);
print_slist(h);
}
SLIST?*creat_slist()
{
int?c;
SLIST?*h,*s,*r;
h=(SLIST?*)malloc(sizeof(SLIST));
r=h;
scanf("%d",c);
while(c!=-1)
{
s=(SLIST?*)malloc(sizeof(SLIST));
s-data=c;
r-next=s;
r=s;
scanf("%d",c);
}
r-next='\0';
return?h;
}
void?print_slist(SLIST?*head)
{
SLIST?*p;
p=head-next;
if(p=='\0')printf("no\n");
else
{
printf("head");
do
{
printf("-%d",p-data);
p=p-next;
}while(p!='\0');
printf("end\n");
}
}
void?insert_slist(SLIST?*head?,int?x,int?y)
{
SLIST?*s,*p,*q;
s=(SLIST??*)malloc(sizeof(SLIST));
s-data=y;
q=head;
p=head-next;
while(p!='\0'p-data!=x)
{
q=p;p=p-next;
}
s-next=p;
q-next=s;
}
void?delete_snode(SLIST?*head,int?x)
{
SLIST?*p,*q;
q=head;
p=head-next;
while(p!='\0'p-data!=x)
{
q=p;
p=p-next;
}
if(p)
if(p-data==x)
q-next=p-next;
}
網(wǎng)頁題目:c語言單向鏈表的刪除函數(shù) c語言用鏈表刪除函數(shù)怎么寫
URL鏈接:http://fisionsoft.com.cn/article/hpeegh.html