求输入一个字符数组,若该数组有一个字符与x相同,则将y插在x之后;否则,插在表尾。
#include<stdio.h>#include<malloc.h>
struct link *DeleteNode(struct link *head,char y);
struct link
{
char data;
struct link *next;
};
void main()
{
struct link *head,*p,*q;
int i,n;
char y;
head=q=(struct link *)malloc(sizeof(struct link));
printf("Input the length of the line:");
scanf("%d",&n);
printf("Input %d datas:",n);
for(i=0;i<n;i++)
{
p=(struct link *)malloc(sizeof(struct link));
scanf("%c",&p->data);
p->next=NULL;
q->next=p;
q=p;
}getchar();
printf("Input y:");
scanf("%c",&y);
getchar();
p=head->next;
head=DeleteNode(head,y);
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}printf("\n");
}
struct link *DeleteNode(struct link *head,char y)
{
struct link *pr=head,*p=head,*temp=NULL;
char x;
pr=(struct link *)malloc(sizeof(struct link));
pr->next=NULL;
pr->data=y;
printf("Input x:");
scanf("%c",&x);
getchar();
while(p->data!=x && p->next!=NULL)
{
temp=p;
p=p->next;
}
if(p->data==x)
{
if(p==head)
{
pr->next=head->next;
head->next=pr;
}
else
{
p=temp->next;
pr->next=p->next;
p->next=pr;
}
}
else
{
p->next=pr;
}
return head;
}\\为什么得不到结果????请赐教,拜托了,现在急用!!!!!!!
[ 本帖最后由 longwang666 于 2015-5-27 12:18 编辑 ]