为什么加入的插入节点函数没有反应(insert_list)萌新求助
# include <stdio.h># include <malloc.h>
# include <stdlib.h>
typedef struct Node
{
int date;
struct Node * pNext;
}NODE,* PNODE;
void traverse_list(PNODE phead);//输出创建链表
bool insert_list(PNODE phead,int pos,int val);
PNODE create_list(void);//创建链表
int main(void)
{
PNODE phead = NULL;
phead = create_list();
traverse_list(phead);
insert_list(PNODE phead,2,3);
traverse_list(phead);
return 0 ;
}
PNODE create_list()
{
int len;
int val;
int i;
PNODE phead = (PNODE)malloc(sizeof(NODE));
if(NULL == phead)
{
printf("shibai\n");
exit(-1);
}
PNODE P1 = phead;
P1->pNext = NULL;
printf("请输入您需要生成的链表节点的个数: len = ");
scanf("%d", &len);
for(i = 0;i < len;++i)
{
printf("请输入第%d个节点的值",i+1);
scanf("%d",&val);
PNODE P2 = (PNODE)malloc(sizeof(NODE));
if(NULL == phead)
{
printf("shibai\n");
exit(-1);
}
P2->date = val;
P1->pNext= P2;
P2->pNext = NULL;
P1 = P2;
}
return phead;
}
void traverse_list(PNODE phead)
{
PNODE p = phead->pNext;
while (NULL != p)
{
printf("%d ", p->date);
p = p->pNext;
}
printf("\n");
return;
}
bool insert_list(PNODE phead,int pos,int val)
{
PNODE p = phead;
int i = 0;
while(i< pos-1 && NULL != p)
{
p = p->pNext;
i++;
}
if(i > pos-1 || p == NULL)
return false;
PNODE pnew =(PNODE)malloc(sizeof(NODE));
if(NULL == pnew)
{
printf("失败!\n");
exit(-1);
}
pnew->date = val;
PNODE q = p->pNext;
p->pNext = pnew;
pnew->pNext = q;
return true;
}