njlulu njlulu
关注数: 6 粉丝数: 39 发帖数: 697 关注贴吧数: 23
用抽象类构造通用链表…… 如题,出现了fatal error C1004: unexpected end of file found。。。求大神纠错。。程序本身我也写的稀里糊涂。。谢谢啦~ #include<iostream> using namespace std; class Object { public: Object() {} virtual ~Object() {} virtual void show()=0; virtual int compare(Object *pb)=0; }; class Node { private: Object *pValue; Node *next; public: Node(Object *p=0) {pValue=p;} friend class Link; }; class Link { private: Node *head,*tail; public: Link(){head=0;tail=&head;} ~Link() { while(head) {node *p=head;head=p->next;delete p;} void delTotal() { node *p;while(p=head) {head=head->next;delete p;}} void add(Object *p) { Node temp={0,head},*q1=&temp,*q2=head; while(q2!=0&&q2.compare(p)<0) {q1=q2;q2=q2->next;} p->next=q2;q1-next=p; } void del(Object *p) { Node temp={0,head}; Node *q1=&temp,*q2=head; while(q2!=0&&q2.compare(p)) {q1=q2;q2=q2->next;} if(q2!=0) {q1->next=q2->nxt;delete q2;} } void show() { while(head!=0) {cout<<head->pValue<<'\t';head=head->next;} cout<<endl;} }; class DoubleObject:public Object { private: double data; public: DoubleObject(double d):data(d) {} ~DoubleObject() {} void show() { cout<<data<<endl;} int compare(Object *pb) { double t=data-((DoubleObject*)pb)->data; return t<0?-1:(t>0?1:0);} }; class StringObject:public Object { private: char *s; public: StringObject(char *ss) {s=new char[strlen(ss)+1];strcpy(s,ss);} ~StringObject() {delete[]s;} void show() { cout<<s<<endl;} int compare(Object *pb) { return strcmp(s,((StringObject*)pb)->s); } }; int main() { Link a;int i;char str[80]; for(i=15;i>=11;i--) a.add(&DoubleObject(i/10.0)); a.show(); DoubleObject t1(0.3),t2(1.3);a.del(&t1);a.del(&t2);a.show(); a.delTotal(); for(i=1;i<=5;i++) { cin>>str;a.add(&StringObject(str));} a.show(); return 0; }
1 下一页