关于建立动态链表的问题
代码如下:#include <stdio.h>
#include <malloc.h>
void First_Open ();
void Certain_Open ();
void Input (struct student *p);
void Output (struct student *p);
void Connection (struct student *pcurrent,struct student *panaddr);
int Judge ();
void Output (struct student *p);
int N=0;
struct student
{
int num;
struct student *next;
}*HEAD,*UNKNOWNADDR;
void main()
{
int n;
printf("是否开始创建动态链表 y/n :");
n=Judge ();
if (!n)
{
printf("\n未能创建动态链表\n");
}
while (n)
{
if (n&&!N)
{
First_Open ();
Input (HEAD);
N++;
}
else if (n&&N)
{
Certain_Open ();
Input (UNKNOWNADDR);
Connection (HEAD,UNKNOWNADDR);
}
printf("是否继续创建? y/n :");
n=Judge ();
if (!n)
{
printf("创建结束\n");
break;
}
}
Output (HEAD);
}
int Judge ()
{
char temp;
scanf("%c",&temp);
if (temp=='y' || temp=='Y')
{
return 1;
}
else
{
return 0;
}
}
void First_Open ()
{
HEAD=(struct student*)malloc(sizeof (struct student));
}
void Certain_Open ()
{
UNKNOWNADDR=(struct student*)malloc(sizeof (struct student));
}
void Input (struct student *p)
{
printf("请输入学号:");
scanf("%d",&p->num);
getchar ();
p->next=NULL;
}
void Connection (struct student *pcurrent,struct student *panaddr)
{
struct student *pnext;
while (pcurrent!=NULL)
{
pnext=pcurrent->next;
if (panaddr->num<pcurrent->num && pcurrent==HEAD)
{
panaddr->next=HEAD;
HEAD=panaddr;
break;
}
else if (panaddr->num>pcurrent->num && panaddr->num<pnext->num)
{
pcurrent->next=panaddr;
panaddr->next=pnext;
break;
}
else if (pcurrent->next == NULL)
{
pcurrent->next=panaddr;
}
pcurrent=pcurrent->next;
}
}
void Output (struct student *p)
{
while (p!=NULL)
{
printf("学号:");
printf("%d ",p->num);
p=p->next;
printf("\n");
}
}
关于插入功能,有点问题,为什么每次当输入完第一个结点后,若继续创建,不能输入大于当前链表内的任意结点。错误提示:access violation (访问冲突)。