您的位置:首页 > 其它

顺序建立链表 分类: 链表 2015-06-07 12:43 15人阅读 评论(0) 收藏

2015-06-07 12:43 267 查看
数据结构实验之链表一:顺序建立链表

TimeLimit: 1000MS Memory limit: 65536K

题目描述

输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。

输入

第一行输入整数的个数N;

第二行依次输入每个整数。

输出

输出这组整数。

示例输入

8

1256 4 6 55 15 33 62

示例输出

1256 4 6 55 15 33 62

#include <bits/stdc++.h>
#define WW freopen("input.txt","r",stdin)
#define RR freopen("ouput.txt","w",stdout)
using namespace std;
struct node
{
int data;
node *next;
}*head;
int n;
void Creat()
{
node *p,*tail;
tail=head;
for(int i=1; i<=n; i++)
{
p=new node;
p->next=NULL;
cin>>p->data;
tail->next=p;
tail=p;
}
}

void Output()
{
node *p;
p=head->next;
while(p)
{
if(p!=head->next)
cout<<" ";
cout<<p->data;
p=p->next;//这里不要忘记
}
cout<<endl;
}
int main()
{
head=new node;
head->next=NULL;
//    WW;
//    RR;
cin>>n;
Creat();
Output();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: