小弟费了很大的劲才把老师的实验作业编完,谁知一运行真是让人大失所望,一直显示“NULL pointer assginment",检查了一下,没发现有什么错误啊!程序如下,望高手指点迷津,谢谢!
#include "stdio.h"
#include "stdlib.h"
typedef struct node
{int data;
struct node *next;
}linklist;
linklist *head,*new1,*q;
void Init_linklist()
{
char ch;
head=(linklist *)malloc(sizeof(linklist));
q=head;
printf("please input a char:");
ch=getchar();
while(ch!='$')
{
new1=(linklist *)malloc(sizeof(linklist));
new1->data=ch;
q->next=new1;
q=new1;
ch=getchar();
}
q->next=0;
}
void Insertafter(p,x)
linklist *p;
char x;
{ new1=(linklist *)malloc(sizeof(linklist));
new1->data=x;
new1->next=p->next;
p->next=new1;
}
void Delete(p)
linklist *p;
{ new1=p->next;
p->next=new1->next;
free(new1);
}
main()
{ linklist *p;
int i,j,t=0;
char ch;
printf("1:Init_linklist()*****\n");
printf("2:Insertafter() *****\n");
printf("3:Delete() *****\n");
printf("please input 1||2||3 to select the action:");
scanf("%d",&i);
if(i==1)
Init_linklist();
else if(i==2||i==3)
{printf("please input the jidian you want to do or the char you want to insert:");
if(i==2)
{ scanf("%d%c",&j,&ch);
q=head->next;
do {t++; q=q->next;p=q;}
while (q->next!=0&&t!=j);
Insertafter(p,ch);
}
if(i==3)
{scanf("%d",&j);
q=head->next;
do {t++;q=q->next;p=q;}
while (q->next!=0);
}
}
}