#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int num;
struct node *next;
}*p,*q,*head,*temp,*p1,*endpointer,*p2,*p3;
int main()
{
/*create*/
p=q=p1=p2=head=temp=(struct node*)malloc(sizeof(struct node));
printf("input num ");scanf("%d",&p->num);
p->next=NULL;
while(p->num)
{
q=p;
p=(struct node*)malloc(sizeof(struct node));
printf("input num ");scanf("%d",&p->num);
q->next=p;
}
q->next=NULL;
endpointer=q;
printf("结果:");
while(temp)
{
printf("%2d ",temp->num);
temp=temp->next;
}
putchar('\n');
/*insert*/
free(temp);
temp=(struct node*)malloc(sizeof(struct node));
printf("input insert num");
scanf("%d",&temp->num);
while(p1!=NULL && p1->num<temp->num)
{
p=p1;
p1=p1->next;
}
if(head->num>temp->num)/*top*/
{
p2=temp;
temp->next=p1;
}
else if(endpointer->num<temp->num)/*end*/
{
p->next=temp;
temp->next=NULL;
endpointer=temp;
}
else /*medium*/
{
p->next=temp;
temp->next=p1;
}
head=p2;
printf("结果:");
while(p2)
{
printf("%2d ",p2->num);
p2=p2->next;
}
putchar('\n');
/*delete*/
free(temp);free(p1);free(p);
p1=head;
int nun;
printf("input delete num");
scanf("%d",&nun);
while(p1->next!=NULL && p1->num!=nun)
{
p=p1;
p1=p1->next;
}
if(head->num==nun==p1->num)
{
head=p1->next;
}
else if(endpointer->num==nun==p1->num)
{
p->next=NULL;
}
else
{
p->next=p1->next;
}
printf("结果:");
while(head)
{
printf("%2d ",head->num);
head=head->next;
}
putchar('\n');
}
/*delete*/
//free(temp);
//free(p1);
//free(p);
这三行去掉