C语言链表尾插法为什么我只能插入后两个
#include<stdio.h>#include<stdlib.h>typedefstructmyStruct{intdate;structmyStruct*pNext...
#include <stdio.h>
#include <stdlib.h>
typedef struct myStruct
{
int date;
struct myStruct *pNext;
}Snake;
Snake *add(Snake *pHead,int date)
{
Snake *pNew = (Snake*)malloc(sizeof(Snake)); //尾插法
Snake *pTile = pHead; //备份一下头节点
pNew->date = date;
pNew->pNext = NULL;
if(pHead == NULL)
{
pHead = pNew;
}
else
{
while(pTile->pNext)
{
pTile = pTile->pNext; //循环前进
}
pTile->pNext = pNew;
}
}
void show(Snake *p) //打印
{
if(p == NULL)
{
return ;
}else
{
printf("%d,%p,%p\n",p->date,p,p->pNext);
show(p->pNext);
}
}
int main()
{
Snake *pHead = NULL;
pHead = add(pHead,11);
pHead = add(pHead,12);
pHead = add(pHead,13);
pHead = add(pHead,14);
pHead = add(pHead,15);
show(pHead);
system("pause");
return 0;
} 展开
#include <stdlib.h>
typedef struct myStruct
{
int date;
struct myStruct *pNext;
}Snake;
Snake *add(Snake *pHead,int date)
{
Snake *pNew = (Snake*)malloc(sizeof(Snake)); //尾插法
Snake *pTile = pHead; //备份一下头节点
pNew->date = date;
pNew->pNext = NULL;
if(pHead == NULL)
{
pHead = pNew;
}
else
{
while(pTile->pNext)
{
pTile = pTile->pNext; //循环前进
}
pTile->pNext = pNew;
}
}
void show(Snake *p) //打印
{
if(p == NULL)
{
return ;
}else
{
printf("%d,%p,%p\n",p->date,p,p->pNext);
show(p->pNext);
}
}
int main()
{
Snake *pHead = NULL;
pHead = add(pHead,11);
pHead = add(pHead,12);
pHead = add(pHead,13);
pHead = add(pHead,14);
pHead = add(pHead,15);
show(pHead);
system("pause");
return 0;
} 展开
展开全部
//函数add()有返回值,在末尾要有return pHead;
#include <stdio.h>
#include <stdlib.h>
typedef struct myStruct
{
int date;
struct myStruct *pNext;
}Snake;
Snake *add(Snake *pHead,int date)
{
Snake *pNew = (Snake*)malloc(sizeof(Snake)); //尾插法
Snake *pTile = pHead; //备份一下头节点
pNew->date = date;
pNew->pNext = NULL;
if(pHead == NULL)
{
pHead = pNew;
}
else
{
while(pTile->pNext)
{
pTile = pTile->pNext; //循环前进
}
pTile->pNext = pNew;
}
return pHead; //增加这个返回语句
}
void show(Snake *p) //打印(递归法)
{
if(p == NULL)
{
return ;
}
else
{
printf("%d,%p,%p\n",p->date,p,p->pNext);
show(p->pNext);
}
}
int main()
{
Snake *pHead = NULL;
pHead = add(pHead,11);
pHead = add(pHead,12);
pHead = add(pHead,13);
pHead = add(pHead,14);
pHead = add(pHead,15);
show(pHead);
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询