#include "stdafx.h"
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
//链式栈的抽象数据类型定义
template <class T>
class Stack{
bool clear();
bool push();
bool pop();
bool top1();
void display();
int length();
bool getValue();
bool isExist();
};
//定义结点
template <class T>
class Link{
public:
T data;
Link <T> * next ;
};
//定义栈
template <class T>
class lnkStack {
private:
int size; //栈中最多可存放的元素个数
Link<T> *top; //做确定栈的位置的指针
public:
lnkStack(){
top = NULL;
size = 0;
}
//存在链栈
template <class T>
bool isExist(){
if(top == NULL){
cout<<"链栈不存在,无法执行操作"<<endl;
return false;
}
else return true;
}
//1.清空栈
bool clear(){
if (isExist == true){
if (size == 0){
cout<<"栈已为空,无法进行清除!"<<endl;
return false;
}
else{
Link <T> *tmp;
while (top->next != NULL){
tmp = top->next;
top->next = tmp->next;
delete tmp;
}
cout<<"链栈清除成功!"<<endl;
return true;
}
}
else return false;
}
//2.显示栈
/* bool display(){
if (isExist() == true){
Link<T> *tmp;
tmp = top->next;
while (tmp != NULL){
cout<<tmp->data<<" ";
tmp = tmp->next;
}
cout<<endl;
return true;
}
else return false;
}
*/
//3.求栈长
/* int length(){
if (isExist() == true){
if (size == 0){
cout<<"栈为空!"<<endl;
}
else{
Link<T> *tmp;
tmp = top->next;
int i=0;
while (tmp != NULL){
i++;
tmp = tmp->next;
}
cout<<"栈长为:"<<i<<" "<<endl;
return true;
}
}
else return false;
}
*/
//4.入栈
bool push(){
if (isExist() == true ){
Link<T> *tmp;
T item;
cout<<"请输入要入栈的元素值:"<<endl;cin>>item;
tmp = new Link<T>;
tmp->data = item;
tmp->next = NULL;
top->next = tmp;
top = tmp;
return true;
}
else return false;
}
//5.出栈
/* bool pop(){
if (isExist() == true ){
if(size == 0){
cout<<"栈为空!"<<endl;
}
else{
Link<T> *tmp;
T item;
item = top->data;
tmp = top->next;
delete top;
top = tmp;
size--;
return true;
}
else return false;
}
}
*/
//6.显示栈顶元素
bool getValue(){
if (isExist() == true){
if (size == 0){
cout<<"栈为空!"<<endl;
}
else{
T item;
item = top->data;
cout<<"栈顶元素的值为:"<<item<<" "<<endl;
return true;
}
}
else return false;
}
//7.创建栈
bool create(){
if (isExist() == true){
cout<<"栈已存在,无法创建!"<<endl;
return false;
}
else{
top = new Link<T>;
top->next = NULL;
size = 0;
return true;
}
}
//8.销毁栈
bool destroy(){
if(isExist() == false){
cout<<"栈不存在,找不到对象!"<<endl;
return false;
}
else{
if (size != 0){
Link<T> *tmp;
tmp = top;
while (top != NULL){
top = top->next;
delete tmp;
tmp = top;
}
return true;
}
}
}
//
bool top1(T & item){
if(size == 0){
cout<<"栈为空,不能读取栈顶元素"<<endl;
return false;
}
else{
item = top->data;
return true;
}
}
};
//主函数
void main(){
lnkStack<char> ls;
int choice;
bool ok;
do{
cout<<"(0退出, 1清除, 2显示, 3栈长, 4入栈, 5出栈, 6读栈,7创建,8销毁 :)"<<endl;
cin>>choice;
switch (choice){
case 0:
cout<<"再见!\n";
break;
case 1:
ls.clear();
break;
/* case 2:
ok = ls.display();
if(ok == false){
cout<<"显示失败!"<<endl;
}
else{
cout<<"显示成功!"<<endl;
}
break;
case 3:
ls.length();
break;
case 4:*/
ok = ls.push();
if(ok == true){
cout<<"入栈成功!"<<endl;
}
else{
cout<<"入栈失败!"<<endl;
}
break;
/*case 5:
ok=ls.pop();
if(ok==true) {
cout<<"出栈成功"<<endl;
}
else{
cout<<"出栈失败!"<<endl;
}
break;*/
case 6:
ok = ls.getValue();
break;
case 7:
ok = ls.create();
if (ok==true ){
cout<<"栈创建成功!\n";
}
else {
cout<<"栈创建失败!\n";
}
break;
case 8:
ok = ls.destroy();
if (ok==true ){
cout<<"销毁成功!\n";
}
else {
cout<<"销毁失败!\n";
}
break;
default: cout<<"选择错误!";
}
} while (choice != 0);
}
error C2783: 'bool __thiscall lnkStack<char>::isExist(void)' : could not deduce template argument for 'T'
while compiling class-template member function 'bool __thiscall lnkStack<char>::push(void)'
41行的这个代码是不是应该删除template <class T>
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html
C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html