//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char chr;
struct node *next;
} node,*list;
list read_str(void) //读取一个字符串
{
char c;
list now,a,str=NULL;
while ((c=getchar())!='\n')
{
now=(list)malloc(sizeof(node));
if (now==NULL) exit(-1);
now->chr=c;
now->next=NULL;
if (str) {
a->next=now;
a=now;
}
else
str=a=now;
}
return str;
}
list remove_chr(list lt,const char ch) //从字符串lt中删除字符ch,并返回删除字符之后的字符串
{
list beg=NULL,now,t=lt;
if (lt->chr==ch) {
beg=lt;
lt=lt->next;
}
while (t->next)
{
if (t->next->chr==ch) {
now=t->next;
t->next=now->next;
free(now);
}
t=t->next;
}
if (beg) {
free(beg);
}
return lt;
}
void write_str(list lt) //输出字符串lt
{
while (lt){
putchar(lt->chr);
lt=lt->next;
}
}
void free_list(list a) //释放a占用的空间
{
list t;
while (a)
{
t=a;
a=a->next;
free(t);
}
}
int main(void)
{
char ch;
list str;
str=read_str();
ch=getchar();
write_str(str=remove_chr(str,ch));
free_list(str);
return 0;
}
//---------------------------------------------------------------------------
温馨提示:内容为网友见解,仅供参考