1.题目:
5
12 32 45 78 54
12 32 45 78 54
2.参考代码:
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
class LinkList{
private:
Node* head;
public:
LinkList(int* a,int n);
~LinkList();
void show();
};
LinkList::LinkList(int* a,int n){
Node* r,* s;
head=new Node;
r=head;
for(int i=0;i<n;i++){
s=new Node;
s->data=a[i];
s->next=r->next;
r->next=s;
r=s;
}
r->next=NULL;
}
LinkList::~LinkList(){
Node* p,* q;
p=head;
while(p){
q=p;
p=p->next;
delete q;
}
}
void LinkList::show(){
Node* p=head->next;
if(p){
cout<<p->data;
p=p->next;
while(p){
cout<<" "<<p->data;
p=p->next;
}
cout<<endl;
}
}
int main()
{
int i,n,a[1111];
while(cin>>n){
for(i=0;i<n;i++)
cin>>a[i];
LinkList w(a,n);
w.show();
}
return 0;
}