|
![]() | 作者: zhoen889 [zhoen889]
![]() |
登录 |
//program 9_1.cpp #include<iostream.h> #include<process.h> #include<iomanip.h> template<class T> class list { struct node { T data; node *next; }*head,*tail; public: list(){head=tail=NULL;} void Insert(T *item) { node *pn; pn=new node;//为节点分配内存 pn->next=head; pn->data=*item; head=pn; if(tail==NULL) tail=pn; } void Append(T *item) { node *pn; pn=new node;//为节点分配内存 pn->next=NULL; pn->data=*item; if(tail==NULL) head=tail=pn; else { tail->next=pn; tail=pn; } } T get() { if(head==NULL) exit(0); T temp=head->data; node *pn=head; if(head->next==NULL) head=tail=NULL; else head=head->next; delete(pn); return (T)temp; } }; class person { public: char name[10]; int age; float hight; }; void main() { person ps[5]; list<int>link1; list<person>link2; for(int i=0;i<5;i++) { cout<<"请输入姓名:"; cin>>ps[i].name; cout<<"请输入年龄:"; cin>>ps[i].age; cout<<"请输入身高:"; cin>>ps[i].hight; link2.Insert(&ps[i]); link1.Append(&i); } for(i=0;i<5;i++) { link2.get(); link2.Append(&ps[i]); cout<<ps[i].name<<setw(3)<<ps[i].age<<setw(6)<<ps[i].hight<<"cm"<<setw(5)<<link1.get()<<endl; } } [此贴被 zhoen889(zhoen889) 在 12月05日20时57分 编辑过] |
地主 发表时间: 12/05 20:43 |
![]() | 回复: cyshaoping [cyshaoping] ![]() |
登录 |
你要干什么? |
B1层 发表时间: 12/05 21:07 |
![]() | 回复: lwei889 [lwei889] ![]() |
登录 |
这好象是一个关于类模板的链表! |
B2层 发表时间: 12/06 10:57 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号