c/c++:逆序建立链表

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

#include <iostream>
#include <cstring>
using namespace std;
struct node
{
    int data;
    struct node *next;
};

int main ()
{
    std::ios::sync_with_stdio(false);
    struct node *head, *tail, *p;
    head = new node;
    head->next = NULL;
    tail = head;
    int N ;
    int tmpN;
    cin>>N;
    {
        tmpN = N;
        while(tmpN--)
        {
            p = new node;
            cin >> p->data ;
            p->next = head->next;
            head->next = p;
        }
        p = head->next;
        while(p->next!=NULL)
        {
            cout<<p->data<<' ';
            p=p->next;
        }
        cout<<p->data<<' ';

        cout<<endl;
    }

    return 0;
}

评论列表