数据结构C语言版谁能帮我解释一下这段动态链表最后把注释补全

#include<stdio.h>#include<conio.h>#include<stdlib.h>structnode{intdata;structnode*nex... #include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*pnode; struct node * create ( void ) //初始化
{
struct node *p_create;
p_create = (struct node *) malloc (sizeof (struct node));
if (p_create == NULL)
{
printf ("动态分配内存失败!");
exit(1);
}
p_create -> next = NULL;
return p_create;
} void insert ( struct node * p ) //插入
{
struct node *newp,*plist;
int i,j,n;
plist = p;
printf ("输入节点数:");
scanf ("%d",&i);
for ( n=0; n<i; n++ )
{
printf ("输入数据:");
scanf ("%d",&j);
newp = (struct node *) malloc (sizeof (struct node));
if ( newp == NULL )
{
printf ("动态分配内存失败!");
exit(1);
}
newp->data = j;
newp->next = NULL;
plist->next = newp;
plist = plist->next;
}
return;
}
void show ( struct node *p ) //查看
{p = p->next;
while ( p!=NULL )
{
printf ("%d,",p->data);
p = p->next;
}
return;
} int main()
{
pnode = create();
insert(pnode);
show(pnode); getch();
return 0;
}
展开
 我来答
百度网友036f89e
2012-08-08 · TA获得超过137个赞
知道答主
回答量:102
采纳率:0%
帮助的人:104万
展开全部
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*pnode; //这里定义链表节点的结构体
struct node * create ( void ) //初始化链表,创建链表头结点
{
struct node *p_create;//定义节点变量
p_create = (struct node *) malloc (sizeof (struct node)); //给定义的节点变量申请空间
if (p_create == NULL) //为NULL 则表示申请失败
{
printf ("动态分配内存失败!");
exit(1);//申请失败退出
}
p_create -> next = NULL;//申请成功,将next置NULL
return p_create;//返回创建的头结点
}
void insert ( struct node * p ) //往链表中插入数据
{
struct node *newp,*plist;
int i,j,n;
plist = p;//plist指向传入的参数,也就是调用的时候传入的头结点,作为临时的头结点
printf ("输入节点数:");
scanf ("%d",&i);
for ( n=0; n<i; n++ )
{
printf ("输入数据:");
scanf ("%d",&j);
newp = (struct node *) malloc (sizeof (struct node));//为要插入的节点分配空间
if ( newp == NULL )//分配失败
{
printf ("动态分配内存失败!");
exit(1);//失败退出函数
}
newp->data = j;//申请成功,给节点的参数赋值
newp->next = NULL;//新节点的next置NULL
plist->next = newp;//将新的节点插在原链表的尾部
plist = plist->next;//插入后,将临时头结点后移,保证始终指向最后一个节点,使得每次插入的时候新节点都插入在最后
}
return;
}
void show ( struct node *p ) //查看链表中的节点值
{p = p->next;
while ( p!=NULL )//但p为NULL的时候表明已经到了链表的末尾,所以要在p不为NULL的时候进行循环输出节点值的操作
{
printf ("%d,",p->data);//输出节点值
p = p->next;//当前节点的值输出后,指针向后移到下一个节点上
}
return;
}
int main()//主函数
{
pnode = create();//调用创建头节点的函数,返回头结点
insert(pnode);//调用插入节点函数,进行链表插入节点操作
show(pnode); //调用显示链表节点函数,打印出当前链表中的所有节点
getch();//获取一个字符 目的是让输出界面停留,指导输入回车或其他字符才退出显示窗口
return 0;//main函数正常退出
}
_Mr_Computer_
2012-08-13 · TA获得超过1042个赞
知道小有建树答主
回答量:464
采纳率:100%
帮助的人:281万
展开全部
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>//malloc 函数所在地库文件
struct node
{
int data;
struct node *next;
}*pnode;
//////////////////////////////////////////////////////////////////////////////定义单链表的结构体结构
struct node * create ( void ) //初始化构建表头指针
{
struct node *p_create;
p_create = (struct node *) malloc (sizeof (struct node));//申请一块内存(即一个节点的空间)
if (p_create == NULL)//申请内存之后,地址值就不再是NULL而是表头结点的地址
{
printf ("动态分配内存失败!");
exit(1);
}
p_create -> next = NULL;//为单链表结构的一个节点中的指针域赋值;NULL
return p_create;//返回一个指针域为NULL的表头节点
}
/////////////////////////////////////////////////////////////////////////////////////初始化单链表(创建表头节点)函数
void insert ( struct node * p ) //插入节点函数的编写;新增加的节点
{
struct node *newp,*plist;
int i,j,n;
plist = p;//将表头结点的地址付给plist指针;
printf ("输入节点数:");
scanf ("%d",&i);
for ( n=0; n<i; n++ )//实现找到单链表的要插入节点的位置
{
printf ("输入数据:");
scanf ("%d",&j);
newp = (struct node *) malloc (sizeof (struct node));//申请增加的节点的空间
if ( newp == NULL )
{
printf ("动态分配内存失败!");
exit(1);
}
newp->data = j;//为新增加的节点的数据域赋值
newp->next = NULL;//为新加的节点的指针域赋值
plist->next = newp;
plist = plist->next;
}
return;
}
/////////////////////////////////////////////////////////////////////////////////插入节点函数
void show ( struct node *p ) //查看现在的单链表中所有节点中的值
{p = p->nextwhile ( p!=NULL )//判断是不是终端节点
{
printf ("%d,",p->data);//输入新增节点的数据域
p = p->next;
}
return;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// 输出函数
int main()
{
pnode = create();//单链表初始化==创建表头节点
insert(pnode);//为新建的只有表头结点的单链表一一插入第一节点,第二节点.....
show(pnode); getch();//输出所见单链表的全部值
return 0;
}//////////////////////////////////////////////////////////////////////////////////////////////////////////////主函数

//===========================================================Mr_Computer
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式