数据结构实验之链表一:顺序建立链表
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;
}