下面这个链表是可以运行的,但结果不对啊!我是个菜鸟,请教各位大虾了! #include <stdio.h>
struct zm
{
char c;
struct zm *next;
};
extern struct zm *insert(struct zm *,char);
extern struct zm *delc(struct zm *,char);
extern void print(struct zm *);
main()
{
char choice,c;
struct zm *head;
head=NULL;
printf("Enter a choice:\n1.Insert a character!\n2.Delete a character!\n3.End of run!\n");
printf("?");
scanf("%c",&choice);
while(choice!='3')
{
switch(choice)
{
case '1':
printf("Insert a character:\n");
scanf("%c",&c);
head=insert(head,c);
print(head);
break;
case '2':
printf("Input the character you want to deleted:\n");
scanf("%c",&c);
head=delc(head,c);
print(head);
break;
default:
printf("Invalid choice!");
}
printf("?");
scanf("%c",&choice);
}
printf("End of run!\n");
return 0;
}
struct zm *insert(struct zm *head,char c)
{
struct zm *p1,*p2,*new;
new=(struct zm *)malloc(sizeof(struct zm));
p1=head;p2=head;
new->c=c;new->next=NULL;
if(head==NULL){head=new;}
else
{
while((new->c>p1->c)&&(p1->next!=NULL))
{
p2=p1;p1=p1->next;
}
if(new->c<=p1->c)
{
if(p1==head)
{new->next;head=new;}
else
{p2->next=new;new->next=p1;}
}
else
{p1->next=new;}
}
return (head);
}
struct zm *delc(struct zm *head,char c)
{
struct zm *p1,*p2,*temp;
p1=head;p2=head;
if(head==NULL){printf("The list is empty!Can't delete any character!\n");}
else
{
while((p1->c!=c)&&(p1->next!=NULL))
{
p2=p1;p1=p1->next;
}
if(p1->c==c)
{
if(head->c==c){head=p1->next;}
else{p2->next=p1->next;}
}
else
{
printf("The character is not exsit!\n");
}
}
return(head);
}
void print(struct zm *head)
{
struct zm *p;
if(head==NULL)
{printf("The list is:\nNULL\n");}
else
{
p=head;
printf("The list is:\n");
while(p!=NULL)
{
printf("%c-->",p->c);
p=p->next;
}
printf("NULL\n");
}
}
[此贴子已经被作者于2005-9-21 15:44:40编辑过]