#define NULL 0
#define TYPE struct stu
#define LEN sizeof(struct stu)
struct stu
{
int num;
int age;
struct stu *next;
};
TYPE * creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE*)malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else
pf->next=pb;
pb->next=NULL;
pf=pb;
}
return(head);
}
TYPE * search(TYPE *head,int n)
{
TYPE *p;
int i;
p=head;
while(p->num!=n && p->next!=NULL)
p=p->next;
if(p->num==n) return(p);
if(p->num!=n && p->next==NULL)
printf("Node %d has not been found!\n",n);
}
TYPE * delete(TYPE * head,int num)
{
TYPE *pf,*pb;
if(head==NULL)
{
printf("\nempty list!\n");
goto end;
}
pb=head;
while(pb->num!=num && pb->next!=NULL)
{
pf=pb;pb=pb->next;
}
if(pb->num==num)
{
if(pb==head)
head=pb->next;
else pf->next=pb->next;
printf("The node is deleted\n");
free(pb);
} else
printf("The node not been found!\n");
end:
return head;
}
// pb->next=pi;
// pi->next=NULL;
TYPE * insert(TYPE * head,TYPE *pi)
{
TYPE *pf,*pb;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;
}else
{
while((pi->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
if(pi->num<=pb->num)
{
if(head==pb) head=pi;
else pf->next=pi;
pi->next=pb;
} else
{
pb->next=pi;
pi->next=NULL;
}
}
return head;
}
void print(TYPE * head)
{
printf("Number\t\tAge\n");
while(head!=NULL)
{
printf("%d\t\t%d\n",head->num,head->age);
head=head->next;
}
}
main()
{
TYPE * head,*pnum;
int n,num;
printf("input number of node:");
scanf("%d",&n);
head=creat(n);
print(head);
printf("Input the deleted number:");
scanf("%d",&num);
head=delete(head,num);
print(head);
printf("Input the inserted number and age:");
pnum=(TYPE *)malloc(LEN);
scanf("%d%d",&pnum->num,&pnum->age);
head=insert(head,pnum);
print(head);
}
#define TYPE struct stu
#define LEN sizeof(struct stu)
struct stu
{
int num;
int age;
struct stu *next;
};
TYPE * creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE*)malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else
pf->next=pb;
pb->next=NULL;
pf=pb;
}
return(head);
}
TYPE * search(TYPE *head,int n)
{
TYPE *p;
int i;
p=head;
while(p->num!=n && p->next!=NULL)
p=p->next;
if(p->num==n) return(p);
if(p->num!=n && p->next==NULL)
printf("Node %d has not been found!\n",n);
}
TYPE * delete(TYPE * head,int num)
{
TYPE *pf,*pb;
if(head==NULL)
{
printf("\nempty list!\n");
goto end;
}
pb=head;
while(pb->num!=num && pb->next!=NULL)
{
pf=pb;pb=pb->next;
}
if(pb->num==num)
{
if(pb==head)
head=pb->next;
else pf->next=pb->next;
printf("The node is deleted\n");
free(pb);
} else
printf("The node not been found!\n");
end:
return head;
}
// pb->next=pi;
// pi->next=NULL;
TYPE * insert(TYPE * head,TYPE *pi)
{
TYPE *pf,*pb;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;
}else
{
while((pi->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
if(pi->num<=pb->num)
{
if(head==pb) head=pi;
else pf->next=pi;
pi->next=pb;
} else
{
pb->next=pi;
pi->next=NULL;
}
}
return head;
}
void print(TYPE * head)
{
printf("Number\t\tAge\n");
while(head!=NULL)
{
printf("%d\t\t%d\n",head->num,head->age);
head=head->next;
}
}
main()
{
TYPE * head,*pnum;
int n,num;
printf("input number of node:");
scanf("%d",&n);
head=creat(n);
print(head);
printf("Input the deleted number:");
scanf("%d",&num);
head=delete(head,num);
print(head);
printf("Input the inserted number and age:");
pnum=(TYPE *)malloc(LEN);
scanf("%d%d",&pnum->num,&pnum->age);
head=insert(head,pnum);
print(head);
}
浙公网安备 33010602011771号