#include <iostream> using namespace std; struct Node { int data; Node * next; Node(int x=0):data(x),next(NULL){} }; class List { public: List() { first = new Node(); } void Insert(int x) { Node *s = new Node(x); s->next = first->next; first->next = s; } void Show() { Node *p = first; while(p->next!=NULL) { cout<<p->next->data<<" "; p=p->next; } cout<<endl; } void Grail()//逆序 { Node *p = first; Node *q = first->next; p = NULL; while(q!=NULL) { Node* m =q->next; q->next = p; p = q; q = m ; } first->next=p; } private: Node *first; }; int main() { int Item; List list; while(cin>>Item,Item!=-1) { list.Insert(Item); } list.Show(); list.Grail(); list.Show(); return 0; }
时间: 2024-10-08 09:24:14