请教一道C的链表程序!
whyes 2003-09-24 09:13:52 这是一个输入数个整数,建立链表,直到输入的数字为-1时为止,并从表头至表尾依次输入链表的程序:
#include<stdio.h>
#include<stdlib.h>
main(){
typedef struct node{ /*定义节点*/
int data;
struct node *next;
}NODE;
NODE **head,*t;
int q;
while(1)
{
scanf("%d",&q);
if(q!=-1){
NODE *u;
u=(NODE*)(sizeof(NODE));
u->data=q;u->next=NULL;
if(*head==NULL) /*如果链表为空,则以新节点为表头*/
{*head=u;}
else
{for(t=*head;t->next!=NULL;t=t->next); /*否则新节点插到表尾*/
t->=u;}
}
else break; /*接收数字为-1时跳出循环*/
}
for(t=*head;t->next!=NULL;t=t->next)printf("\n%d",t->data); /*依次输出链表中元素*/
}
----------------
输出时,总是进入死循环。
请高手指教!谢谢~!