倒序输出链表

来源:互联网 发布:.net core cms 编辑:程序博客网 时间:2024/05/22 11:40
//递归实现#include <iostream>#include <cstdio>using namespace std;class node{public:    int data;    node * next;    node(int d):data(d),next(NULL)    {    }};class lists{    node * head;    node * tail;public:    lists();    void add(int d);    void order(node*);    void output();};lists::lists(){    head=NULL;    tail=NULL;}void lists::add(int d){    node * p=new node(d);    if(head==NULL)    {        head=p;        tail=head;    }    else    {        tail->next =p;        tail=p;    }}void lists::output()     //我好机智啊{    order(head);}void lists::order(node * tmp){    if(tmp->next!=NULL)    {        order(tmp->next);    }    printf("%d",tmp->data);}int main(){    lists l;    int data;    //freopen("in.txt","r",stdin);    while(cin>>data&&data!=-1)    {        l.add(data);    }    l.output();}


0 0
原创粉丝点击