求解这个简单的链表,为什么会出错,而且运行过后编译器不能调试了求解
#include<stdio.h>
#include<stdlib.h>
struct node
{
int date;
node *next;
};
int main()
{
int n;
scanf("%d",&n);
node *head,*q,*tail;
node p;
q = &p;
int a;
head = NULL;
for(int i = 0;i<n;i++)
{
scanf("%d",&a);
q->date = a;
if(head==NULL)
{
head = q;
}
else
{
tail = q;
}
q = q->next;
q = (node*)malloc(sizeof(node));
}
free(q);
tail->next=NULL;
node *t = head;
while(t->next!=NULL)
{
printf("%d\n",t->date);
t = t->next;
}
return 0;
}