C++ 使用模板实现一个List的实例
C++使用模板写的一个List
templateclassList { private: structNode { Tdata; Node*next; }; //head Node*head; //size intlength; //process Node*p; //temp Node*q; public: List() { head=NULL; length=0; p=NULL; } voidadd(Tt) { if(head==NULL) { q=newNode(); q->data=t; q->next=NULL; length++; head=q; p=head; } else { q=newNode(); q->data=t; q->next=NULL; length++; p->next=q; p=q; } } voidremove(intn) { if(n>=length) { return; } length--; //删除头节点 if(n==0) { q=head; head=head->next; delete(q); } else { q=head; for(inti=0;i next; } Node*t=q->next; q->next=q->next->next; delete(t); } // p=head; if(p!=NULL) { while(p->next!=NULL) { p=p->next; } } } intgetSize() { returnlength; } intgetLength() { returngetSize(); } Tget(intn) { q=head; for(inti=0;i next; } returnq->data; } };
调用方式如下
Listlist; Stustu1; Stustu2; Stustu3; stu1.username="1"; stu2.username="2"; stu3.username="3"; list.add(stu1); list.remove(0); list.add(stu2); list.add(stu3); for(inti=0;i 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练