#include using namespace std; typedef int T; class List{ struct Node{ T data; Node *next; Node(const T&d):data(d),next(NULL){} //Node(){} }; Node *head; int len; public: List():len(0),head(NULL){} void push_front(const T&d)//头插法 { Node *p=new Node(d); if(head==NULL) head=p; else { p->next=head;//使p和head指向相同 head=p; } } void push_back(const T&d)//尾插法 { static Node *r;//防止r在出了这个作用域之后就释放 Node *p=new Node(d); if(head==NULL) {head=p;r=p;} else { r->next=p; r=p; r->next=NULL; } } Node *&getptr(int pos)//得到指定链表中的位置 { if(pos<0) pos=0; if(pos==0) return head; Node *p=head; for(int i=0;inext; return p->next; } void insert(const T&d,int pos)//在任意位置插入 { Node *&pn=getptr(pos); Node *p=new Node(d); p->next=pn->next; pn->next=p; } int Num()//统计结点个数 { Node *p=head; int cnt=0; while(p) { cnt++; p=p->next; } return cnt; } void clear()//清空链表 { while(head) { Node *p=head->next; delete head; head=p; } } void erase(int pos)//删除指定位置的元素 { if(pos<0) return; Node *&p=getptr(pos);//引用保证是原始指针 Node *pn=p; p=p->next; delete pn; } void remove(const T&d)//删除指定元素 { int pos=0; Node *p=head; while(p) { if(p->data==d) { erase(pos); return; } p=p->next; pos++; } } void set(int pos,const T& d)//修改指定位置处的元素 { if(pos<0) return; getptr(pos)->data=d; } void revise()//单链表的逆置 { Node *p,*q; p=head->next; head->next=NULL; while(p!=NULL) { q=p->next; p->next=head; head=p; p=q; } } void print()//输出 { Node *p=head; while(p) { cout<data<<' '; p=p->next; } cout<