// 单链表.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include"malloc.h"
//1.定义节点的数据结构
typedef struct node
{
int data;
struct node *next;
}Node,*pNode;
//2.动态创建节点
pNode createlist()
{
int i,len;
pNode head=(pNode)malloc(sizeof(Node));
pNode end=head;//尾结点=头结点
end->next=NULL;
printf("\n请输入结点个数:");
scanf("%d",&len);
for(i=1;i<=len;i++)
{
pNode New=(pNode)malloc(sizeof(Node));//开辟新结点
printf("\n请输入第%d个结点值:",i);
scanf("%d",&New->data);
end->next=New;//尾结点指向新结点
New->next=NULL;//新结点指向NULL表头指针
end=New;//当前尾结点为新结点
}
return head;
}
//3.顺序遍历单链表
void select(pNode test)
{
pNode p=test->next;
printf("此单链表中的值有:");
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
pNode test=createlist();
select(test);
return 0;
}
实验课题二:单链表的创建、遍历
最新推荐文章于 2022-01-12 15:59:45 发布