关于啊哈算法p52页的程序,我想插入一个比链表中的数都大的数,但程序陷入死循环,求纠错!程序代码如下,求各位指点哈!
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
int main()
{
struct node *head,*p,*q,*t;
int i,n,a;
scanf("%d",&n);
head=NULL;
for(i=1;i<=n;i++){
scanf("%d",&a);
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=NULL;
if(head==NULL){
head=p;
q=p;
}
else
q->next=p;
q=p;
}
printf("linklist as bellow:\n");
t=head;
while(t!=NULL){
printf("%d ",t->data);
t=t->next;
}
printf("\ninput number which you want to insert:");
scanf("%d",&a);
t=head;
while(t->next!=NULL){
if(t->next->data > a){
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=t->next;
t->next=p;
break;
}
t=t->next;
}
if(t->next==NULL){
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
t->next=p;
t=p;
}
t=head;
while(t!=NULL){
printf("%d ",t->data);
t=t->next;
}
getchar();getchar();
return 0;
} |