主题:[讨论]求助~!在Y前面插入X~!!但显示有误~~!望指点~~!
#include<stdio.h>
struct node{
int date;
struct node *next;
};
node *creat()
{
node *h,*p,*s;
int x ;
h=new node;
// scanf("%d",&x);
h->date=NULL;
h->next=NULL;
p=h;
printf("In put the number of arary:\n");
while(1)
{
scanf("%d",&x);
if(x==0) break;
// if(getchar()=="\n") break;
s=new node;
s->date=x;
s->next=NULL;
p->next=s;
p=s;
}
return(h);
}
void insert(struct node *h,int x,int y )
{
node *s,*p,*q;
int a=0,m;
p=h;
s=h;
while(p->next!=NULL)
{
if(p->date==y)
m=a;
else
p=p->next,a++;
}
for(int i=1;i<a;i++)
{s=s->next;}
q=new node;
q->date=x;
q->next=p;
s->next=q;
}
void dispay(struct node *h)
{
node *p;
p=h;
while(p)
{
printf("%5d",p->date);
p=p->next;
}
printf("\n");
}
void main()
{
int x,y;
struct node *head;
head=creat();
dispay(head);
printf("在y前面插入x:\n");
printf("in put y=");
scanf("%d",&y);
printf("in put x=");
scanf("%d",&x);
insert(head,x,y);
dispay(head);
}
可能是插入函数有误~~!
还有~!!添加时怎么可以在输入回车就结束循环????
if(getchar()=="\n") break;不可以吗??
[em18][em18][em18][em18]
struct node{
int date;
struct node *next;
};
node *creat()
{
node *h,*p,*s;
int x ;
h=new node;
// scanf("%d",&x);
h->date=NULL;
h->next=NULL;
p=h;
printf("In put the number of arary:\n");
while(1)
{
scanf("%d",&x);
if(x==0) break;
// if(getchar()=="\n") break;
s=new node;
s->date=x;
s->next=NULL;
p->next=s;
p=s;
}
return(h);
}
void insert(struct node *h,int x,int y )
{
node *s,*p,*q;
int a=0,m;
p=h;
s=h;
while(p->next!=NULL)
{
if(p->date==y)
m=a;
else
p=p->next,a++;
}
for(int i=1;i<a;i++)
{s=s->next;}
q=new node;
q->date=x;
q->next=p;
s->next=q;
}
void dispay(struct node *h)
{
node *p;
p=h;
while(p)
{
printf("%5d",p->date);
p=p->next;
}
printf("\n");
}
void main()
{
int x,y;
struct node *head;
head=creat();
dispay(head);
printf("在y前面插入x:\n");
printf("in put y=");
scanf("%d",&y);
printf("in put x=");
scanf("%d",&x);
insert(head,x,y);
dispay(head);
}
可能是插入函数有误~~!
还有~!!添加时怎么可以在输入回车就结束循环????
if(getchar()=="\n") break;不可以吗??
[em18][em18][em18][em18]