YTU 2620: B 链表操作
2024-08-26 07:36:32
2620: B 链表操作
时间限制: 1 Sec 内存限制: 128 MB
提交: 418 解决: 261
题目描述
(1)编写一个函数createlink,用来建立一个动态链表(链表中的节点个数由参数count来控制)。
节点结构如下:
struct Node
{
int data;
Node * next;
};
函数createlink的声明如下:
Node * createlink(int count);
(2)编写一个函数printlink,用来遍历输出一个链表。
函数printlink的声明如下:
void printlink(Node * head);
(3) 在主程序中调用函数createlink来动态创建链表,然后调用printlink函数遍历输出链表节点数据。
主程序如下:(提交时不用提交主程序)
int main()
{
Node * head=NULL;
int n;
cin>>n;
head=createlink(n);
printlink(head);
return 0;
}
输入
输入要建立链表的节点个数
输入各个节点的数据
输出
输出链表中各个节点的数据
样例输入
4
1 2 3 4
样例输出
1 2 3 4
提示
提交时不用提交主程序,其他都要提交
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include<iostream>
using namespace std;
struct Node
{
int data;
Node * next;
};
Node * createlink(int count)
{
Node *p=new Node;
if(count==0)
{
p->next=NULL;
return p;
}
cin>>p->data;
p->next=createlink(count-1);
return p;
}
void printlink(Node * head)
{
if(head!=NULL)cout<<head->data;
while(head->next->next!=NULL)
{
cout<<" "<<head->next->data;
head=head->next;
}
}
int main()
{
Node * head=NULL;
int n;
cin>>n;
head=createlink(n);
printlink(head);
return 0;
}
using namespace std;
struct Node
{
int data;
Node * next;
};
Node * createlink(int count)
{
Node *p=new Node;
if(count==0)
{
p->next=NULL;
return p;
}
cin>>p->data;
p->next=createlink(count-1);
return p;
}
void printlink(Node * head)
{
if(head!=NULL)cout<<head->data;
while(head->next->next!=NULL)
{
cout<<" "<<head->next->data;
head=head->next;
}
}
int main()
{
Node * head=NULL;
int n;
cin>>n;
head=createlink(n);
printlink(head);
return 0;
}
最新文章
- html5标签的改变
- GPL与LGPL的区别
- ASP.NET MVC 4 Web编程
- SVN 的使用
- 机器学习&;&;数据挖掘之一:决策树基础认识
- STL函数模板(即算法)一览
- ubuntu常用文件搜索命令 分类: linux 学习笔记 ubuntu 2015-07-05 15:40 84人阅读 评论(0) 收藏
- UE4创建空白关卡并添加碰撞体
- git 客户端提交
- [笔记]Linux命令行大全
- Myeclipse 10安装与破解
- [APIO2015]八邻旁之桥
- 如何获得Android手机的软件安装列表
- websocket(二)--简单实现网页版群聊
- JavaScript原型链和继承
- Hero Patterns - 聚合各种 SVG 背景纹理素材的网站
- 猫眼电影爬取(三):requests+pyquery,并将数据存储到mysql数据库
- gitlab ssh clone问题解决
- compatible with
- JavaScript中七种数据类型&#183;中&#183;一