逆序建立链表 分类: 链表 2015-06-07 12:46 10人阅读 评论(0) 收藏

数据结构实验之链表二:逆序建立链表

TimeLimit: 1000MS Memory limit: 65536K

题目描述

输入整数个数N,再输入N个整数,按照这些整数输入的相反顺序建立单链表,并依次遍历输出单链表的数据。

输入

第一行输入整数N;

第二行依次输入N个整数,逆序建立单链表。

输出

依次输出单链表所存放的数据。

示例输入

10

113 5 27 9 12 43 16 84 22

示例输出

2284 16 43 12 9 27 5 3 11

#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;
    for(int i=1; i<=n; i++)
    {
        p=new node;
        p->next=head->next;
        cin>>p->data;
        head->next=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;
    cin>>n;
    Creat();
    Output();
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/juechen/p/4722064.html