关于栈的问题!
要求:
1:利用链表模拟栈
2:演示栈的基本操作
3:栈采用模板,栈的节点需要是一个描述学生记录的类对象
(帮小弟改一改吧!!!我都要疯拉!!!!我用的 是两个文件存放的,header,souce)
template<class T>
class genabstractstack
{
protected:
unsigned height;
public:
bool isenpty()
{
return(height==0)?true:false;
}
virtual void push(T &)=0;
virtual bool pop(T &)=0;
virtual void clear()=0;
virtual void check()=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);
virtual void check(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)
{
allcocateerror=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;
}
template<class T>
void genstack<T>::check(T & p)
{
T temp;
if(top==0)
{
cout<<"栈为空,不能进行数据查询!"<<endl;
//exit(1);
}
else
for(int n=0;n<6;n++)
{
temp=s[top-n];
if(p!=temp)
{}
else
{cout<<"该数据在栈中。"<<endl;
break;}
if(n==top-n)cout<<"该数据不在栈中。"<<endl;
}
}
#include "stdafx.h"
#include<string.h>
#include<stdio.h>
#include<dos.h>
#include<iostream.h>
class student
{
public:
char name[80];
int age;
float mark_average;
char sex[8];
//void assign(char *name,int age,float mark_average,char *sex);
void print();
};
//void student::assign(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);
//clrscr();
while(stackcopy.pop(p))
{
gotoxy(1,i+5);
printf("%2d:",i++);
p.print();
}
}
main()
{
genstack g;
for(s=0;s<80;s++)
{ cin>>s;
g.push(s);
}
student s;
while(stack.pop(s))
{
viewstack(stack);
delay(1000);
}
stack.clear();
return 1;
}
--------------------Configuration: ll - Win32 Debug--------------------
Compiling...
ll.cpp
d:\ll\stdafx.h(145) : error C2244: 'genstack<T>::push' : unable to resolve function overload
d:\ll\stdafx.h(146) : error C2954: template definitions cannot nest
d:\ll\ll.cpp(27) : error C2259: 'genstack<class student>' : cannot instantiate abstract class due to following members:
d:\ll\ll.cpp(27) : warning C4259: 'void __thiscall genabstractstack<class student>::push(int)' : pure virtual function was not defined
d:\ll\stdafx.h(30) : see declaration of 'push'
d:\ll\ll.cpp(27) : warning C4259: 'void __thiscall genabstractstack<class student>::check(void)' : pure virtual function was not defined
d:\ll\stdafx.h(33) : see declaration of 'check'
d:\ll\ll.cpp(27) : error C2259: 'genstack<class student>' : cannot instantiate abstract class due to following members:
d:\ll\ll.cpp(27) : warning C4259: 'void __thiscall genabstractstack<class student>::push(int)' : pure virtual function was not defined
d:\ll\stdafx.h(30) : see declaration of 'push'
d:\ll\ll.cpp(27) : warning C4259: 'void __thiscall genabstractstack<class student>::check(void)' : pure virtual function was not defined
d:\ll\stdafx.h(33) : see declaration of 'check'
d:\ll\ll.cpp(31) : error C2065: 'gotoxy' : undeclared identifier
d:\ll\ll.cpp(39) : error C2955: 'genstack' : use of class template requires template argument list
d:\ll\stdafx.h(65) : see declaration of 'genstack'
d:\ll\ll.cpp(39) : error C2133: 'g' : unknown size
d:\ll\ll.cpp(39) : error C2512: 'genstack' : no appropriate default constructor available
d:\ll\ll.cpp(39) : error C2262: 'g' : cannot be destroyed
d:\ll\ll.cpp(40) : error C2065: 's' : undeclared identifier
d:\ll\ll.cpp(45) : error C2065: 'stack' : undeclared identifier
d:\ll\ll.cpp(45) : error C2228: left of '.pop' must have class/struct/union type
d:\ll\ll.cpp(45) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
ll.exe - 13 error(s), 4 warning(s)