还有一点小错误!
要求:
1:利用链表模拟栈
2:演示栈的基本操作
3:栈采用模板,栈的节点需要是一个描述学生记录的类对象
template<class T>
class genabstractstack
{
protected:
unsigned height;
public:
bool isempty()
{
return(height==0)? true:false;
}
virtual void push(T &)=0;
virtual bool pop(T &)=0;
virtual void clear()=0;
};
template<class T>
struct genstackrec
{
T nodedata;
genstackrec * next;
};
template<class T>
class genstack:public genabstractstack<T>
{ protected:
bool allocateerror;
genstackrec<T> * top;
genstack & copy(genstack &);
public:
genstack();
genstack(genstack & g)
{top=NULL;copy(g);}
~genstack()
{clear();}
bool getallocateerror()
{return allocateerror;}
virtual void clear();
virtual void push(T & x);
virtual bool pop(T & x);
genstack & operator=(genstack & g)
{copy(g);return * this;}
};
template<class T>
genstack<T>::genstack()
{
height=0;
allocateerror=false;
top=NULL;
}
template<class T>
genstack<T>&genstack<T>::copy(genstack<T>&g)
{
genstackrec<T> *p,*q,*r;
if(top) clear();
height=g.height;
allocateerror=false;
top=NULL;
if(! g.top)
return * this;
top=new genstackrec<T>;
if(! top)
{
allocateerror=true;
return * this;
}
top->next=NULL;
top->nodedata=g.top->nodedata;
q=g.top->next;
p=top;
while(q)
{
r=new genstackrec<T>;
if(! r)
{allocateerror=true;
return * this;
}
r->nodedata=q->nodedata;
r->next=NULL;
p->next=r;
p=p->next;
q=q->next;
}
return * this;
}
template<class T>
void genstack<T>::clear()
{
T x;
while(pop(x));
}
template<class T>
void genstack<T>::push(T & x)
{
genstackrec<T> * p;
allocateerror=false;
if(top)
{
p=new genstackrec<T>;
if(!p)
{
allocateerror=true;
return;
}
p->nodedata=x;
p->next=top;
top=p;
}
else
{
top=new genstackrec<T>;
if(!top)
{
allocateerror=true;
return;
}
top->nodedata=x;
top->next=NULL;
}
height++;
}
template<class T>
bool genstack<T>::pop(T & x)
{
genstackrec<T> * p;
if(height)
{
x=top->nodedata;
p=top;
top=top->next;
delete p;
height--;
return true;
}
return false;
}
#include<string.h>
#include<stdio.h>
#include<dos.h>
#include<iostream.h>
#include<conio.h>
#include "StdAfx.h"
class student
{
public:
char name[80];
int age;
float mark_average;
char sex[8];
void scanf(char *name,int age,float mark_average,char *sex);
void print();
};
void student::scanf(char *name,int age,float mark_average,char *sex)
{
strcpy(student::name,name);
student::age=age;
student::mark_average=mark_average;
strcpy(student::sex,sex);
}
void viewstack(genstack <student> &stack)
{
student p;
int i=1;
genstack <student> stackcopy(stack);
while(stackcopy.pop(p))
{
printf("%2d:",i++);
p.print();
}
}
main()
{ student p;
genstack<student>stack;
int i;
for(i=0;i<4;i++)
{
//cin>>p;
scanf("请输入学生的数据:");
stack.push(p);
}
while(stack.pop(p))
{
viewstack(stack);
}
stack.clear();
return 1;
}
--------------------Configuration: ll - Win32 Debug--------------------
Linking...
ll.obj : error LNK2001: unresolved external symbol "public: void __thiscall student::print(void)" (?print@student@@QAEXXZ)
Debug/ll.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
ll.exe - 2 error(s), 0 warning(s)