关于栈的问题!

840206 2004-08-29 10:21:41
要求:
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)
...全文
188 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yohua 2004-08-30
  • 打赏
  • 举报
回复
无法面对,风格不是很好。看不清楚!
cadkeji 2004-08-30
  • 打赏
  • 举报
回复
看不了呀。
Paris_Luo 2004-08-30
  • 打赏
  • 举报
回复
太长了,兄弟
littleeagle007 2004-08-30
  • 打赏
  • 举报
回复
搂主的太程序太混乱
Daviescai 2004-08-30
  • 打赏
  • 举报
回复
看看数据结构的书吗,有很多的例子,可以参考
小崔爱读书 2004-08-30
  • 打赏
  • 举报
回复
楼主这么提问不行的,你这么问谁会回答?我们认为你都不知道自己要问什么,只是要我们替你写程序呀。你要自己找到错误点,然后提问。明白?
积木 2004-08-29
  • 打赏
  • 举报
回复
我忍不了了,里面的单词都写错了,格式也非常得不好。
你应该去看看模板,然后把你的实现和定义放在一个头文件里面。
sharkhuang 2004-08-29
  • 打赏
  • 举报
回复
标准模板库有啊!
或者着本数据机构的c++版本.
840206 2004-08-29
  • 打赏
  • 举报
回复
我在线等待!
840206 2004-08-29
  • 打赏
  • 举报
回复
怎么没人会吗?
freefalcon 2004-08-29
  • 打赏
  • 举报
回复
学会看错误提示, 里面真的说得很清楚啊

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧