求大神帮我看看这几个C语言程序错在哪里啊?有逻辑错误,找不出来.谢!是有关单链表的程序 5
#include<stdio.h>#include<malloc.h>typedefcharelemtype;structnode{chardata;structnode...
#include<stdio.h>
#include<malloc.h>
typedef char elemtype ;
struct node
{
char data;
struct node *next;
};
struct node * create()
{
struct node *head,*p,*s;
elemtype x;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
printf("\nplease put in a string and quit with ENTER:");
scanf ("%c",&x);
while(x!='\n')
{
s=(struct node *)malloc(sizeof(struct node));
s->data=x;
if(head->next==NULL)
head->next=s;
else
p->next=s;
p=s;
scanf("%c",&x);
}
p->next=NULL;
return(head);
}
struct node *insert(struct node *head,int i)
{
struct node *p,*t;
char x;
int j;
p=head;
j=0;
while (p->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if(j!=i-1)
{
printf("i is invalid!\n");
exit(0);
}
t=(struct node*)malloc(sizeof(struct node));
scanf("%c",&x);
t->data=x;
t->next=p->next;
p->next=t;
return (head);
}
void access(struct node *head)
{
struct node *p;
p=head;
while(p->next!=NULL);
{
p=p->next;
printf("\n %c\t",p->data);
}
}
int main()
{
struct node *head,*list;
int i;
head=create();
printf("单链表为:");
access(head);
printf("请输入要插入的位置以及DATA的值:");
scanf("%d",&i);
insert(head,i);
return 0;
} 展开
#include<malloc.h>
typedef char elemtype ;
struct node
{
char data;
struct node *next;
};
struct node * create()
{
struct node *head,*p,*s;
elemtype x;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
printf("\nplease put in a string and quit with ENTER:");
scanf ("%c",&x);
while(x!='\n')
{
s=(struct node *)malloc(sizeof(struct node));
s->data=x;
if(head->next==NULL)
head->next=s;
else
p->next=s;
p=s;
scanf("%c",&x);
}
p->next=NULL;
return(head);
}
struct node *insert(struct node *head,int i)
{
struct node *p,*t;
char x;
int j;
p=head;
j=0;
while (p->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if(j!=i-1)
{
printf("i is invalid!\n");
exit(0);
}
t=(struct node*)malloc(sizeof(struct node));
scanf("%c",&x);
t->data=x;
t->next=p->next;
p->next=t;
return (head);
}
void access(struct node *head)
{
struct node *p;
p=head;
while(p->next!=NULL);
{
p=p->next;
printf("\n %c\t",p->data);
}
}
int main()
{
struct node *head,*list;
int i;
head=create();
printf("单链表为:");
access(head);
printf("请输入要插入的位置以及DATA的值:");
scanf("%d",&i);
insert(head,i);
return 0;
} 展开
2个回答
展开全部
==我调试中... ,好了,所有问题都已用注释标出:
#include<stdio.h>
#include <stdlib.h>
#include<malloc.h>
typedef char elemtype ;
struct node
{
char data;
struct node *next;
};
struct node * create()
{
struct node *head,*p,*s;
elemtype x;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
printf("\nplease put in a string and quit with ENTER:");
scanf ("%c",&x);
while(x!='\n')
{
s=(struct node *)malloc(sizeof(struct node));
s->data=x;
s->next = NULL; //用这句就既可以避免下面我注释的那个bug,也可以让尾结点的next为NULL值
if(head->next==NULL)
head->next=s;
else
p->next=s;
p=s;
scanf("%c",&x);
}
//p->next=NULL; //这里存在一个隐藏bug,即如果调用create后就直接输入回车的话,那么循环体是没执行过的,所以这时候的p是个野指针,对它访问next元素会使程序崩溃
return(head);
}
struct node *insert(struct node *head,int i)
{
struct node *p,*t;
char x;
int j;
p=head;
j=0;
while (p->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if(j!=i-1)
{
printf("i is invalid!\n");
exit(0);
}
t=(struct node*)malloc(sizeof(struct node));
printf("请输入要插入的DATA的值: "); //这里再提示用户输入值较合理
scanf("%c%*c",&x);
t->data=x;
t->next=p->next;
p->next=t;
return (head);
}
void access(struct node *head)
{
struct node *p;
p=head;
while(p->next!=NULL) //多了个分号
{
p=p->next;
printf("\n %c\t",p->data);
}
}
int main()
{
struct node *head;
int i;
head=create();
printf("单链表为:");
access(head);
printf("\n请输入要插入的位置:"); //应只提示用户输入插入位置
scanf("%d%*c",&i); //%*c用来接收scanf产生的回车符,不接收的话会影响下一次scanf的输入
insert(head,i);
access(head); //应该是head
return 0;
}
#include<stdio.h>
#include <stdlib.h>
#include<malloc.h>
typedef char elemtype ;
struct node
{
char data;
struct node *next;
};
struct node * create()
{
struct node *head,*p,*s;
elemtype x;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
printf("\nplease put in a string and quit with ENTER:");
scanf ("%c",&x);
while(x!='\n')
{
s=(struct node *)malloc(sizeof(struct node));
s->data=x;
s->next = NULL; //用这句就既可以避免下面我注释的那个bug,也可以让尾结点的next为NULL值
if(head->next==NULL)
head->next=s;
else
p->next=s;
p=s;
scanf("%c",&x);
}
//p->next=NULL; //这里存在一个隐藏bug,即如果调用create后就直接输入回车的话,那么循环体是没执行过的,所以这时候的p是个野指针,对它访问next元素会使程序崩溃
return(head);
}
struct node *insert(struct node *head,int i)
{
struct node *p,*t;
char x;
int j;
p=head;
j=0;
while (p->next!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if(j!=i-1)
{
printf("i is invalid!\n");
exit(0);
}
t=(struct node*)malloc(sizeof(struct node));
printf("请输入要插入的DATA的值: "); //这里再提示用户输入值较合理
scanf("%c%*c",&x);
t->data=x;
t->next=p->next;
p->next=t;
return (head);
}
void access(struct node *head)
{
struct node *p;
p=head;
while(p->next!=NULL) //多了个分号
{
p=p->next;
printf("\n %c\t",p->data);
}
}
int main()
{
struct node *head;
int i;
head=create();
printf("单链表为:");
access(head);
printf("\n请输入要插入的位置:"); //应只提示用户输入插入位置
scanf("%d%*c",&i); //%*c用来接收scanf产生的回车符,不接收的话会影响下一次scanf的输入
insert(head,i);
access(head); //应该是head
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你问题是没有输出吗? 大哥 你的 while(p->next!=NULL); 这一句 有没有发现多了一个分号??
更多追问追答
追问
int main()
{
struct node *head,*list;
int i;
head=create();
printf("单链表为:");
access(head);
printf("请输入要插入的位置以及DATA的值:");
scanf("%d",&i);
insert(head,i);
access(list);
return 0;
}
主函数是这样的。我想说是输入要插入的位置和DATA 值后不会执行access函数了
追答
printf("请输入要插入的位置以及DATA的值:");
scanf("%d",&i);
insert(head,i);
access(list);
这一句 access(list); 要改成 access(head),
然后你在 insert里面好像也有问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询