原题:使用后置递归法求解删除带有头结点的单链表中所有值为x的数据元素的算法。
(1)采用后置递归算法删除指定结点
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
typedef struct LNode
{//定义表结点结构
char data;
struct LNode *next;
}LNode,*LinkList;
int CreateList(LinkList &head)//生成链表
{
srand((unsigned)time(NULL));
LinkList p=NULL,q=NULL;head=NULL;
for(int i=0;i<20;i++)
{
p=new LNode;
p->data=(char)(rand()%26+'a');
if(head == NULL)
head=p;
else
q->next=p;
q=p;
}
if(head!=NULL)
{
q->next=NULL;
}
head->data='*';//头结点数据域为特殊字符*号
return 1;
}
void DelListNode(LinkList &head,char ch)
{
LinkList h,p;
if(head->next)
{
if(head->next->data == ch)
{
//删除head->next
p=head->next;head->next=p->next;delete p;
h=head;//h为线性链表(a2,a3, ... an)的头指针
}
else
h=head->nex