链表删除的问题
#include"stdio.h"#include"malloc.h"#include"string.h"typedefstructstu{intnum;charname...
#include "stdio.h"
#include "malloc.h"
#include "string.h"
typedef struct stu{
int num;
char name[10];
struct stu*next;
}stu_line;
stu_line*stu_input()
{
char ch;
stu_line*head=NULL;
stu_line*tail=NULL;
stu_line*p=NULL;
head=(stu_line*)malloc(sizeof(stu_line));//头节点
if(head==NULL)
{
printf("申请内存失败!");
exit(0);
}
head->next=NULL;
tail=head;
do{
p=(stu_line*)malloc(sizeof(stu_line));
if(p==NULL)
{
printf("申请内存失败!");
exit(0);
}
printf("请输入学生的信息:\n");
printf("请输入学生的学号:");
scanf("%d",&p->num);
printf("请输入学生的姓名:");
scanf("%s",p->name);
p->next=NULL;
tail->next=p;
tail=p;
printf("是否继续输入学生信息?(y/n)");
getchar();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return head;
}
stu_line*stu_inser(stu_line*head,char *per)//根据姓名删除节点
{
stu_line*p=head;
stu_line*pa;
if(head==NULL)
{
printf("链表为空!!!");
}
else{
while(strcmp(p->name,per)!=0&&p->next!=NULL)//判断姓名是否相同
{
pa=p;
p=p->next;//指针后移
}
if(strcmp(p->name,per)==0)//找到条件删除
{
pa->next=p->next;
free(p);//释放内存
}
else
{
printf("未找到该数据!");
}
return head;
}
}
void stu_output(stu_line*head)
{
stu_line*p=head->next;
int i=1;
while(p!=NULL)
{
printf("第%d个学生的信息:\n",i);
printf("%d,%s\n",p->num,p->name);
p=p->next;
i++;
}
}
int main(void)
{
stu_line *s=NULL;
stu_line *f=NULL;
char fallname;
printf("创建链表!\n");
s=stu_input();
printf("请输入要删除的学生的姓名:");
scanf("%s",&fallname);
f=stu_inser(s,fallname);
printf("输出链表:\n");
stu_output(f);
} 这个程序哪里出错了?输入了要删除的数据怎么不输出了啊? 展开
#include "malloc.h"
#include "string.h"
typedef struct stu{
int num;
char name[10];
struct stu*next;
}stu_line;
stu_line*stu_input()
{
char ch;
stu_line*head=NULL;
stu_line*tail=NULL;
stu_line*p=NULL;
head=(stu_line*)malloc(sizeof(stu_line));//头节点
if(head==NULL)
{
printf("申请内存失败!");
exit(0);
}
head->next=NULL;
tail=head;
do{
p=(stu_line*)malloc(sizeof(stu_line));
if(p==NULL)
{
printf("申请内存失败!");
exit(0);
}
printf("请输入学生的信息:\n");
printf("请输入学生的学号:");
scanf("%d",&p->num);
printf("请输入学生的姓名:");
scanf("%s",p->name);
p->next=NULL;
tail->next=p;
tail=p;
printf("是否继续输入学生信息?(y/n)");
getchar();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return head;
}
stu_line*stu_inser(stu_line*head,char *per)//根据姓名删除节点
{
stu_line*p=head;
stu_line*pa;
if(head==NULL)
{
printf("链表为空!!!");
}
else{
while(strcmp(p->name,per)!=0&&p->next!=NULL)//判断姓名是否相同
{
pa=p;
p=p->next;//指针后移
}
if(strcmp(p->name,per)==0)//找到条件删除
{
pa->next=p->next;
free(p);//释放内存
}
else
{
printf("未找到该数据!");
}
return head;
}
}
void stu_output(stu_line*head)
{
stu_line*p=head->next;
int i=1;
while(p!=NULL)
{
printf("第%d个学生的信息:\n",i);
printf("%d,%s\n",p->num,p->name);
p=p->next;
i++;
}
}
int main(void)
{
stu_line *s=NULL;
stu_line *f=NULL;
char fallname;
printf("创建链表!\n");
s=stu_input();
printf("请输入要删除的学生的姓名:");
scanf("%s",&fallname);
f=stu_inser(s,fallname);
printf("输出链表:\n");
stu_output(f);
} 这个程序哪里出错了?输入了要删除的数据怎么不输出了啊? 展开
1个回答
展开全部
/*
问题不大
给你改了下
后面有标志 /////////
你参考参考吧 呵呵
*/
# include "stdio.h"
# include "malloc.h"
# include "string.h"
# include <stdlib.h>////////////////////
typedef struct stu
{
int num;
char name[10];
struct stu * next;
}stu_line;
stu_line * stu_input()
{
char ch;
stu_line * head = NULL;
stu_line * tail = NULL;
stu_line * p = NULL;
head = (stu_line *)malloc(sizeof(stu_line));//头节点
if (head == NULL)
{
printf("申请内存失败!");
exit(-1);///////////////
}
head->next=NULL;
tail=head;
do{
p = (stu_line *)malloc(sizeof(stu_line));
if (p == NULL)
{
printf("申请内存失败!");
exit(-1);///////////
}
printf("请输入学生的信息:\n");
printf("请输入学生的学号:");
scanf("%d",&p->num);
printf("请输入学生的姓名:");
scanf("%s",p->name);
p->next=NULL;
tail->next=p;
tail=p;
printf("是否继续输入学生信息?(y/n)");
getchar();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return head;
}
stu_line * stu_inser(stu_line * head,char * per)//根据姓名删除节点
{
stu_line * p = head;
stu_line * pa;
if (head->next == NULL)//////////////
{
printf("链表为空!!!");
}
else
{
while(strcmp(p->name,per)!=0 && p->next!=NULL)//判断姓名是否相同
{
pa=p;
p=p->next;//指针后移
}
if(strcmp(p->name,per)==0)//找到条件删除
{
pa->next=p->next;
free(p);//释放内存
}
else
{
printf("未找到该数据!");
}
}
return head;
}
void stu_output(stu_line*head)
{
stu_line*p=head->next;
int i=1;
while(p!=NULL)
{
printf("第%d个学生的信息:\n",i);
printf("%d,%s\n",p->num,p->name);
p=p->next;
i++;
}
}
int main(void)
{
stu_line * s=NULL;
stu_line * f=NULL;
char fallname;
printf("创建链表!\n");
s = stu_input();
printf("请输入要删除的学生的姓名:");
scanf("%s",&fallname);
f=stu_inser(s,&fallname);////////////
printf("输出链表:\n");
stu_output(f);
}
问题不大
给你改了下
后面有标志 /////////
你参考参考吧 呵呵
*/
# include "stdio.h"
# include "malloc.h"
# include "string.h"
# include <stdlib.h>////////////////////
typedef struct stu
{
int num;
char name[10];
struct stu * next;
}stu_line;
stu_line * stu_input()
{
char ch;
stu_line * head = NULL;
stu_line * tail = NULL;
stu_line * p = NULL;
head = (stu_line *)malloc(sizeof(stu_line));//头节点
if (head == NULL)
{
printf("申请内存失败!");
exit(-1);///////////////
}
head->next=NULL;
tail=head;
do{
p = (stu_line *)malloc(sizeof(stu_line));
if (p == NULL)
{
printf("申请内存失败!");
exit(-1);///////////
}
printf("请输入学生的信息:\n");
printf("请输入学生的学号:");
scanf("%d",&p->num);
printf("请输入学生的姓名:");
scanf("%s",p->name);
p->next=NULL;
tail->next=p;
tail=p;
printf("是否继续输入学生信息?(y/n)");
getchar();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return head;
}
stu_line * stu_inser(stu_line * head,char * per)//根据姓名删除节点
{
stu_line * p = head;
stu_line * pa;
if (head->next == NULL)//////////////
{
printf("链表为空!!!");
}
else
{
while(strcmp(p->name,per)!=0 && p->next!=NULL)//判断姓名是否相同
{
pa=p;
p=p->next;//指针后移
}
if(strcmp(p->name,per)==0)//找到条件删除
{
pa->next=p->next;
free(p);//释放内存
}
else
{
printf("未找到该数据!");
}
}
return head;
}
void stu_output(stu_line*head)
{
stu_line*p=head->next;
int i=1;
while(p!=NULL)
{
printf("第%d个学生的信息:\n",i);
printf("%d,%s\n",p->num,p->name);
p=p->next;
i++;
}
}
int main(void)
{
stu_line * s=NULL;
stu_line * f=NULL;
char fallname;
printf("创建链表!\n");
s = stu_input();
printf("请输入要删除的学生的姓名:");
scanf("%s",&fallname);
f=stu_inser(s,&fallname);////////////
printf("输出链表:\n");
stu_output(f);
}
追问
f=stu_liser(s,&fallname)这里为什么传地址呢?直接传值不行吗?
追答
stu_line * stu_inser(stu_line * head,char * per)
的第二个参数是char * 类型 而fallname是char类型
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询