利用尾插法建立單鏈表 2.設(shè)計一個算法,將單鏈表中結(jié)點以逆序排列。逆序的單鏈表中的結(jié)點均為原表中的結(jié)點?
2.設(shè)計一個算法,將單鏈表中結(jié)點以逆序排列。逆序的單鏈表中的結(jié)點均為原表中的結(jié)點?Typedef struct listnode{listnode*nextelement data}listnode,
2.設(shè)計一個算法,將單鏈表中結(jié)點以逆序排列。逆序的單鏈表中的結(jié)點均為原表中的結(jié)點?
Typedef struct listnode{
listnode*next
element data
}listnode,*plist
這是三種不同的單鏈表逆序算法,兩種遞歸算法和一種非遞歸算法
plist reverselist(plist head){
if(!Head |(Head->next))
return Head
plist pH=reverselist(Head->next)
Head->next->next=Head
Head->next=null
return Head pH
}
plist reverselist(plist Head,plist&tail){
if(!頭| |?。╤ead->next){
tail=head
return head
}
pList pt
pList ph=ReverseList(head->next,pt)
pt->next=head
head->next=NULL
tail=head
return ph
}
pList reverselistunrec(pList head){
if(!頭| |?。╤ead->next))
返回head
pList h=NULL,h1=head
而(head){
h1=head->next
head->next=h
h=head
head=h1
}
返回h
}