???? C 链表!!急~~ 救救菜鸟!! 在线等~
题目是:
建立单向链表,连续输入10个字符创建链表,并实现在元链表中插入字符、删除字符、查找字符的功能。创建链表用creat_list、插入字符insert_list、查找字符用search_list、删除字符用delete_list。
编程如下:
# include <stdio.h>
# include <malloc.h>
# define NULL 0
# define LEN sizeof(struct txt)
struct txt
{
char a;
struct txt *next;
};
struct txt *creat_list(void) //创建
{
struct txt *head;
struct txt *p1,*p2;
int n=0;
p1=p2=(struct txt *)malloc(LEN);
scanf("%c",&p1->a);
head=NULL;
while(p1->a!='\n')
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct txt *)malloc(LEN);
scanf("%c",&p1->a);
}
p2->next=NULL;
return(head);
}
void print(struct txt *head) //打印
{
struct txt *p;
p=head;
if(head!=NULL)
{
do{
printf("%c",p->a);
p=p->next;
} while(p!=NULL);
}
}
void insert_list (struct txt *head) //插入(默认插入最后一个)
{
struct txt *p=head;
struct txt *p1,*p2;
char b;
int n,i;
p2=(struct txt *)malloc(LEN);
printf("\nthe character to insert: ");
scanf("%c",&p2->a);
while(p!=NULL)
{
p1=p;
p=p->next;
}
p1->next=p2;
p2->next=NULL;
printf("\nnow the list is:\n");
print(head);
}
int get_depth_list(struct txt *head) //链表长度测量
{
int m=0;
struct txt *p;
p=head;
while(p!=NULL)
{
m+=1;
p=p->next;
}
return(m);
}
void search_list (struct txt *head) //查找
{
struct txt *p=head;
int i=0,m;
char ll;
printf("\n\nthe charachter you'd like to find: ");
scanf("%c",&ll); //!!!!!这一步有问题!!!单步调试到此死机!!!运行时自动跳过此处!!
m=get_depth_list(head);
if(head!=NULL)
{
while((p->a!=ll)&&(i <=m))
{i+=1;
p=p->next;
}
}
if(m>=i)
{
printf("\ngot it!\nIt's in the place of No.%d\n",i+1);
}
else
{
printf("\nnot found.\n");
}
}
void delete_list(struct txt *head,char y) //删除
{
struct txt *p=head;
struct txt *p1=head;
int m;
if(head!=NULL)
{
if(head->a==y)
{
head=head->next;
p=p->next;
}
else{p=p->next;}
while(p!=NULL)
{
if(p->a==y)
{
p1->next=p->next;
p=p->next;
}
else
{
p=p->next;p1=p1->next;
}
}
}
printf("\nnow the list is:\n");
print(head);
}
void main()
{
struct txt *head,*p1;
char l,y;
printf("\n\ninput the string: ");
head=creat_list();
printf("\nthe original character-string is:\n");
print(head);
insert_list(head);
// printf("\n\nthe character you'd like to find: ");
// scanf("%c",&l);
// search_list(head,l);
search_list(head);
printf("\n\ninput the character to delete: ");
scanf("%c",&y);
delete_list(head,y);
}
运行状态:可以正常插入(输入字符插入最后一个)、正常删除字符,但是程序运行自动跳过查找字符,单步调试时TC异常退出……
单独把查找字符部分提出来编了一个程序:
# include <stdio.h>
# include <math.h>
# include <malloc.h>
# define NULL 0
# define LEN sizeof(struct txt)
struct txt
{
char a;
struct txt *next;
};
struct txt *creat_list(void)
{
struct txt *head;
struct txt *p1,*p2;
int n=0;
p1=p2=(struct txt *)malloc(LEN);
scanf("%c",&p1->a);
head=NULL;
while(p1->a!='\n')
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct txt *)malloc(LEN);
scanf("%c",&p1->a);
}
p2->next=NULL;
return(head);
}
int get_depth_list(struct txt *head)
{
int m=0;
struct txt *p;
p=head;
while(p!=NULL)
{
m+=1;
p=p->next;
}
return(m);
}
void main ()
{
struct txt *p,*head;
struct txt *p1,*p2;
char ll;
int m,i=0;
head=creat_list();
p=head;
printf("\n\nthe charachter you'd like to find: ");
scanf("%c",&ll);
m=get_depth_list(head);
if(head!=NULL)
{
while((p->a!=ll)&&(i <=m))
{i+=1;
p=p->next;
}
}
if(m>=i)
{
printf("\ngot it!\nIt's in the place of No.%d\n",i+1);
}
else
{
printf("\nnot found.\n");
}
}
运行正常……
菜鸟请问各位大牛,到底是哪出错了?