#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct string{
	char ch;
	struct string * next;
};
void CreatList(struct string **headp);
void PrintString(struct string *head);
struct string * DeleteNode(struct string **headp,char n);
void add(struct string **headp,char n);
int main()
{
	char NewCh;
	struct string *head=NULL;
	printf("Please input a string.\n");
	CreatList(&head);
	PrintString(head);
	printf("Input a char\n");
	scanf("%c",&NewCh);
	struct string *aim=NULL;
	aim=DeleteNode(&head,NewCh);
	if(aim==NULL)
	{
		add(&head,NewCh);
	}
	PrintString(head);
	return 0;
}
void CreatList(struct string **headp)
{
	struct string * LocHead=NULL,*tail;
	LocHead=(struct string *)malloc(sizeof(struct string));
	scanf("%c",&LocHead->ch);
	tail=LocHead;
	while(tail->ch!='\n')
	{
		tail->next=(struct string *)malloc(sizeof(struct string));
		tail=tail->next;
		scanf("%c",&tail->ch);
	}
//	tail->ch='\0';
	tail->next=NULL;
	*headp=LocHead;
}
void PrintString(struct string *head)
{
	while(head!=NULL)
	{
		printf("%c",head->ch);
		head=head->next;
	}
	printf("\n");
}
struct string * DeleteNode(struct string **headp,char n)
{
	struct string *current=*headp,*prior=*headp;
	while(current!=NULL&&current->ch!=n)
	{
		prior=current;
		current=current->next;
	}
	if(!current)
		return NULL;
	if(current==*headp)
		*headp=current->next;
	else
		prior->next=current->next;
	free(current);
	return current;
}
void add(struct string **headp,char n)
{
	struct string *current=*headp,*tail=*headp,*flag=NULL,*NewString;
	int delt,min=100;
	while(tail!=NULL)
	{
		delt=abs((int)(current->ch-n));
		if(delt<min)
		{
			min=delt;
			flag=current;
		}
		current=tail;
		tail=tail->next;
	}
	NewString=(struct string *)malloc(sizeof(struct string));
	NewString->ch=n;
	NewString->next=flag->next->next;
	flag->next=NewString;
}
