
一道C语言链表问题
#include<stdio.h>#include<malloc.h>#defineNULL0#defineLENsizeof(structstudent)structs...
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
int n;//全局变量n
struct student *creat(void)//函数类型为struct student 的指针型
{ struct student *head ,*p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%d %f"&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;//将P2.next指向P1
p2=p1;//p2向前移动
p1=(struct student *)malloc(LEN);
scanf("%d %f",&p1->num,&p2->score);
}
p2->next=NULL;//
return(head);
}
struct student *del(struct student *head, int num)
{
struct student *p1,*p2;
if(head==NULL){printf("\nlist is null!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!==NULL)//p1指向的不是要找的节点而且后面还有节点
{
p2=p1;p1=p1->next;//p1后移了一个节点
}
if(num==p1->num)//找到了
{if(p1==head)head=p1->next;//如果是头结点,把头结点向后东西一个
else p2->next=p1->next;
printf("delect: %d",num);
n=n-1;//减少了一个节点
}
}
struct student * insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;//P1指向第一个节点
p0=stud;//p0指向要插入的节点
if(head==NULL)//原来的链表是空表
{ head=p0;p0->next=NULL;//使p0成为头结点}
else
{while((p0->num>p1->num)&&(p1->num!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{ if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}//插到节点的最后
}
n=n+1; //节点数加一
return(head);
}
void print(struct student *head)
{
struct student *p;
printf("\n these%d records are\n",n);
p=head;
if(head!=NULL)
do
{printf("%d %f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
void main()
{ struct student *head,stu;
int del_num;
printf("input records :\n");
head=creat();
print(head);
printf("\n input the delected number:");
scanf("%d",&del_num);
head=del(head,del_num);
print(head);
printf("\ninput the inserted record:");
scanf("%d,%f",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
}
大神们 哪里错了啊 ? 展开
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
int n;//全局变量n
struct student *creat(void)//函数类型为struct student 的指针型
{ struct student *head ,*p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%d %f"&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;//将P2.next指向P1
p2=p1;//p2向前移动
p1=(struct student *)malloc(LEN);
scanf("%d %f",&p1->num,&p2->score);
}
p2->next=NULL;//
return(head);
}
struct student *del(struct student *head, int num)
{
struct student *p1,*p2;
if(head==NULL){printf("\nlist is null!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!==NULL)//p1指向的不是要找的节点而且后面还有节点
{
p2=p1;p1=p1->next;//p1后移了一个节点
}
if(num==p1->num)//找到了
{if(p1==head)head=p1->next;//如果是头结点,把头结点向后东西一个
else p2->next=p1->next;
printf("delect: %d",num);
n=n-1;//减少了一个节点
}
}
struct student * insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;//P1指向第一个节点
p0=stud;//p0指向要插入的节点
if(head==NULL)//原来的链表是空表
{ head=p0;p0->next=NULL;//使p0成为头结点}
else
{while((p0->num>p1->num)&&(p1->num!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{ if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}//插到节点的最后
}
n=n+1; //节点数加一
return(head);
}
void print(struct student *head)
{
struct student *p;
printf("\n these%d records are\n",n);
p=head;
if(head!=NULL)
do
{printf("%d %f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
void main()
{ struct student *head,stu;
int del_num;
printf("input records :\n");
head=creat();
print(head);
printf("\n input the delected number:");
scanf("%d",&del_num);
head=del(head,del_num);
print(head);
printf("\ninput the inserted record:");
scanf("%d,%f",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
}
大神们 哪里错了啊 ? 展开
展开全部
1、create函数里面scanf("%d %f,&p1->num,&p1->score);少写了个"
2、del函数里面while(num!=p1->num&&p1->next!==NULL),!==改成!=
3、del函数里面goto end;改成return NULL;
4、insert函数里面{ head=p0;p0->next=NULL;//使p0成为头结点}
注释写在了{}里面
2、del函数里面while(num!=p1->num&&p1->next!==NULL),!==改成!=
3、del函数里面goto end;改成return NULL;
4、insert函数里面{ head=p0;p0->next=NULL;//使p0成为头结点}
注释写在了{}里面
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询