不知为什么链表中不能插入数据,帮忙看一下
#include<stdio.h>#include<stdlib.h>
struct nodelink
{
int data;
struct nodelink* next;
};
typedef struct nodelink NodeLink;
typedef NodeLink* NodeLinkPtr;
void insertdata(NodeLinkPtr*,int);
void deletdata(NodeLinkPtr*,int);
void printdata(NodeLinkPtr);
int main(void)
{
NodeLinkPtr startPtr=NULL;
int select=0,value=0;
printf("\n请输入需要的功能:\n"
"\n1.插入数据\n"
"\n2.删除数据\n"
"\n3.打印链表\n");
printf("\n? ");
while(scanf("%d",&select))
{
switch(select)
{
case 1:printf("\n请输入要插入的数据: \n");
lab1:if(scanf("%d",&value))
{
insertdata(&startPtr,value);
break;
}
else
{
printf("\n输入错误\n");
fflush(stdin);
goto lab1;
}
case 2:printf("\n请输入要删除的数据\n");
lab2:if(scanf("%d",&value))
{
deletdata(&startPtr,value);
break;
}
else
{
printf("\n输入错误\n");
fflush(stdin);
goto lab2;
}
case 3:printf("\n打印链表数据: \n");
printdata(startPtr);
break;
default:printf("\n输入错误,请重新输入:\n");
}
printf("\n? \n");
}
printf("\n程序结束\n");
fflush(stdin);
getchar();
return 0;
}
void printdata(NodeLinkPtr startPtr)
{
if(startPtr!=NULL)
{
while(startPtr!=NULL)
{
printf("%d",startPtr->data);
startPtr=startPtr->next;
}
}
else
printf("\n链表为空\n");
}
void insertdata(NodeLinkPtr* startPtr,int value)
{
NodeLinkPtr newPtr,previousPtr=NULL,currentPtr=*startPtr;
newPtr=(struct nodelink*)malloc(sizeof(NodeLink));
if(newPtr!=NULL)
{
newPtr->data=value;
//newPtr->next=NULL;
while(value>(currentPtr->data)&¤tPtr!=NULL)
{
previousPtr=currentPtr;
currentPtr=currentPtr->next;
}
if(previousPtr!=NULL)
{
previousPtr->next=newPtr;
newPtr->next=currentPtr;
}
else
{
newPtr->next=*startPtr;
*startPtr=newPtr;
}
}
else
printf("\n没有足够的内存\n");
}
void deletdata(NodeLinkPtr* startPtr,int value)
{
NodeLinkPtr currentPtr=*startPtr,previousPtr=NULL;
if(currentPtr!=NULL)
{
while(value!=currentPtr->data)
{
previousPtr=currentPtr;
currentPtr=currentPtr->next;
}
if(previousPtr!=NULL)
{
previousPtr->next=currentPtr->next;
free(currentPtr);
}
else
{
*startPtr=currentPtr->next;
free(currentPtr);
}
}
else
printf("\n链表为空\n");
}
插入数据函数insertdata(),一插入数据就提示内存不能为read,找了好久也没找到错