#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
#define NULL 0
struct student
{ int num;
char x;
struct student *next;
};
struct student *creat(void) //建立链表
{ struct student *head,*p1,*p2;
int n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
scanf("%d %c",&p1->num,&p1->x);
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d %c",&p1->num,&p1->x);
}
p2->next=NULL;
return(head);
}
void print(struct student *head) //输出链表
{ struct student *p;
p=head;
if(head!=NULL)
do{
printf("%d,%c",p->num,p->x);
p=p->next;
}while(p!=NULL);
}
struct student *insert(struct student *head,struct student *p) //插入结点
{
struct student *p1,*p2,*p0;
p1=head;
p0=p;
if(head==NULL)
{ head=p0;
head->next=NULL;
}
else
{ while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;}
if(p0->num<=p1->num)
{if(p1==head)
{
p0->next=head;
head=p0;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{ p1->next=p0;
p0->next=NULL;
}
}
return(head);
}
struct student * del(struct student *head ,int i) //删除num值为i的结点
{
struct student *p1,*p2;
p1=head;
if(head==NULL)
{ printf("\nlist null\n");
goto end ;
}
else
{
while(p1->num!=i&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==i)
{
if(p1==head)
head=head->next;
else
{
p2->next=p1->next;
printf("delete:%d",i);
}
}
else
printf("\n%dno fond i in the link\n");
}
end: return(head);
}
void main()
{
struct student *head,*stu;
int del_num;
printf("input records:\n");
head=creat();
print(head);
printf("input the delete num:\n");
scanf("%d",&del_num);
head=del(head,del_num);
print(head);
printf("\ninput the inserted record:\n");
stu=(struct student *)malloc(LEN);
scanf("%d,%c",&stu->num,&stu->x);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student * )malloc(LEN);
scanf("%d,%c",&stu->num,&stu->x);
}
}
看看哪里错了
#include<malloc.h>
#define LEN sizeof(struct student)
#define NULL 0
struct student
{ int num;
char x;
struct student *next;
};
struct student *creat(void) //建立链表
{ struct student *head,*p1,*p2;
int n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
scanf("%d %c",&p1->num,&p1->x);
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d %c",&p1->num,&p1->x);
}
p2->next=NULL;
return(head);
}
void print(struct student *head) //输出链表
{ struct student *p;
p=head;
if(head!=NULL)
do{
printf("%d,%c",p->num,p->x);
p=p->next;
}while(p!=NULL);
}
struct student *insert(struct student *head,struct student *p) //插入结点
{
struct student *p1,*p2,*p0;
p1=head;
p0=p;
if(head==NULL)
{ head=p0;
head->next=NULL;
}
else
{ while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;}
if(p0->num<=p1->num)
{if(p1==head)
{
p0->next=head;
head=p0;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{ p1->next=p0;
p0->next=NULL;
}
}
return(head);
}
struct student * del(struct student *head ,int i) //删除num值为i的结点
{
struct student *p1,*p2;
p1=head;
if(head==NULL)
{ printf("\nlist null\n");
goto end ;
}
else
{
while(p1->num!=i&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==i)
{
if(p1==head)
head=head->next;
else
{
p2->next=p1->next;
printf("delete:%d",i);
}
}
else
printf("\n%dno fond i in the link\n");
}
end: return(head);
}
void main()
{
struct student *head,*stu;
int del_num;
printf("input records:\n");
head=creat();
print(head);
printf("input the delete num:\n");
scanf("%d",&del_num);
head=del(head,del_num);
print(head);
printf("\ninput the inserted record:\n");
stu=(struct student *)malloc(LEN);
scanf("%d,%c",&stu->num,&stu->x);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student * )malloc(LEN);
scanf("%d,%c",&stu->num,&stu->x);
}
}
看看哪里错了