该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我想了一个小时也看不出来 ,求大神帮忙 ,指点 。必粉!!!
#include
#include
#define LEN sizeof(struct node)
struct node{
int no;
struct node *next;
};
struct node *create()
{
struct node *head,*p1,*p2;
int n=0;
head=NULL;
p1=p2=(struct node *)malloc(LEN);
scanf("%d",&p1->no);
while(p1->no!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct node *)malloc(LEN);
scanf("%d",&p1->no);
}
p2->next=NULL;
free(p1);
return head;
}
struct node *delete(struct node *head,int x)
{
struct node *p1,*p2;
p1=head;
while(x!=p1->no&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(x==p1->no)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
free(p1);
}
return head;
}
void display(struct node *head)
{
struct node *p;
if(head!=NULL)
{
p=head;
do
{
printf("%d-->",p->no);
p=p->next;
}while(p!=NULL);
}
}
struct node *insert(struct node *head,struct node *x)
{
struct node *p1,*p2,*p0;
p1=head;
p0=x;
if(head==NULL)
{printf("11");
head=p0;
p0->next=NULL;
}
p2=p1;
while(p1->nono&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p2->next!=NULL)
{
if(head==p1)
{
head=p0;
printf("%d",head->no);
p0->next=p1;
}
else
{ printf("11");
p2->next=p0;
p0->next=p1;
}
}
else
{ printf("11");
p1->next=p0;
p0->next=NULL;
}
return head;
}
int main()
{
struct node *head,*x;
x=(struct node *)malloc(LEN);
x->no=5;
head=create();
display(head);
printf("----------------\n");
delete(head,4);
insert(head,x);
display(head);
}