各位高人,请问一下,如果想使这个程序可以任意输入数字,然后再逆置,要如何改呢?
#include<iostream>
using namespace std;
struct node
{
int data;
node *pre;
};
class mylist
{
private:
node *head;
node *tail;
int size;
public:
mylist(){head=NULL;tail=NULL;size=0;}
void push_back(int a);
int pop_back();
};
void mylist::push_back(int a)
{
node *p;
p=new node;
p->data =a;
p->pre=tail;
tail=p;
if(head==NULL){head=p;head->pre=NULL;}
size++;
}
int mylist::pop_back()
{
node *p,p1;
p=head;
int temp;
if(head==tail)
{
temp=tail->data;
delete tail;
tail=NULL;
head=NULL;
}
else
{
temp=tail->data;
p=tail;
tail=tail->pre;
delete p;
}
return temp;
}
int main()
{
mylist a;
for(int i=0;i<10;i++)
a.push_back(i);
for(i=0;i<10;i++)
cout<<a.pop_back()<<endl;
}