我想问关于顺序栈的为题,里面的元素是一个struct结构的,我怎么也弄不好……

来源:百度知道 编辑:UC知道 时间:2024/07/04 11:30:47
如果元素是int的话就没问题,可是……如果元素是struct结构定义的话,就错误连连了,我想请教一下有什么解决办法。
//stack.h
typedef stuct DataType{
public:
int x;
int y;
};

class stack{
public:
DataType *Vec;
int MaxSize;
int top;
public:
stack(int size);
~stack();
void Push(DataType *x);
DataType Pop();
DataType GetTop();
void clear();
int IsEmpty();
};

//stack.cpp
#include "stack.h"
#include<iostream>
using namespace std;

stack::stack(int size)
{
Vec=new DataType[size];
MaxSize=size;
top=-1;
}

stack::~stack()
{
delete[]Vec;
}

void stack::Push(DataType *x)
{
if(top==MaxSize-1)
cout<<"overflow!"<<endl;
else{
top++;
Vec[top]=x;
}
}

DataType stack::Pop()
{
DataType Temp=NULL;
if(top=-1)
cout<<"

typedef stuct DataType{
public:
int x;
int y;
};
typedef的语法被你弄错了

这个语法可以这样理解, 如果去掉typedef就成为一个定义变量的语法, 加上typedef后, 这个变量的名字就变成它的类型的名字

所以这句应该这样写:
typedef stuct{
public:
int x;
int y;
} DataType;